I'm creating a ChatBot for Discord in python. I am creating a dictionary with questions and responses called responses. Whenever the user types in the question, it should give the appropriate response. But I got stuck and don't know how to print the response. The solution is probably pretty simple, but I feel like I'm missing something. Please help. Thanks in advance.
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")
Look up the message content, not the message object itself.
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])
To access the value in the dictionary you would need to do this
print(responses["WHAT'S YOUR NAME?"])
which would return the value assigned to that, in your case My name is ChatBot!
would be the reply. You could replace the query any way you see fit, for example if your responses dictionary was this
responses = {
"WHAT'S YOUR NAME?":"My name is ChatBot!,
"COOKIE":":cookie:"
}
You could access cookie by just doing reponses["COOKIE"]
This is what I do:
@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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.