【问题标题】:discord.py how to give a specific role when adding a reaction to a specific messagediscord.py 如何在对特定消息添加反应时赋予特定角色
【发布时间】:2020-09-06 09:23:33
【问题描述】:

经过一些研究,我发现自己在这里写: 我希望当用户对 对特定消息做出反应时,他必须给它一个角色并取消另一个角色。 但尝试它我不能在这里是我使用的代码:

spunta = "✅"

#Avvio del Bot
@client.event
async def on_ready():
    channel = client.get_channel(749639084267536464)
    global message
    message = message.id(751836025697075270)
    await message.add_reaction(msg, spunta)

@client.event
async def on_reaction_add(reaction, user):
    if reaction.user == client.user:
        return
    if reaction.message == message and reaction.emoji == spunta:
        role = get(user.guild.roles, id=691004248753832007)
        await client.add_roles(user, role)

错误:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 253, in on_ready
    message = message.id(751836025697075270)
NameError: name 'message' is not defined

【问题讨论】:

  • 您正在尝试获取message 对象,但您尚未在代码中的任何位置定义它。

标签: discord.py


【解决方案1】:

您可以使用on_raw_reaction_add,它通过RawReactionActionEvent 作为payload

注意:我使用 msg id 来查找消息本身

@client.event
async def on_raw_reaction_add(payload):
    channel = client.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    guild = client.get_guild(payload.guild_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)

    # only work if it is the client
    if payload.member.id == client.user.id:
        return

    if payload.message_id == 750089806872314038 and reaction.emoji == '✅':
        role = discord.utils.get(guild.roles, name='ROLE NAME HERE')
        await payload.member.add_roles(role)
        await reaction.remove(payload.member)

【讨论】:

    【解决方案2】:

    您收到此错误是因为您没有定义 message。您可以使用fetch_message 仅在特定频道上执行此操作(我不确定公会是否可能)。因此,在on_ready 事件中,您应该将message = message.id() 更改为message = channel.fetch_message(id)。这应该适合你。

    【讨论】:

    • 它不起作用,它现在给我这个errorcode
    • 尝试从await message.add_reaction(msg, spunta)中删除msg参数。
    • 结果没有变化(我也发了更新代码的照​​片)
    • 你确定消息和频道存在吗?
    • 我把ID放回去了,是的,它仍然给出同样的错误
    猜你喜欢
    • 2019-02-12
    • 2021-05-21
    • 2019-05-31
    • 2019-05-24
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2021-01-17
    相关资源
    最近更新 更多