检查是否有任何邮件内容与数组项匹配

时间:2019-06-11 20:06:11

标签: python arrays string discord.py

我正在编写一个小型的,开玩笑的Discord机器人,并决定将我的机器人将对它们做出相同响应的所有关键字集中到一个数组中,以节省时间。我试图弄清楚如何测试消息的内容是否与数组中的任何关键字匹配。

client = discord.Client()

keywords=["keyword1", "keyword2", "keyword3"]


@client.event                              ######################
async def on_message(message):             # stops bot from     #
    if message.author == client.user:      # replying to itself #
        return                             ######################

    if message.content.contains(keywords):
        msg = "Hello, {0.author.mention}!".format(message)
        await client.send_message(message.channel, msg)

我期望代码检查数组中是否有与数组关键字匹配的内容,但实际上我得到了以下TB:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Baguette\PycharmProjects\bot\venv\lib\site-packages\discord\client.py", line 270, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/Baguette/PycharmProjects/bot/main", line 17, in on_message
    if message.content.contains(keywords):
AttributeError: 'str' object has no attribute 'contains'

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

if any(keyword in message.content.lower() for keyword in keywords):
    ...  # respond accordingly here