[英]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:我认为是因为两个原因:
in
operator before the or
operator. or
运算符之前检查in
运算符。True
or False
.True
或False
的详细信息。 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:所以:
'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
根据情况变为True
或False
'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'
也是如此if
will always see that the condition is True
and will always executeif
将始终看到条件为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.