Reputation: 11
I have a project (given to me by myself) of making a quiz bot. I love pokemon a lot so I am making on it. The pokedex is a library I made that has the names of every pokemon. I have included time for afterward. Anyways,
import random
import pokedex
import discord
import pandas as pd
import time
import os
client=discord.Client()
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
msg=message.content
if message.author==client.user:
return
if msg.startswith("-q jname"):
await message.channel.send("Unscramble these letters to create a name of a pokemon:")
s=random.choice(pokedex.poke)["name"]
await message.channel.send(''.join(random.sample(s,len(s))))
p=""
for i in s:
if i=="(":
p=p+i
elif i==")":
p=p+i
else:
p=p+"-"
await message.channel.send(p)
if(msg==s):
await message.channel.send("Correct Answer")
else:
await message.channel.send("Wrong! The correct Answer is:")
await message.channel.send(s)
client.run(os.getenv("Token"))
This is my code. Now apparently the error I am having is that the quiz bot thinks that the command is the answer to the question. Please tell me how could I fix this.
This is how the error looks: (bot repeating wrong answer msg, and taking the command as answer)
Upvotes: 0
Views: 151
Reputation: 438
Firstly building on Oblique's answer with bot.wait_for
, your code has the send functions within a for loop, and is sending a message every iteration.
I've attached my edited code below. Also it's good practise to use IDs, so if two channels (or users) have the same name it won't cause any errors
import random
import pokedex
import discord
import pandas as pd
import time
import os
client = discord.Client()
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
msg = message.content
if message.author.id == client.user.id:
return
if msg.startswith("-q jname"):
def check(check_message)
if message.author.id != check_message.author.id:
return False
return True
await message.channel.send("Unscramble these letters to create a name of a pokemon:")
pokemon = random.choice(pokedex.poke)["name"]
await message.channel.send(''.join(random.sample(pokemon, len(pokemon))))
try:
msg = await client.wait_for('message', check=check, timeout=30)
except:
pass
if msg.content.lower() == pokemon:
await message.channel.send("Correct Answer")
else:
await message.channel.send("Wrong! The correct Answer is:")
await message.channel.send(pokemon)
client.run(os.getenv("Token"))
Upvotes: 1
Reputation: 560
Have a look at discord.py rewrite | How to wait for author message?
The problem is, you are checking if the message starts with "-q jname", and then doing the check inside that command if the message is correct. Of course "-q jname" is incorrect, so you need to wait for the user to send another message, then use that message to do the check on.
import random
import pokedex
import discord
import pandas as pd
import time
import os
client=discord.Client()
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
msg=message.content
if message.author==client.user:
return
if msg.startswith("-q jname"):
def check(author)
if message.author != author:
return False
else:
return True
await message.channel.send("Unscramble these letters to create a name of a pokemon:")
s=random.choice(pokedex.poke)["name"]
await message.channel.send(''.join(random.sample(s,len(s))))
msg = await client.wait_for('message', check=check(message.author), timeout=30).content
p=""
for i in s:
if i=="(":
p=p+i
elif i==")":
p=p+i
else:
p=p+"-"
await message.channel.send(p)
if(msg==s):
await message.channel.send("Correct Answer")
else:
await message.channel.send("Wrong! The correct Answer is:")
await message.channel.send(s)
client.run(os.getenv("Token"))
Upvotes: 1