嘿,我正在用Python开发Discord Bot。 这是文档:https://discordpy.readthedocs.io/en/latest/index.html
如果该程序对消息做出反应,则该程序应赋予用户角色
我试图在函数本身中定义ctx
@client.event
async def on_raw_reaction_add(payload):
member = discord.utils.get(client.get_all_members())
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
check_in = ["612637944544624690"]
if str(payload.channel_id) in check_in:
await client.add_roles(member, role, reason="check-in")
这是在函数本身中没有ctx的错误:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "C:/Users/kaitr/PycharmProjects/Discord Tutorial/bot.py", line 39, in on_raw_reaction_add
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
NameError: name 'ctx' is not defined
函数本身就是ctx的错误:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
TypeError: on_raw_reaction_add() missing 1 required positional argument: 'ctx'
答案 0 :(得分:0)
您无权访问invocation context,因为这不是命令。您只能使用有效载荷的属性RawReactionActionEvent
对象。这包括一个guild_id
属性,可用于获取公会:
@client.event
async def on_raw_reaction_add(payload):
if payload.guild_id is None:
return # Reaction is on a private message
guild = client.get_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name="check-in")
member = guild.get_member(payload.user_id)
if str(payload.channel_id) in check_in:
await member.add_roles(role, reason="check-in")