From e28050a9e268c2cb91caf723c820ba7965610a8d Mon Sep 17 00:00:00 2001 From: Aesthetic Date: Thu, 9 Jul 2026 12:11:15 -0700 Subject: [PATCH] refactor: move dice commands into seperate cog --- cogs/dice.py | 36 ++++++++++++++++++++++++++++++++++++ cogs/fun.py | 33 --------------------------------- main.py | 10 ++++++---- 3 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 cogs/dice.py diff --git a/cogs/dice.py b/cogs/dice.py new file mode 100644 index 0000000..42083a1 --- /dev/null +++ b/cogs/dice.py @@ -0,0 +1,36 @@ +import random +import discohook +import time + +@discohook.command.slash('dice_old', description = 'Roll a dice!') +async def old_dice_command(interaction): + # Dice! + number = random.randint(1, 6) + text = "You rolled a...\n-# (Wait 1 second)" + await interaction.response.send(text) + + # anticipation + time.sleep(1) + text = f"You rolled a {number}!" + await interaction.response.followup(text) + + +@discohook.command.slash('dice', description="roll a dice using Discord's roll a dice system.", options=[ + discohook.Option.integer('dice_num', "The number of dice", required=True, max_value=10, min_value=1), + discohook.Option.integer('dice_sides', "The number of sides on the dice", required=True, choices=[ + discohook.Choice("D4", 4), discohook.Choice("D6", 6), discohook.Choice("D8", 8), + discohook.Choice("D10", 10), discohook.Choice("D12", 12), discohook.Choice("D20", 20) + ] + )], integration_types=[discohook.ApplicationIntegrationType.guild, discohook.ApplicationIntegrationType.user], + contexts=[discohook.InteractionContextType.guild, discohook.InteractionContextType.bot_dm, discohook.InteractionContextType.private_channel] +) +async def dice_command(interaction: discohook.Interaction, dice_num, dice_sides): + channel_id = interaction.channel_id + guild_id = interaction.guild_id + if not guild_id: + guild_id = "@me" + link = f"https://discord.com/channels/{guild_id}/{channel_id}/roll-dice/{dice_num}d{dice_sides}" + + view = discohook.View() + view.add_buttons(discohook.Button("Roll", url=link, style=discohook.ButtonStyle.link)) + await interaction.response.send(f"Click the button to roll.", view=view) diff --git a/cogs/fun.py b/cogs/fun.py index 08b55e1..796e969 100644 --- a/cogs/fun.py +++ b/cogs/fun.py @@ -4,21 +4,9 @@ This is a fun cog, it contains commands that are just for fun and don't have any import random import discohook -import time import aiohttp import json -@discohook.command.slash('dice_old', description = 'Roll a dice!') -async def old_dice_command(interaction): - # Dice! - number = random.randint(1, 6) - text = "You rolled a...\n-# (Wait 1 second)" - await interaction.response.send(text) - - # anticipation - time.sleep(1) - text = f"You rolled a {number}!" - await interaction.response.followup(text) @discohook.command.slash('decide', description = 'Answer any yes or no question with this helpful command!') async def decide_command(interaction): @@ -43,24 +31,3 @@ async def randomgif_cmd(interaction): with open("data/gifs.json", "rt") as data: gifs = json.load(data) await interaction.response.followup(random.choice(gifs) + "\n **heres ur tuff gif**") - -@discohook.command.slash('dice', description="roll a dice using Discord's roll a dice system.", options=[ - discohook.Option.integer('dice_num', "The number of dice", required=True, max_value=10, min_value=1), - discohook.Option.integer('dice_sides', "The number of sides on the dice", required=True, choices=[ - discohook.Choice("D4", 4), discohook.Choice("D6", 6), discohook.Choice("D8", 8), - discohook.Choice("D10", 10), discohook.Choice("D12", 12), discohook.Choice("D20", 20) - ] - )], integration_types=[discohook.ApplicationIntegrationType.guild, discohook.ApplicationIntegrationType.user], - contexts=[discohook.InteractionContextType.guild, discohook.InteractionContextType.bot_dm, discohook.InteractionContextType.private_channel] -) -async def dice_command(interaction: discohook.Interaction, dice_num, dice_sides): - channel_id = interaction.channel_id - guild_id = interaction.guild_id - if not guild_id: - guild_id = "@me" - link = f"https://discord.com/channels/{guild_id}/{channel_id}/roll-dice/{dice_num}d{dice_sides}" - - view = discohook.View() - view.add_buttons(discohook.Button("Roll", url=link, style=discohook.ButtonStyle.link)) - await interaction.response.send(f"Click the button to roll.", view=view) - diff --git a/main.py b/main.py index 7e30ff3..610f3f4 100644 --- a/main.py +++ b/main.py @@ -8,8 +8,9 @@ import aiohttp import discohook from starlette.responses import JSONResponse from cogs.utils import ping_command, timestamp_command, info_command -from cogs.fun import dice_command, decide_command, funfact_cmd, randomgif_cmd, old_dice_command +from cogs.fun import decide_command, funfact_cmd, randomgif_cmd from cogs.games import rps_command, gtn_command +from cogs.dice import dice_command, old_dice_command def run(): @contextlib.asynccontextmanager @@ -48,14 +49,15 @@ def run(): timestamp_command, info_command, ### --- Cog: Fun --- ### - dice_command, decide_command, funfact_cmd, randomgif_cmd, - old_dice_command, ### --- Cog: Games --- ### rps_command, - gtn_command + gtn_command, + ### --- Cog: Dice --- ### + dice_command, + old_dice_command, ) return app -- 2.51.2