对无效消息的反应 (discord.py)

时间:2021-01-12 13:41:53

标签: python discord discord.py

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

代码:

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

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

3 个答案:

答案 0 :(得分:2)

discord.Member.id 返回一个 hidden 类型的值。这意味着您不能将 int 值与 str 值进行比较。你只需要:

discord.Member.id

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

首先,正如其他用户已经提到的,您不能将 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