skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 :

    if user_ans in napo :
       await ctx.send("You are correct! :)")
    else: 
       await ctx.send(f"You are incorrect :vnThe answer is {napo}")
    

    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.

    Login or Signup to reply.
  3. user_ans == napo

    user_ans is a discord.Message object, while napo is a string. When you compare these two objects using the == operator, it will always return False, because user_ans and napo 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 the discord.Message object:

    if user_ans.content == napo:

    This will compare the text of the message (user_ans.content) to the string napo, and return True if they are equal, and False otherwise.

    Login or Signup to reply.
  4. 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 user_ans.content.lower() == napo.lower():
          await ctx.send("You are correct! :)")
    else:
          await ctx.send(f"You are incorrect :vnThe answer is {napo}")
    

    If you want to make your command wait only for a specific time/ or until correct answer then you can try:

    def check(msg):
       return msg.author == ctx.author and msg.channel == ctx.channel
    for i in range(3):
     try:
      user_ans = await client.wait_for("message", check=check, timeout=10)
      if user_ans.content.lower() == napo.lower():
          await ctx.send("You are correct! :)")
      else:
          await ctx.send(f"You are incorrect!")
     except asyncio.TimeoutError:
        await ctx.send(f"Opps, you ran out of time... the correct answer was {napo}")
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search