[英]Reaction to messages not working (discord.py)
I wanted the bot to react to the message if it's sent by a certain author.如果消息是由某个作者发送的,我希望机器人对消息做出反应。
Code:代码:
@bot.event
async def on_message(message):
if message.author.id == '356268235697553409':
await message.add_reaction('👍')
await message.add_reaction('👎')
await message.add_reaction('😐')
I tried this but it didn't work, it didn't even show any errors, what am I doing wrong?我试过了,但它没有用,它甚至没有显示任何错误,我做错了什么? Thanks!
谢谢!
discord.Member.id
returns you a int
type value. discord.Member.id
返回一个int
类型的值。 That means that you cannot compare a str
value to a discord.Member.id
value.这意味着您无法将
str
值与discord.Member.id
值进行比较。 You just have to do:你只需要做:
if message.author.id == 356268235697553409:
ID's are always integers, you're comparing an int to a string, to fix it: ID 始终是整数,您将 int 与字符串进行比较,以修复它:
if message.author.id == 356268235697553409:
# ...
First of all, as other users have already mentioned, you cannot compare an int
to a string.
首先,正如其他用户已经提到的那样,您不能将
int
与string.
The correct syntax would be:正确的语法是:
if message.author.id == 356268235697553409:
# Rest of your code here
Secondly, what you are doing is overriding the default provided on_message
command.其次,您正在做的是覆盖默认提供的
on_message
命令。 This forbids any commands from running, which might be the root cause of your issue.这会禁止运行任何命令,这可能是问题的根本原因。 The correct way of using
on_message
would be adding the following like to the end of your on_message
:使用
on_message
的正确方法是将以下内容添加到on_message
的末尾:
@bot.event
async def on_message(message):
# Your code
await bot.process_commands(message)
Alternatively, you an place your on_message
logic into a listener (without the need of manually calling bot.process_commands()
. This is done to allow multiple asynchronous actions in response to a message. Example:或者,您将
on_message
逻辑放入侦听器中(无需手动调用bot.process_commands()
。这样做是为了允许多个异步操作来响应消息。例如:
@bot.listen('on_message')
async def whatever_you_want_to_call_it(message):
# do stuff here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.