现在我可以使机器人对单个单词做出反应,是否可以用一段代码使机器人对具有相同句子的另一组单词做出反应?
例如,我尝试过在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)
答案 0 :(得分: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
一旦发送了一条消息就停止循环,否则当同一条消息中出现多个单词时,机器人将发送多条消息。