首页 > 解决方案 > Python discord bot如何将消息与列表进行比较

问题描述

我正在制作一个 python Discord Bot,现在我正试图让他响应列表中的某个消息,但是存在一些问题,因为他只在消息开始文本时才响应(而不是在中间或结尾) . 所以我想弄清楚如何让他比较所有文本并与消息列表匹配,发送文本答案。

Python 3.8.2

代码:

import discord
from discord.ext        import commands
from discord.ext.commands   import Bot
import asyncio

bot = commands.Bot(command_prefix = "$")
phrases = ["QWACK","KWAK","AARK","KWAAAK"]

@bot.event
async def on_ready():
    print ("I'm ready!")

@bot.event
async def on_message(message):
    if str(phrases) in message.content:                                                    
        await message.channel.send("dhbang")

标签: pythonpython-requestsbotsdiscorddiscord.py

解决方案


我很惊讶机器人的反应xD

str(phrases) 是一个字符串,这个特定的字符串:"["QWACK","KWAK","AARK","KWAAAK"]"

您应该遍历每个单词,并检查该单词是否在消息中。

您还应该考虑在一封邮件中出现的大小写和多个关键字。


推荐阅读