0
votes

Im trying to make a discord bot guessing game (using python) but it keeps failing. I have looked at many similar posts and haven't found anything that helps.

@bot.command(name='play')
async def play(ctx):
  number = random.randint(1,100)
  await ctx.send('I have a number in mind between 1 and 100, guess')
  for i in range(0,5):
    guess = await client.wait_for('message') **(see below the code)
        if guess.content == number:
          await ctx.send('You got it!')
        elif guess.content < number:
          await ctx.send('Higher!')
        elif guess.content > number:
          await ctx.send('Lower!')
      else:
        await ctx.send("You lost, type $play to play again.")

** normaly it would say: , check=check, but that doesnt seem to work, it says, check not defined or something like that

the code seems to get stuck at either: for i in range(0,5):, or: guess = await client.wait_for('message')

could someone help?, or post a guessing game that works + explanation?

2
check=check assumes that you have defined check before. You can check if the message comes from the author for example. - Dominik

2 Answers

1
votes

As mentioned in the comments, you need to include your own check. This looks like the following:

    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel

This ensures that only the message from the author of the command is recognized and accepted. We include this in the code.

Also, you can't use </> number, you will get the following error:

TypeError: '<' not supported between instances of 'str' and 'int'.

To fix this, simply change number to a str:

</> str(number)

A possible code would be:

@bot.command(name='play')
async def play(ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel

    number = random.randint(1, 100)
    await ctx.send('I have a number in mind between 1 and 100, guess')

    for i in range(0, 5):
        guess = await bot.wait_for('message', check=check)

        if guess.content == number:
            await ctx.send('You got it!')

        elif guess.content < str(number):
            await ctx.send('Higher!')

        elif guess.content > str(number):
            await ctx.send('Lower!')

        else:
            return # Or something else
        
    else:
        await ctx.send("You lost, type $play to play again.")
0
votes

I thought you'd like to know, after two days (few hours a day) I got it to work (almost) flawlesly, sometimes it will give wrong hints but I can't fix that. Heres the code( I ended up using the @bot.event):

if msg.startswith('$play'):
    number = random.randint(1,100)
    await message.channel.send('I have a number in mind between 1 and 100, you have 5 guesses, guess.')
    for guesses in range(0,5):
      guess = await bot.wait_for('message')
      print(number)
      if guess.content < str(number):
        if guesses == 4:
          await message.channel.send("Too many guesses, try again.")
          await message.channel.send("The number I was thinking of was " + str(number) + ".")
          break
        else:
          await message.channel.send("Higher!")
      elif guess.content > str(number):
        if guesses == 4:
          await message.channel.send("Too many guesses, try again.")
          await message.channel.send("The number I was thinking of was " + str(number) + ".")
          break
        else:
          await message.channel.send("Lower!")
      else:
        await message.channel.send("You got it! You guessed " + str(guesses+1) + " times.")
        break