goober/modules/minigames.py

71 lines
3.6 KiB
Python
Raw Permalink Normal View History

import random
import discord
from discord import ui, Interaction, TextStyle
from discord.ext import commands
import aiohttp
import asyncio
from modules.globalvars import bot
from modules.volta.main import _
2025-07-18 16:15:34 +02:00
# @bot.hybrid_command(description=_('minigames_guess_the_number'))
async def guessthenumber(ctx: commands.Context):
number = random.randint(1, 10)
2025-07-18 16:09:14 +02:00
class GuessModal(ui.Modal, title=_('minigames_guess_the_number')):
guess = ui.TextInput(label=_('minigames_your_guess'), style=TextStyle.short)
async def on_submit(self, interaction: Interaction):
try:
user_guess = int(self.guess.value)
except:
2025-07-18 16:09:14 +02:00
await interaction.response.send_message(_('minigames_invalid_number'), ephemeral=True)
return
if user_guess == number:
2025-07-18 16:09:14 +02:00
await interaction.response.send_message(_('minigames_correct'), ephemeral=True)
else:
2025-07-18 16:09:14 +02:00
await interaction.response.send_message(f"{_('minigames_wrong_number')} {number}.", ephemeral=True)
async def button_callback(interaction: Interaction):
await interaction.response.send_modal(GuessModal())
2025-07-18 16:09:14 +02:00
button = ui.Button(label=_('minigames_guess_button'), style=discord.ButtonStyle.primary)
button.callback = button_callback
view = ui.View()
view.add_item(button)
2025-07-18 16:09:14 +02:00
await ctx.send(_('minigames_click_to_guess'), view=view)
2025-07-18 16:15:34 +02:00
# @bot.hybrid_command(description=_('minigames_hangman')) nope nope nope fuck no nope no thanks no nuh uh not today nope
async def hangman(ctx: commands.Context):
async with aiohttp.ClientSession() as session:
async with session.get("https://random-word-api.herokuapp.com/word?number=1") as resp:
if resp.status != 200:
await ctx.send("Failed to get a random word.")
return
data = await resp.json()
word = data[0].lower()
print(word)
guessed_letters = set()
wrong_guesses = 0
max_wrong = 6
def display_word():
return " ".join([c if c in guessed_letters else "_" for c in word])
2025-07-18 16:09:14 +02:00
class GuessModal(ui.Modal, title=_('minigames_hangman_guess')):
letter = ui.TextInput(label=_('minigames_hangman_user_letter_guess'), style=TextStyle.short, max_length=1)
async def on_submit(self, interaction: Interaction):
nonlocal guessed_letters, wrong_guesses
guess = self.letter.value.lower()
if guess in guessed_letters:
2025-07-18 16:09:14 +02:00
await interaction.response.send_message(f"{_('minigames_hangman_already_guessed')}'{guess}'!", ephemeral=True)
return
guessed_letters.add(guess)
if guess not in word:
wrong_guesses += 1
if all(c in guessed_letters for c in word):
2025-07-18 16:09:14 +02:00
await interaction.response.edit_message(content=f"{_('minigames_hangman_won')} **{word}**", view=None)
elif wrong_guesses >= max_wrong:
2025-07-18 16:09:14 +02:00
await interaction.response.edit_message(content=f"{_('minigames_hangman_lost')} **{word}**", view=None)
else:
2025-07-18 16:15:34 +02:00
await interaction.response.edit_message(content=_('minigames_hangman_game').format(display_word=display_word(),wrong_guesses=wrong_guesses,max_wrong=max_wrong), view=view)
async def button_callback(interaction: Interaction):
await interaction.response.send_modal(GuessModal())
2025-07-18 16:15:34 +02:00
button = ui.Button(label=_('minigames_click_to_guess'), style=discord.ButtonStyle.primary)
button.callback = button_callback
view = ui.View()
view.add_item(button)
2025-07-18 16:15:34 +02:00
await ctx.send(_('minigames_hangman_game').format(display_word=display_word,wrong_guesses=wrong_guesses,max_wrong=max_wrong), view=view)