Home > database >  Random pokemon generator guessing game on discord bot with python
Random pokemon generator guessing game on discord bot with python

Time:12-16

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 :v\nThe 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.

CodePudding user response:

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.

CodePudding user response:

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 :v\nThe 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.

CodePudding user response:

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.

  • Related