Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

python - 当有人说出某个字词(两个字)时,discord bot relpys(discord bot when somebody says a certain word (two words) bot relpys)

im trying to make a discord bot on py and im new to py

(我正在尝试在py上创建一个不和谐的机器人,并且对py来说是新的)

im trying to make my bot reply when someones says certain word

(我试图让我的机器人在有人说某些字时回复)

for example if there is "bot" and "not" in sentace bot reply with "do you think bot are not working?"

(例如,如果sendace bot中回答“ bot”和“ not”,并回答“您认为bot不起作用?”。)

i made this code

(我写了这个代码)

    if message.content.find("bot" and "not") != -1:
        await message.channel.send("do you think bots aren't working? contact @sdu or ask other player in #bot channal")
        await message.channel.send("sorry for inconvenient")

but if i only say "not" bot will reply

(但是如果我只说“不”,机器人会回复)

can anyone help me?

(谁能帮我?)

  ask by dlrPaud translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue you're running into has nothing to do with the bot, it's how Python's boolean evaluation works.

(您遇到的问题与该机器人无关,这是Python的布尔值评估的工作方式。)

If you run a binary operator like or or and , then Python will return the first thing that determines its truth value.

(如果运行像orand这样的二进制运算符,则Python将返回确定其真值的第一件事。)

For x and y , Python will return False if x evaluates to false and if x evaluates to True , Python will return the value of y , as this determines the truth value of the and statement.

(对于x and y ,如果x计算结果为false,则Python将返回False ;如果x计算结果为True ,则Python将返回y的值,因为这将确定and语句的真值。)

Given that a non-empty string evaluates to True , the output in your example will always be determined by the second string.

(给定一个非空字符串的值为True ,示例中的输出将始终由第二个字符串确定。)

In [1]: True and 'bot'
Out[1]: 'bot'

In [2]: 'bot' and True
Out[2]: True

In [3]: 'bot' and 'not'
Out[3]: 'not'

In [4]: 'not' and 'bot'
Out[4]: 'bot'

Therefore, your "bot" and "not" evaluation gives you "not" and the bot will react to any message that has the word "not" in it.

(因此,您的"bot" and "not"评估结果为"not"并且该bot将对其中带有单词“ not”的任何消息作出反应。)

You need to change your condition to check for both words individually and react only if both checks pass

(您需要更改条件以单独检查两个单词,并且仅在两个检查均通过时才做出反应)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...