如果单词不存在,Discord bot Ignore命令

2024-05-16 23:58:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果用户消息中没有该单词,我试图忽略该命令,但没有成功

我现在对该命令的代码如下:

@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!')

任何帮助都将不胜感激

多谢各位


Tags: in命令服务器消息messageifnot机器人
2条回答

尝试使用以下方法:

@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。我还删除了else语句中的if语句,因为它是不必要的

由于您已经创建了自己的on_message方法,因此您必须负责告诉它处理消息末尾可能包含的命令,方法如下:

await bot.process_commands(message)

您可以根据希望bot如何工作来选择放置此选项,即,如果消息不包含“think”,则else语句只会处理命令

相关问题 更多 >