feat: Refactored all cogs implementation
Some checks failed
CI / Run tests (pull_request) Has been cancelled

This commit is contained in:
2026-09-12 23:16:43 +02:00
parent 505621e0cd
commit c86ea54e86
10 changed files with 451 additions and 123 deletions

View File

@@ -5,7 +5,9 @@ from discord.ext import bridge
from cogs.cs import CSCog
from cogs.insult import InsultsCog
from cogs.roulette import RouletteCog
from cogs.roles import RolesCog
from setup.logger import LOGGER
from setup.config import Config
bot_intents = discord.Intents.default()
bot_intents.message_content = True
@@ -19,9 +21,26 @@ bot = bridge.Bot(command_prefix='$', intents=bot_intents)
COG_REGISTRY = {
'cs': CSCog,
'insult': InsultsCog,
'roulette': RouletteCog
'roulette': RouletteCog,
'roles': RolesCog,
}
# Register a global admin slash command directly on the bot to avoid circular imports.
from discord import option as discord_option
@bot.slash_command(name="reload", description="Reload a feature (cog) by name, or 'all')")
@discord_option(name="feature", description="Feature to reload (e.g. cs, insult, roles, roulette, all)", input_type=str, required=True)
async def reload(ctx: discord.ApplicationContext, feature: str):
# Only allow administrators
if not ctx.author or not getattr(ctx.author, 'guild_permissions', None) or not ctx.author.guild_permissions.administrator:
await ctx.respond("You must be a server administrator to use this command.", ephemeral=True)
return
await ctx.defer()
reload_feature(feature)
await ctx.followup.send(f"Reload requested for feature: {feature}", ephemeral=True)
def _normalize_cog_names(cogs=None):
if cogs is None:
@@ -46,6 +65,12 @@ def _normalize_cog_names(cogs=None):
def register_cogs(cogs=None):
# Ensure guild configs are loaded before instantiating cogs
try:
Config.load_guilds_configs()
except Exception:
LOGGER.debug('Could not reload guild configs during register_cogs.')
selected_cogs = _normalize_cog_names(cogs)
existing_cog_names = set(COG_REGISTRY.values())
@@ -75,6 +100,11 @@ def reload_feature(feature: str):
return
LOGGER.debug(f'Reloading {feature}...')
# Refresh guild configs before reloading a single feature
try:
Config.load_guilds_configs()
except Exception:
LOGGER.debug('Could not reload guild configs during reload_feature.')
target_cog_name = COG_REGISTRY[normalized_feature].__name__
for existing_name in list(bot.cogs.keys()):
if existing_name == target_cog_name: