我正在编写一个机器人,该机器人将根据与表情符号的反应来分配角色。我已经仔细检查了语法,discord.py文档,并交叉引用了一些YouTube视频,似乎所有内容都已签出,但到目前为止,该漫游器仍不会向成员添加角色。
{{1}}
任何帮助将不胜感激。
答案 0 :(得分:0)
请确保您设置了intents。另外,使用guild = client.get_guild(guild_id)
获取公会,使用member = get(guild.members, id=payload.user_id)
获取成员,使用role = get(payload.member.guild.roles, name='TestRole')
获取角色。这是youtube tutorial,介绍如何使用最新的discord.py设置角色响应机器人。当您使用if message_id == MESSAGE:
时,它什么都不会做,因为您需要在其中放置角色响应消息的实际整数ID。设置意图后,这应该可以工作:
from discord.utils import get
@client.event
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == 1234567890:
guild_id = payload.guild_id
guild = client.get_guild(guild_id)
# this wouldn't do anything because MESSAGE is not defined
print(MESSAGE)
if payload.emoji.name == 'test':
role = get(payload.member.guild.roles, name='TestRole')
print("TestRole")
else:
role = get(payload.member.guild.roles, name=payload.emoji.name)
if role is not None:
member = get(guild.members, id=payload.user_id)
if member is not None:
await payload.member.add_roles(role)
print(f"Added role to {member}")
else:
print("User not found . . .")
else:
print("Role not found . . .")