我正在制作一个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")
我很惊讶机器人对所有xD都做出了回应
str(phrases)是一个字符串,这个特定的字符串:“ [” QUACK“,” KWAK“,” AARK“,” KWAK“]”
您应该遍历每个单词,并检查该单词是否在消息中。
您还应考虑大写字母和在一条消息中出现多个关键字。
As wrong1man said,
str(phrases)
is a string, so it should only respond to messages likeLovely day, isn't it ["QWACK","KWAK","AARK","KWAAAK"]?
. I would just like to extend wrong1man's answer by giving some code:The above is the more concise version using a generator expression. If you are a Python begginer, you may be more comfortable with a normal for loop: