From 8666c835656b92703a866c7e53e5523c20849842 Mon Sep 17 00:00:00 2001 From: WhatDidYouExpect <89535984+WhatDidYouExpect@users.noreply.github.com> Date: Fri, 18 Jul 2025 13:57:58 +0200 Subject: [PATCH] prolly gonna move command to their own modules soon soo --- bot.py | 16 +++++----- modules/globalvars.py | 16 ++++++++-- modules/minigames.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 modules/minigames.py diff --git a/bot.py b/bot.py index 38a01b4..3af7e5e 100644 --- a/bot.py +++ b/bot.py @@ -49,7 +49,7 @@ from modules.version import * from modules.sentenceprocessing import * from modules.unhandledexception import handle_exception from modules.image import gen_meme, gen_demotivator - +from modules.minigames import guessthenumber, hangman sys.excepthook = handle_exception check_for_update() # Check for updates (from modules/version.py) @@ -64,12 +64,6 @@ currenthash: str = "" launched: bool = False slash_commands_enabled: bool = False -# Set up Discord bot intents and create bot instance -intents: discord.Intents = discord.Intents.default() -intents.messages = True -intents.message_content = True -bot: commands.Bot = commands.Bot(command_prefix=PREFIX, intents=intents, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False, replied_user=True)) - # Load memory and Markov model for text generation memory: List[str] = load_memory() markov_model: Optional[markovify.Text] = load_markov_model() @@ -419,10 +413,14 @@ async def on_message(message: discord.Message) -> None: await bot.process_commands(message) -# Event: Called on every interaction (slash command, etc.) @bot.event async def on_interaction(interaction: discord.Interaction) -> None: - logger.info(f"{(_('command_ran_s')).format(interaction=interaction)}{interaction.data['name']}") + name = None + if interaction.data.get('name') is None: + name = "Unknown" + else: + name = interaction.data['name'] + logger.info(f"{(_('command_ran_s')).format(interaction=interaction)}{name}") # Global check: Block blacklisted users from running commands @bot.check diff --git a/modules/globalvars.py b/modules/globalvars.py index 37b22e9..5c20f1d 100644 --- a/modules/globalvars.py +++ b/modules/globalvars.py @@ -2,6 +2,12 @@ import os import platform from dotenv import load_dotenv import pathlib +import discord +from discord.ext import commands +from discord import app_commands +from discord import Colour, Embed, File, Interaction, Message +from discord.abc import Messageable +from discord.ext import commands import subprocess def get_git_branch(): try: @@ -15,7 +21,6 @@ def get_git_branch(): env_path = pathlib.Path(__file__).parent.parent / '.env' load_dotenv(dotenv_path=env_path) - ANSI = "\033[" RED = f"{ANSI}31m" GREEN = f"{ANSI}32m" @@ -55,4 +60,11 @@ if get_git_branch() == "dev": beta = True # this makes goober think its a beta version, so it will not update to the latest stable version or run any version checks else: - beta = False \ No newline at end of file + beta = False + + +# Set up Discord bot intents and create bot instance +intents: discord.Intents = discord.Intents.default() +intents.messages = True +intents.message_content = True +bot: commands.Bot = commands.Bot(command_prefix=PREFIX, intents=intents, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False, replied_user=True)) \ No newline at end of file diff --git a/modules/minigames.py b/modules/minigames.py new file mode 100644 index 0000000..d7ef821 --- /dev/null +++ b/modules/minigames.py @@ -0,0 +1,70 @@ +import random +import discord +from discord import ui, Interaction, TextStyle +from discord.ext import commands +import aiohttp +import asyncio +from modules.globalvars import bot + +@bot.hybrid_command(description="Guess the number game") +async def guessthenumber(ctx: commands.Context): + number = random.randint(1, 10) + class GuessModal(ui.Modal, title="Guess the Number"): + guess = ui.TextInput(label="Your guess (1-10)", style=TextStyle.short) + async def on_submit(self, interaction: Interaction): + try: + user_guess = int(self.guess.value) + except: + await interaction.response.send_message("Invalid number!", ephemeral=True) + return + if user_guess == number: + await interaction.response.send_message("Correct!", ephemeral=True) + else: + await interaction.response.send_message(f"Wrong! The number was {number}.", ephemeral=True) + async def button_callback(interaction: Interaction): + await interaction.response.send_modal(GuessModal()) + button = ui.Button(label="Guess", style=discord.ButtonStyle.primary) + button.callback = button_callback + view = ui.View() + view.add_item(button) + await ctx.send("Click to guess a number from 1 to 10", view=view) + +@bot.hybrid_command(description="Play Hangman with a random word") +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]) + class GuessModal(ui.Modal, title="Guess a Letter"): + letter = ui.TextInput(label="Your 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: + await interaction.response.send_message(f"You 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): + await interaction.response.edit_message(content=f"You won! The word was: **{word}**", view=None) + elif wrong_guesses >= max_wrong: + await interaction.response.edit_message(content=f"You lost! The word was: **{word}**", view=None) + else: + await interaction.response.edit_message(content=f"Word: {display_word()}\nWrong guesses: {wrong_guesses}/{max_wrong}", view=view) + async def button_callback(interaction: Interaction): + await interaction.response.send_modal(GuessModal()) + button = ui.Button(label="Guess a letter", style=discord.ButtonStyle.primary) + button.callback = button_callback + view = ui.View() + view.add_item(button) + await ctx.send(f"Word: {display_word()}\nWrong guesses: {wrong_guesses}/{max_wrong}\nClick the button to guess a letter.", view=view) \ No newline at end of file