So I made this command where it would give a random generated image of a pokemon and then the person who runs the command gotta guess what’s the pokemon’s name. he needs to give the answer on the channel he runs the command previously. the problem is, even when the answer is correct it’d still response with "the answer is incorrect". so how do I fix this? can someone help me please?
@client.command()
async def rp(ctx):
ranpoke = random.randint(1, 905)
url = "https://pokeapi.co/api/v2/pokemon/"
secs = 5
try:
r = requests.get(f"{url}{ranpoke}")
packages_json = r.json()
packages_json.keys
napo = packages_json["name"]
embed = discord.Embed(title = "Who's that pokemon?!", color = discord.Color.random())
embed.set_image(url = f"https://play.pokemonshowdown.com/sprites/ani/{napo}.gif")
await ctx.send(embed = embed)
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
user_ans = await client.wait_for("message", check=check)
if user_ans == napo:
await ctx.send("You are correct! :)")
elif user_ans != napo:
await ctx.send(f"You are incorrect :vnThe answer is {napo}")
except:
await ctx.send("bruh")
I’m expecting it to like say the answer is correct when it’s correct, and say that the answer is incorrect when it’s incorrect.
4
Answers
I’ve tried the first part of the code and the request seems to work fine, so the problem is either when you get the message or when you compare it. I see a case when it can fail even with a "correct" answer, comparing strings on python is case-sensitive, to avoid that you can use the string method
.lower()
before comparing the answer.If this doesn’t solve the problem, maybe the issue is located when you catch the user message, try doing a
print(user_ans)
to see in the console if you are catching the information properly.if
napo = packages_json["name"]
is a list of names then comparing it using == and != is wrong on lists and you may use something like this :Also be careful, if the JSON name contains lower/upper case and the user submission is not the same, the function returns false.
so you may use .lower() on user submission and list of names before comparing them.
user_ans == napo
user_ans
is a discord.Message object, whilenapo
is a string. When you compare these two objects using the == operator, it will always return False, becauseuser_ans
andnapo
are not the same object.To fix the issue, you can compare the content of the message (i.e., the text that the user typed) to napo. To do this, you can use the
.content
attribute of thediscord.Message
object:if user_ans.content == napo:
This will compare the text of the message (
user_ans.content
) to the stringnapo
, and return True if they are equal, and False otherwise.well… i saw it and you mistook in a thing there, actually you need to see the content there,
user_ans.content
also try to check them in.lowercase()
so it will be like this:If you want to make your command wait only for a specific time/ or until correct answer then you can try:
Here your bot will wait for 10 seconds and the command user have 3 attempts to answer correctly… If they fail then it will send correct answer