xpyct_bot/cogs/guessing_game.py

39 lines
1.3 KiB
Python
Raw Normal View History

2021-10-19 16:27:25 +02:00
import nextcord
from nextcord.ext import commands
import random
import asyncio
2021-10-19 16:55:59 +02:00
class guessing_game(commands.Cog):
2021-10-19 16:27:25 +02:00
def __init__(self, bot):
self.bot = bot
2021-10-19 17:28:42 +02:00
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f'Для использования команды повторно жди {round(error.retry_after, 2)} секунд')
2021-10-20 10:34:44 +02:00
2021-10-19 17:28:42 +02:00
@commands.command(aliases = ['guess', 'номера', 'угадайка'])
2021-10-20 10:34:44 +02:00
@commands.cooldown(1, 10, commands.BucketType.user)
2021-10-19 16:55:59 +02:00
async def guessing_game(self, ctx):
2021-10-20 10:34:44 +02:00
await ctx.send('Введи число от 1 до 10.')
2021-10-19 16:27:25 +02:00
def is_correct(m):
return m.author == ctx.author and m.content.isdigit()
answer = random.randint(1, 10)
try:
2021-10-19 16:55:59 +02:00
guess = await self.bot.wait_for('message', check=is_correct, timeout=5.0)
2021-10-19 16:27:25 +02:00
except asyncio.TimeoutError:
return await ctx.send(f'Извини, тебе потребовалось слишком много времени, чтобы это было {answer}.')
if int(guess.content) == answer:
await ctx.send('Ты угадал!')
else:
await ctx.send(f'Ты не угадал. Я загадывал {answer}.')
def setup(bot):
2021-10-19 16:55:59 +02:00
bot.add_cog(guessing_game(bot))