简体   繁体   English

Discord.py bot 在输入另一个命令时从另一个命令发送消息

[英]Discord.py bot sends messages from a different command when another command is input

This is my code for a chatbot:这是我的聊天机器人代码:

if 'Hello' or 'hello' in message.content:
    await message.reply((random.choice(hello)))

if 'How are you' or 'how are you' in message.content:
    mention = ' Thanks for asking, ' + message.author.mention + '!'
    await message.channel.send((random.choice(howareyou)) + mention)

However, whenever I type 'hello' or 'how are you' into the chat separately, it sends both messages.但是,每当我在聊天中分别输入“你好”或“你好吗”时,它都会发送两条消息。 How do I fix this?我该如何解决?

I think it's because of two reasons:我认为是因为两个原因:

  1. The operator precedence make the interpreter check the in operator before the or operator. 运算符优先级使解释器在or运算符之前检查in运算符。
  2. For strings, anything that is not an empty string is considered as having the value True if a boolean is asked.对于字符串,如果询问 boolean,则任何非空字符串都被视为具有值 True。 You can have more details in the official documentation about what is considered True or False .您可以在官方文档中了解更多关于什么是TrueFalse的详细信息。

To your interpreter, your code looks like:对于您的解释器,您的代码如下所示:

if True or ('hello' in message.content):
    await message.reply((random.choice(hello)))

if True or ('how are you' in message.content):
    mention = ' Thanks for asking, ' + message.author.mention + '!'
    await message.channel.send((random.choice(howareyou)) + mention)

So:所以:

  1. 'hello' in message.content and 'how are you' in message.content becomes True of False depending on the situation 'hello' in message.content 'how are you' in message.content根据情况变为TrueFalse
  2. 'Hello' or True becomes True or True which will become True . 'Hello' or True变为True or True将变为True The same is true for 'Hello' or False . 'Hello' or False也是如此。 The same is true for 'How are you' 'How are you'也是如此
  3. So your two if will always see that the condition is True and will always execute因此,您的两个if将始终看到条件为True并且将始终执行

To correct your situation simply:简单地纠正你的情况:

if 'Hello' in message.content or 'hello' in message.content:
    await message.reply((random.choice(hello)))

if 'How are you' in message.content or 'how are you' in message.content:
    mention = ' Thanks for asking, ' + message.author.mention + '!'
    await message.channel.send((random.choice(howareyou)) + mention)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM