on_reaction_add 未运行

2022-01-15 00:00:00 python discord discord.py

问题描述

我是 discord.py 的新手,正在尝试制作一个翻译机器人.当用户对某个标志做出反应时,机器人会翻译它,但该事件永远不会被调用,因此我还没有代码来翻译任何消息.我知道它没有被调用,因为程序没有将 'x' 打印到控制台.

I'm new to discord.py and trying to make a translator bot. When the user reacts with a certain flag, the bot translates it, but the event is never getting called hence I have no code to translate any messages yet. I know it's not getting called because the program isn't printing an 'x' to the console.

@client.event
async def on_reaction_add(reaction, user):
    channel = reaction.message.channel
    print('x')
    await client.send_message(channel, '{} has added {} to the the message {}'.format(user.name, reaction.emoji, reaction.message.content))

    await client.process_commands(reaction.message)


解决方案

事件没有注册/调用的原因并不多.

There isn't much valid reason for why the event isn't registered/called.

其中一个在文档中说明:http://discordpy.readthedocs.io/en/async/api.html#discord.on_reaction_add.尝试立即对机器人在线后发送的消息添加反应.由于在机器人上线之前发送的消息将不会被机器人识别(不在 Client.messages 中).

One of which is stated in the docs: http://discordpy.readthedocs.io/en/async/api.html#discord.on_reaction_add. Try adding a reaction immediately to a message that is sent after the bot is online. Since messages sent before the bot is online will not be recognized by the bot (not in Client.messages).

如果在 Client.messages 缓存中没有找到消息,那么这个事件不会被调用.

if the message is not found in the Client.messages cache, then this event will not be called.

另一个可能的原因是在客户端循环开始之前从未定义过此函数.验证您的缩进.和/或尝试将函数直接放在 client = Bot(...) 下,以检查是否是问题所在.

Another possible reason is that this function was never defined before the client loop commenced. Verify your indentation. And/Or try placing the function directly under client = Bot(...), to check if this is the problem.

如果上述方法都不能解决您的问题,请发布一个最小的、完整的、可验证的示例(从上到下的简短可运行代码,表明您的问题).

If neither of the aforementioned solves your problem, please post a minimal, complete, verifiable example (a short runnable code from top to bottom that indicates your problem).

相关文章