Reputation: 475
I am trying to make a command which activates a random number guesser game. Obviously, I am stuck in the first few lines. I have written what I THINK will work, however it may be blatantly wrong. I want it to change the message on the discord server to an int, so it will work in my if statement.
It's my first time making a bot in with discord.py, so I am running into many obstacles. I am not fully sure what the error is telling me, so I haven't been able to try any fixes. This is the code:
async def numgame(context):
number = random.randint(1,100)
for guess in range(0,5):
await context.send('Pick a number between 1 and 100')
Message = await client.wait_for('message')
Message = int(Message)
if Message.cleant_content > number:
await context.send(guess + ' guesses left...')
asyncio.sleep(1)
await context.send('Try going lower')
asyncio.sleep(1)
elif Message.clean_content < number:
await context.send(guess + ' guesses left...')
asyncio.sleep(1)
await context.send('Try going higher')
asyncio.sleep(1)
else:
await context.send('You guessed it! Good job!')
if number != Message:
await context.send('Tough luck!')
Whenever I do the command in my discord server, my shell gives me this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Message'
I am not too sure what it is telling me. As stated, I would like "Message" to be an integer, but I get the error. but help would be appreciated! [still a beginner, don't be too harsh :(]
Upvotes: 1
Views: 3075
Reputation: 61042
wait_for('message')
returns a Message
object, which int
doesn't know how to handle yet. You need to convert Message.content
to an int instead. Just below is your code with some other changes:
def check(message):
try:
int(message.content)
return True
except ValueError:
return False
@bot.command()
async def numgame(context):
number = random.randint(1,100)
for guess in range(0,5):
await context.send('Pick a number between 1 and 100')
msg = await client.wait_for('message', check=check)
attempt = int(msg.content)
if attempt > number:
await context.send(str(guess) + ' guesses left...')
await asyncio.sleep(1)
await context.send('Try going lower')
await asyncio.sleep(1)
elif attempt < number:
await context.send(str(guess) + ' guesses left...')
await asyncio.sleep(1)
await context.send('Try going higher')
await asyncio.sleep(1)
else:
await context.send('You guessed it! Good job!')
break
else:
await context.send("You didn't get it")
Upvotes: 1