enhanced-discord.py/examples/guessing_game.py
Rapptz 9d39b135f4 Modernize code to use f-strings
This also removes the encoding on the top, since Python 3 does it by
default. It also changes some methods to use `yield from`.
2021-04-04 07:03:53 -04:00

37 lines
1.1 KiB
Python

import discord
import random
import asyncio
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('$guess'):
await message.channel.send('Guess a number between 1 and 10.')
def is_correct(m):
return m.author == message.author and m.content.isdigit()
answer = random.randint(1, 10)
try:
guess = await self.wait_for('message', check=is_correct, timeout=5.0)
except asyncio.TimeoutError:
return await message.channel.send(f'Sorry, you took too long it was {answer}.')
if int(guess.content) == answer:
await message.channel.send('You are right!')
else:
await message.channel.send(f'Oops. It is actually {answer}.')
client = MyClient()
client.run('token')