【问题标题】: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('????')
我试过了,但它没有用,它甚至没有显示任何错误,我做错了什么?谢谢!
【问题讨论】:
标签:
python
discord
discord.py
【解决方案1】:
首先,正如其他用户已经提到的,您不能将int 与string. 进行比较,正确的语法是:
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:
# ...