refactor(fun): cogs into one group

Combined all fun commands into a single 'fun' GroupCog with subcommands, replacing individual cogs for coinflip, eightball, minesweeper, randomfact, and rockpaperscissors. Updated bot.py to load the fun cog as a package and adjusted help.py to reflect the new command structure. This improves organization and discoverability of fun commands.
This commit is contained in:
neoarz
2025-09-28 22:48:10 -04:00
parent 6ed3b0d378
commit e7ee97074b
8 changed files with 114 additions and 93 deletions

42
bot.py
View File

@@ -86,21 +86,33 @@ class DiscordBot(commands.Bot):
for folder in os.listdir(cogs_path):
folder_path = os.path.join(cogs_path, folder)
if os.path.isdir(folder_path) and not folder.startswith('__'):
for file in os.listdir(folder_path):
if file.endswith(".py") and not file.startswith('__'):
extension = file[:-3]
full_name = f"{folder}.{extension}".lower()
if extension.lower() in disabled_cogs or full_name in disabled_cogs:
self.logger.info(f"Skipped disabled extension '{full_name}'")
continue
try:
await self.load_extension(f"cogs.{folder}.{extension}")
self.logger.info(f"Loaded extension '{folder}.{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
self.logger.error(
f"Failed to load extension {folder}.{extension}\n{exception}"
)
init_file = os.path.join(folder_path, "__init__.py")
if os.path.exists(init_file):
try:
await self.load_extension(f"cogs.{folder}")
if folder != "fun":
self.logger.info(f"Loaded extension '{folder}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
self.logger.error(
f"Failed to load extension {folder}\n{exception}"
)
else:
for file in os.listdir(folder_path):
if file.endswith(".py") and not file.startswith('__'):
extension = file[:-3]
full_name = f"{folder}.{extension}".lower()
if extension.lower() in disabled_cogs or full_name in disabled_cogs:
self.logger.info(f"Skipped disabled extension '{full_name}'")
continue
try:
await self.load_extension(f"cogs.{folder}.{extension}")
self.logger.info(f"Loaded extension '{folder}.{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
self.logger.error(
f"Failed to load extension {folder}.{extension}\n{exception}"
)
for file in os.listdir(cogs_path):
if file.endswith(".py") and not file.startswith('__'):

55
cogs/fun/__init__.py Normal file
View File

@@ -0,0 +1,55 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context
from .coinflip import coinflip_command
from .eightball import eightball_command
from .minesweeper import minesweeper_command
from .randomfact import randomfact_command
from .rockpaperscissors import rps_command
class Fun(commands.GroupCog, name="fun"):
def __init__(self, bot) -> None:
self.bot = bot
super().__init__()
@commands.hybrid_command(
name="coinflip",
description="Make a coin flip, but give your bet before."
)
async def coinflip(self, context):
return await coinflip_command()(self, context)
@commands.hybrid_command(
name="8ball",
description="Ask any question to the bot.",
)
async def eight_ball(self, context, *, question: str):
return await eightball_command()(self, context, question=question)
@commands.hybrid_command(
name="minesweeper",
description="Play a buttoned minesweeper mini-game."
)
async def minesweeper(self, context):
return await minesweeper_command()(self, context)
@commands.hybrid_command(name="randomfact", description="Get a random fact.")
async def randomfact(self, context):
return await randomfact_command()(self, context)
@commands.hybrid_command(
name="rps", description="Play the rock paper scissors game against the bot."
)
async def rock_paper_scissors(self, context):
return await rps_command()(self, context)
async def setup(bot) -> None:
cog = Fun(bot)
await bot.add_cog(cog)
bot.logger.info("Loaded extension 'fun.coinflip'")
bot.logger.info("Loaded extension 'fun.8ball'")
bot.logger.info("Loaded extension 'fun.minesweeper'")
bot.logger.info("Loaded extension 'fun.randomfact'")
bot.logger.info("Loaded extension 'fun.rps'")

View File

@@ -1,7 +1,6 @@
import random
import discord
from discord.ext import commands
from discord.ext.commands import Context
class Choice(discord.ui.View):
def __init__(self) -> None:
@@ -22,18 +21,12 @@ class Choice(discord.ui.View):
self.value = "tails"
self.stop()
class CoinFlip(commands.Cog, name="coinflip"):
def __init__(self, bot) -> None:
self.bot = bot
def coinflip_command():
@commands.hybrid_command(
name="coinflip", description="Make a coin flip, but give your bet before."
name="coinflip",
description="Make a coin flip, but give your bet before."
)
async def coinflip(self, context: Context) -> None:
"""
Make a coin flip, but give your bet before.
:param context: The hybrid command context.
"""
async def coinflip(self, context):
buttons = Choice()
embed = discord.Embed(
title="Coinflip",
@@ -60,5 +53,4 @@ class CoinFlip(commands.Cog, name="coinflip"):
embed.set_author(name="Fun", icon_url="https://yes.nighty.works/raw/eW5lLm.webp")
await message.edit(embed=embed, view=None, content=None)
async def setup(bot) -> None:
await bot.add_cog(CoinFlip(bot))
return coinflip

View File

@@ -1,26 +1,12 @@
import random
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
class EightBall(commands.Cog, name="8ball"):
def __init__(self, bot) -> None:
self.bot = bot
def eightball_command():
@commands.hybrid_command(
name="8ball",
description="Ask any question to the bot.",
)
@app_commands.describe(question="The question you want to ask.")
async def eight_ball(self, context: Context, *, question: str) -> None:
"""
Ask any question to the bot.
:param context: The hybrid command context.
:param question: The question that should be asked by the user.
"""
async def eight_ball(self, context, *, question: str):
answers = [
"It is certain.",
"It is decidedly so.",
@@ -52,6 +38,4 @@ class EightBall(commands.Cog, name="8ball"):
embed.set_footer(text=f"The question was: {question}")
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(EightBall(bot))
return eight_ball

View File

@@ -2,8 +2,6 @@ import random
from itertools import repeat
import discord
from discord.ext import commands
from discord.ext.commands import Context
class RowButton(discord.ui.Button):
def __init__(self, ctx, label, custom_id, bombs, board):
@@ -66,7 +64,6 @@ class RowButton(discord.ui.Button):
await interaction.edit_original_response(view=view)
class MsView(discord.ui.View):
def __init__(self, ctx, options, bombs, board):
super().__init__()
@@ -155,21 +152,13 @@ class MsView(discord.ui.View):
self.GetBoardPos(pos)
] = bombemo
class Minesweeper(commands.Cog, name="minesweeper"):
def __init__(self, bot) -> None:
self.bot = bot
def minesweeper_command():
@commands.hybrid_command(
name="minesweeper",
description="Play a buttoned minesweeper mini-game."
)
async def minesweeper(self, context: Context) -> None:
"""
Play a buttoned minesweeper mini-game.
:param context: The hybrid command context.
"""
board = [[" "] * 5 for _ in range(5)] # Unicode block character, usually doesnt show up in Discord or github, search up invisible character on google
async def minesweeper(self, context):
board = [[" "] * 5 for _ in range(5)]
bombs = 0
bombpositions = []
for x in repeat(None, random.randint(4, 11)):
@@ -198,6 +187,4 @@ class Minesweeper(commands.Cog, name="minesweeper"):
message = await context.send(embed=embed, view=view)
view.message = message
async def setup(bot) -> None:
await bot.add_cog(Minesweeper(bot))
return minesweeper

View File

@@ -1,15 +1,10 @@
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import Context
class RandomFact(commands.Cog, name="randomfact"):
def __init__(self, bot) -> None:
self.bot = bot
def randomfact_command():
@commands.hybrid_command(name="randomfact", description="Get a random fact.")
async def randomfact(self, context: Context) -> None:
async def randomfact(self, context):
async with aiohttp.ClientSession() as session:
async with session.get(
"https://uselessfacts.jsph.pl/random.json?language=en"
@@ -31,6 +26,4 @@ class RandomFact(commands.Cog, name="randomfact"):
embed.set_author(name="Fun", icon_url="https://yes.nighty.works/raw/eW5lLm.webp")
await context.send(embed=embed)
async def setup(bot) -> None:
await bot.add_cog(RandomFact(bot))
return randomfact

View File

@@ -1,7 +1,6 @@
import random
import discord
from discord.ext import commands
from discord.ext.commands import Context
class RockPaperScissors(discord.ui.Select):
def __init__(self) -> None:
@@ -39,7 +38,6 @@ class RockPaperScissors(discord.ui.Select):
winner = (3 + user_choice_index - bot_choice_index) % 3
# Get the user mention
user_mention = interaction.user.mention
if winner == 0:
@@ -61,14 +59,11 @@ class RockPaperScissorsView(discord.ui.View):
super().__init__()
self.add_item(RockPaperScissors())
class RPS(commands.Cog, name="rps"):
def __init__(self, bot) -> None:
self.bot = bot
def rps_command():
@commands.hybrid_command(
name="rps", description="Play the rock paper scissors game against the bot."
)
async def rock_paper_scissors(self, context: Context) -> None:
async def rock_paper_scissors(self, context):
view = RockPaperScissorsView()
embed = discord.Embed(
title="Rock Paper Scissors",
@@ -78,5 +73,4 @@ class RPS(commands.Cog, name="rps"):
embed.set_author(name="Fun", icon_url="https://yes.nighty.works/raw/eW5lLm.webp")
await context.send(embed=embed, view=view)
async def setup(bot) -> None:
await bot.add_cog(RPS(bot))
return rock_paper_scissors

View File

@@ -47,11 +47,7 @@ class Help(commands.Cog, name="help"):
# "context_menus": "general",
# Fun Commands
"randomfact": "fun",
"coinflip": "fun",
"rps": "fun",
"8ball": "fun",
"minesweeper": "fun",
"fun": "fun",
# Moderation Commands
"kick": "moderation",
@@ -184,6 +180,14 @@ class Help(commands.Cog, name="help"):
commands_in_category.append((app_command.name, description))
seen_names.add(app_command.name)
if hasattr(app_command, 'commands') and category == "fun":
for subcommand in app_command.commands:
if subcommand.name in seen_names:
continue
sub_desc = subcommand.description.partition("\n")[0] if getattr(subcommand, "description", None) else "No description available"
commands_in_category.append((f"{app_command.name} {subcommand.name}", sub_desc))
seen_names.add(f"{app_command.name} {subcommand.name}")
if not commands_in_category:
embed = discord.Embed(
title="Error",