【问题标题】:discord bot when somebody says a certain word (two words) bot relpys当有人说某个词(两个词)时,不和谐机器人机器人 relpys
【发布时间】:2020-03-26 04:06:14
【问题描述】:

我试图在 py 上创建一个不和谐的机器人,而我是 py 的新手

当有人说某些词时,我试图让我的机器人回复

例如,如果在 sentace bot 回复中出现“bot”和“not”,则回复“你认为 bot 不工作吗?” 我做了这个代码

    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")

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

谁能帮帮我?

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    您遇到的问题与机器人无关,它是 Python 布尔评估的工作方式。如果您运行像 orand 这样的二元运算符,那么 Python 将返回确定其真值的第一个事物。

    对于x and y,如果x 的计算结果为假,Python 将返回False,如果x 的计算结果为True,Python 将返回y 的值,因为这决定了and 声明。

    鉴于非空字符串的计算结果为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'
    

    因此,您的"bot" and "not" 评估为您提供"not",机器人将对其中包含“not”一词的任何消息做出反应。

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

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 1970-01-01
      • 2020-09-01
      • 2020-12-30
      • 2023-04-04
      • 1970-01-01
      • 2018-11-19
      • 2022-01-26
      • 2021-10-07
      相关资源
      最近更新 更多