简体   繁体   English

如何使 message.channel.send 命令需要来自同一个库的两个单词? (Python 和 Discord.py)

[英]How do I make a message.channel.send command require two words from the same library? (Python and Discord.py)

I am very new to coding so sorry if my phrasing is wrong, but I have this library of words我对编码很陌生,如果我的措辞有误,我很抱歉,但我有这个词库

california_triggers = [
  "California", "california", "Fire", "fire"
]

And I want to have the following command trigger only when a message has two of the words in said library并且我希望仅当消息在所述库中有两个单词时才触发以下命令

    if any(word in msg for word in california_triggers):
      await message.channel.send("California is currently on fire as of "+now_string+".")

How would I make "California is on fire as of now" require a trigger of two of the words from california_triggers?我如何让“加利福尼亚现在着火了”需要触发来自 california_triggers 的两个单词? I am very sorry as I am new to all of this.我很抱歉,因为我对这一切都不熟悉。 And would I require imports that deviate from just the discord library?我是否需要仅偏离 discord 库的导入?

First of all, it's not a library of words.首先,它不是一个词库。 Libraries have an entirely different meaning in python. 在 python 中具有完全不同的含义。 You have a list of words.你有一个单词列表。 Now checking if the msg has two of the words from the list, can be done in several ways.现在检查 msg 是否有列表中的两个单词,可以通过多种方式完成。

if len(set(msg.content.split()) & set(california_triggers)) == 2:
    #msg and california have two common words

Explanation:解释:

msg.content.split() splits the message by space. msg.content.split()按空格分割消息。 ie into words.即成词。
The & operation between two sets returns the intersection of the two sets.两个集合之间的&操作返回两个集合的交集。 ie common words.即常用词。
We check if the length of common words is 2 and then trigger the if block.我们检查常用词的长度是否为 2,然后触发 if 块。

References:参考:

Note:笔记:
Your question says that you haven't learnt enough python to start coding your own discord bot.您的问题表明您还没有学到足够的 python 来开始编写自己的 discord 机器人。 I would recommend learning Python from scratch , and OOP specifically for discord.py before you can make an actual bot我建议从头开始学习 PythonOOP专门用于 discord.py 之前你可以制作一个真正的机器人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM