Discord bot - 如果单词不存在,则忽略命令

时间:2021-03-07 00:18:39

标签: python python-3.x discord bots

我对 python 非常陌生,我正在为我的服务器和乐趣制作一个不和谐的机器人。如果消息包含“思想”一词,我有一个命令可以对消息做出反应。但是这个命令使所有其他命令都不起作用。

如果用户消息没有那个词,我试图忽略这个命令,但没有成功。

我的那个命令的代码现在是这样的:

<块引用>
@bot.event
async def on_message(message):
    if 'thought' in message.content:
        emoji = '\U0001F64F'
        await message.add_reaction(emoji)
    else:
        if 'thought' not in message.content:
            print('Thought is not contained in the message!')

任何帮助将不胜感激!

谢谢

2 个答案:

答案 0 :(得分:1)

既然你已经创建了你自己的 on_message 方法,你必须负责告诉它处理可能在消息末尾的命令:

await bot.process_commands(message)

您可以根据您希望机器人如何工作来选择放置它,即如果消息不包含“想法”,则在 else 语句中只会处理命令。

答案 1 :(得分:0)

试试这个:

@bot.event
async def on_message(message):
    if 'thought' in message.content.lower():
        await message.add_reaction('\U0001F64F')
    else:
        print('Thought is not contained in the message!')

通过调用 str.lower() 方法,无论大小写,条件都会评估为 True。我还删除了 if 语句中的 else 语句,因为它是不必要的。