我在python中为Discord创建一个ChatBot。我正在创建包含问题和响应的字典,称为响应。每当用户键入问题时,都应给出适当的答复。但是我被卡住了,不知道如何打印响应。该解决方案可能非常简单,但是我感觉好像缺少了一些东西。请帮忙。预先感谢。
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = ":")
responses = {
"WHAT'S YOUR NAME?": "My name is ChatBot!"
}
@client.event
async def on_ready() :
print("I'm pretty much ready to talk...")
@client.event
async def on_message(message) :
# When you say cookie, the bot responds with a cookie emoji
if message.content.upper() == "COOKIE" :
await client.send_message(message.channel, ":cookie:")
# A simple question answerer
if message.content.upper() in responses:
await client.send_message(message.channel, responses[message])
client.("Don't worry about my token")
答案 0 :(得分:0)
查找消息内容,而不是消息对象本身。
responses = {
"WHAT'S YOUR NAME?": "My name is ChatBot!",
"COOKIE": ":cookie:"
}
@client.event
async def on_message(message) :
content = message.content.upper()
if content in responses:
await client.send_message(message.channel, responses[content])
答案 1 :(得分:0)
要访问字典中的值,您需要这样做
print(responses["WHAT'S YOUR NAME?"])
将返回分配给它的值,在您的情况下,My name is ChatBot!
将是答复。您可以按照自己认为合适的任何方式替换查询,例如,如果您的响应字典是这样
responses = {
"WHAT'S YOUR NAME?":"My name is ChatBot!,
"COOKIE":":cookie:"
}
您只需执行reponses["COOKIE"]
答案 2 :(得分:-1)
这就是我所做的:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('message-here'):
await message.channel.send('response-here')