【问题标题】:Reaction to messages not working (discord.py)对消息不起作用的反应 (discord.py)
【发布时间】:2021-04-17 11:23:11
【问题描述】:

我希望机器人对某个作者发送的消息做出反应。

代码:

@bot.event
async def on_message(message):
    if message.author.id == '356268235697553409':
        await message.add_reaction('????')
        await message.add_reaction('????')
        await message.add_reaction('????')

我试过了,但它没有用,它甚至没有显示任何错误,我做错了什么?谢谢!

【问题讨论】:

  • 您是否验证过程序输入了if 语句?

标签: python discord discord.py


【解决方案1】:

首先,正如其他用户已经提到的,您不能将intstring. 进行比较,正确的语法是:

if message.author.id == 356268235697553409:
    # Rest of your code here

其次,您所做的是覆盖默认提供的on_message 命令。这会禁止运行任何命令,这可能是问题的根本原因。使用on_message 的正确方法是在on_message 的末尾添加以下类似:

@bot.event
async def on_message(message):
    # Your code
    await bot.process_commands(message)

或者,您可以将 on_message 逻辑放入侦听器中(无需手动调用 bot.process_commands()。这样做是为了允许多个异步操作来响应消息。示例:

@bot.listen('on_message')
async def whatever_you_want_to_call_it(message):
    # do stuff here

【讨论】:

  • 不,仍然没有,很遗憾。 :-/
【解决方案2】:

discord.Member.id 返回一个int 类型值。这意味着您无法将 str 值与 discord.Member.id 值进行比较。你只需要这样做:

if message.author.id == 356268235697553409:

【讨论】:

    【解决方案3】:

    ID 始终是整数,您将 int 与字符串进行比较,以修复它:

    if message.author.id == 356268235697553409:
        # ...
    

    【讨论】:

    • 感谢您的帮助,但很遗憾,这没有用。
    • 你启用了哪些意图?
    猜你喜欢
    • 2020-03-03
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    相关资源
    最近更新 更多