discord.py wait_for check isnt working properly
Question:
I’m trying to make something that asks the user a question and checks if the answer is correct. But when I come to check if the answer = the recorded answer it shows False, although I checked that they are equal.
So the bot does not congratulate the user for saying the correct answer, it just ignores the answer. This is what happens:
This is my code:
@commands.Cog.listener()
async def on_message(self, message):
if message.channel.id == 1051856493764415568:
if message.author.bot:
return
else:
rand_num = randint(4,6)
if rand_num == 5:
ss = open('scrambled sentences.txt','r')
sslist = ss.readlines()
chosen_sentence = choice(sslist)
cs_list = chosen_sentence.split("|")
question_msg = await message.reply(f"Unscramble the following sentence in 10 seconds for a chance to win a firework:nn{cs_list[0]}")
def check(m: discord.Message):
print(m.content)
print(cs_list[-1])
#When I tried printing these two they are identical
return m.channel.id == m.channel.id and m.content.lower == cs_list[-1]
try:
msg = await self.bot.wait_for('message', check = check, timeout = 10.0)
except asyncio.TimeoutError:
await question_msg.reply(f"Noone sent the correct answer in the 10 seconds provided...")
return
else:
await msg.channel.send(f"**{msg.author}**, you responded with {msg.content}!")
return
#await message.channel.send(f"{msg.author.mention} was first! He gets one firework. The answer was:nn{cs_list[-1]}")
This is scrambledsentences.txt
:
Question) `RsgaD si het btse reve!`|drags is the best ever!
Question) `RsgaD si het torws reve!`|drags is the worst ever!
Answers:
They are not equal. str.lower
is a function & you’re not calling it.
... and m.content.lower == cs_list[-1]
This is always False
, because a function will not be equal to a string, so your check always fails.
>>> "TEST".lower == "test"
False
>>> "TEST".lower
<built-in method lower of str object at 0x7f8110ec7ab0>
You can solve it by… well, calling the function.
m.content.lower() == cs_list[-1]
You said you printed them and they were equal, yet you’re printing m.content
instead of m.content.lower
. If you’re trying to debug-print to check if two things are equal, you should print those very things to see that they aren’t. Printing something different isn’t useful so you might as well not print anything at all then.
# Print the first, the second, and the boolean that comes out of it
# If that isn't True then they aren't equal
print(m.content.lower, cs_list[-1], m.content.lower == cs_list[-1])
This would instantly show you what you’re doing wrong.
I’m trying to make something that asks the user a question and checks if the answer is correct. But when I come to check if the answer = the recorded answer it shows False, although I checked that they are equal.
So the bot does not congratulate the user for saying the correct answer, it just ignores the answer. This is what happens:
This is my code:
@commands.Cog.listener()
async def on_message(self, message):
if message.channel.id == 1051856493764415568:
if message.author.bot:
return
else:
rand_num = randint(4,6)
if rand_num == 5:
ss = open('scrambled sentences.txt','r')
sslist = ss.readlines()
chosen_sentence = choice(sslist)
cs_list = chosen_sentence.split("|")
question_msg = await message.reply(f"Unscramble the following sentence in 10 seconds for a chance to win a firework:nn{cs_list[0]}")
def check(m: discord.Message):
print(m.content)
print(cs_list[-1])
#When I tried printing these two they are identical
return m.channel.id == m.channel.id and m.content.lower == cs_list[-1]
try:
msg = await self.bot.wait_for('message', check = check, timeout = 10.0)
except asyncio.TimeoutError:
await question_msg.reply(f"Noone sent the correct answer in the 10 seconds provided...")
return
else:
await msg.channel.send(f"**{msg.author}**, you responded with {msg.content}!")
return
#await message.channel.send(f"{msg.author.mention} was first! He gets one firework. The answer was:nn{cs_list[-1]}")
This is scrambledsentences.txt
:
Question) `RsgaD si het btse reve!`|drags is the best ever!
Question) `RsgaD si het torws reve!`|drags is the worst ever!
They are not equal. str.lower
is a function & you’re not calling it.
... and m.content.lower == cs_list[-1]
This is always False
, because a function will not be equal to a string, so your check always fails.
>>> "TEST".lower == "test"
False
>>> "TEST".lower
<built-in method lower of str object at 0x7f8110ec7ab0>
You can solve it by… well, calling the function.
m.content.lower() == cs_list[-1]
You said you printed them and they were equal, yet you’re printing m.content
instead of m.content.lower
. If you’re trying to debug-print to check if two things are equal, you should print those very things to see that they aren’t. Printing something different isn’t useful so you might as well not print anything at all then.
# Print the first, the second, and the boolean that comes out of it
# If that isn't True then they aren't equal
print(m.content.lower, cs_list[-1], m.content.lower == cs_list[-1])
This would instantly show you what you’re doing wrong.