【问题标题】:How to make a discord bot react to a set of different words with the same set of sentences?如何使不和谐机器人对具有相同句子集的一组不同单词做出反应?
【发布时间】:2019-11-27 20:58:02
【问题描述】:

现在我可以让机器人对单个单词做出反应,是否有可能在一段代码中让机器人对具有相同句子的一组不同单词做出反应?

例如,我尝试了 message.content 中的“X”、“Y”和“Z”:当然它没有反应,但我不知道为什么以及如何使它成为可能?有人告诉我使用 any(),但我不知道如何使用它。

 xyz_quotes = ['QuotesX','QuotesY','QuotesZ']

    if 'X' in message.content:
        response = random.choice(xyz_quotes)
        await message.channel.send(response)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您可以使用一个简单的 for 循环来做到这一点:

    xyz_quotes = ['QuotesX','QuotesY','QuotesZ']
    trigger_words = ['X', 'Y', 'Z']
    
    for word in trigger_words:
        if word in message.content:
            response = random.choice(xyz_quotes)
            await message.channel.send(response)
            break
    

    这会遍历您定义的单词列表,然后检查这些单词是否在消息中。 break 在发送消息后停止循环,否则当同一消息中出现多个单词时,机器人将发送多条消息。

    【讨论】:

    • 非常感谢,这正是我所需要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2021-05-19
    • 1970-01-01
    • 2020-10-11
    • 2021-02-16
    • 2021-07-27
    • 2019-02-13
    相关资源
    最近更新 更多