Last active
October 4, 2021 13:30
-
-
Save izxxr/d07dc328ab34579c7344a92beb071b0f to your computer and use it in GitHub Desktop.
Guess the number game using Diskord Python library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Guess the number game using Diskord Python library. | |
This game uses slash commands. | |
""" | |
import diskord | |
import random | |
from diskord.ext import commands | |
# command_prefix is just a placeholder here it won't be used. | |
# Slash commands don't need member intents for member mentions so we | |
# don't need that data. | |
bot = commands.Bot(command_prefix='$') | |
@bot.slash_command() | |
# grange option is registered here using slash_option decorator. | |
@diskord.slash_option('grange', description='The range of guess. Can not be smaller then 5 or larger then 50. Defaults to 20.') | |
async def guess(ctx, grange: int = 20): # grange takes an integer, is optional and defaults to 20 | |
if grange < 5 or grange > 20: | |
return await ctx.send(f'Guess range must not be less then 5 or larger then 20. "{grange}" was provided.') | |
number: int = random.randint(5, grange) | |
tries: int = 0 | |
playing: bool = True | |
check = lambda m: all(m.author == ctx.author, m.content.isdigit(), m.channel == ctx.channel) | |
# the interaction is being responded here. | |
await ctx.send(f'Guess the number between 5 and {grange}') | |
while playing: | |
guess = await bot.wait_for('message', check=check) | |
guess = int(guess.content) | |
if guess > grange: | |
# the guessed number is larger then the actual number. | |
await ctx.interaction.followup.send('High!') | |
tries += 1 | |
elif guess < grange: | |
# the guessed number is smaller then the actual number. | |
await ctx.interaction.followup.send('Low!') | |
tries += 1 | |
else: | |
# in this case, user has guessed the number so we will tell the user that he won and end | |
# function. | |
await ctx.interaction.followup.send('You won!') | |
playing = False | |
# check if user has tried 5 or more times, if tries limit is reached, User loses and function ends. | |
if tries >= 5: | |
await ctx.interaction.followup.send(content='You lost! The number was %s' % str(grange)) | |
playing = False | |
# run the bot | |
bot.run('token') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment