Reputation: 25
I am currently trying to create a quiz bot in discord.py. I'm trying to find a way to have my bot read multiple values i've assigned to a single function, to serve as multiple answers to one question. This is my code:
@client.command()
async def ask(ctx):
_list = [
'question_1',
'question_2']
list1 = random.choice(_list)
answer = "default"
hint = "default1"
if _list[0] == list1:
answer = "1"
answer = "One"
hint = "one"
else:
answer = "2"
answer = "Two"
hint = "two"
await ctx.send("What is the answer to this question?")
await asyncio.sleep(1)
await ctx.send(list1)
def check(m): return m.author == ctx.author and m.channel == ctx.channel
msg = await client.wait_for('message', check=check, timeout=None)
if (answer in msg.content):
await ctx.send("good")
else:
await ctx.send("What is the answer to this question? hint:" + hint)
await ctx.send(list1)
def check(m): return m.author == ctx.author and m.channel == ctx.channel
msg = await client.wait_for('message', check=check, timeout=None)
if (answer in msg.content):
await ctx.send("good")
else:
await ctx.send("wrong.")
This is the concept i'm currently using. At the moment, the bot only accepts the second value given (One
and Two
). I have tried different ways as well but I haven't found anything that worked properly. I hope I was able to explain everything clearly but feel free to ask any questions. Note that the hints are working properly and the code does not go through any errors. If possible, I also would want to know if I can make the bot case insensitive to the answers and variables.
Sorry if the answer is supposed to be obvious and I haven't done enough reading, or if this is impossible to pull off. Thank you so much!
Upvotes: 0
Views: 904
Reputation: 71434
If you reassign a different value to an existing variable, you replace the original value. What you want instead is to use a list so you can go over all the answers in the list and see if any of them match what the user said.
Using a dict to handle the mapping of questions to answers/hints will also make this easier to expand as you add more questions.
@client.command()
async def ask(ctx):
quiz_data = {
'question_1': (["1", "One"], "one"),
'question_2': (["2", "Two"], "two"),
}
question = random.choice(list(quiz_data.keys()))
answers, hint = quiz_data[question]
await ctx.send("What is the answer to this question?")
await asyncio.sleep(1)
await ctx.send(question)
def check_sender(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
def check_answer(msg):
return any(answer in msg.content for answer in answers)
msg = await client.wait_for('message', check=check_sender, timeout=None)
if check_answer(msg):
await ctx.send("good")
else:
await ctx.send(f"What is the answer to this question? hint: {hint}")
await ctx.send(question)
msg = await client.wait_for('message', check=check_sender, timeout=None)
if check_answer(msg):
await ctx.send("good")
else:
await ctx.send("wrong.")
Upvotes: 1