import discord from discord import app_commands from discord.ext import commands from utils.guild_config import ( load_guild_config, save_guild_config, set_config_value, DEFAULT_CONFIG, ROLE_LABELS, CHANNEL_LABELS, CATEGORY_LABELS, is_configured, get_role_id, get_channel_id, get_category_id ) class GuildConfig(commands.Cog): def __init__(self, bot): self.bot = bot config_group = app_commands.Group(name="config", description="Configuration du bot pour ce serveur") @config_group.command(name="set_role", description="Associer un rôle du serveur à une fonction du bot") @app_commands.describe( cle="Nom de la fonction", role="Le rôle Discord à associer" ) @app_commands.choices(cle=[app_commands.Choice(name=f"{ROLE_LABELS[k]} ({k})", value=k) for k in ROLE_LABELS]) async def set_role(self, interaction: discord.Interaction, cle: app_commands.Choice[str], role: discord.Role): if not interaction.user.guild_permissions.manage_guild: await interaction.response.send_message("❌ Il faut la permission **Gérer le serveur**.", ephemeral=True) return set_config_value(interaction.guild.id, "roles", cle.value, role.id) await interaction.response.send_message( f"✅ Rôle **{ROLE_LABELS.get(cle.value, cle.value)}** associé à {role.mention}", ephemeral=True ) @config_group.command(name="set_channel", description="Associer un salon du serveur à une fonction du bot") @app_commands.describe( cle="Nom de la fonction", channel="Le salon Discord à associer" ) @app_commands.choices(cle=[app_commands.Choice(name=f"{CHANNEL_LABELS[k]} ({k})", value=k) for k in CHANNEL_LABELS]) async def set_channel(self, interaction: discord.Interaction, cle: app_commands.Choice[str], channel: discord.TextChannel): if not interaction.user.guild_permissions.manage_guild: await interaction.response.send_message("❌ Il faut la permission **Gérer le serveur**.", ephemeral=True) return set_config_value(interaction.guild.id, "channels", cle.value, channel.id) await interaction.response.send_message( f"✅ Salon **{CHANNEL_LABELS.get(cle.value, cle.value)}** associé à {channel.mention}", ephemeral=True ) @config_group.command(name="set_category", description="Associer une catégorie du serveur à une fonction du bot") @app_commands.describe( cle="Nom de la fonction", category="La catégorie Discord à associer" ) @app_commands.choices(cle=[app_commands.Choice(name=f"{CATEGORY_LABELS[k]} ({k})", value=k) for k in CATEGORY_LABELS]) async def set_category(self, interaction: discord.Interaction, cle: app_commands.Choice[str], category: discord.CategoryChannel): if not interaction.user.guild_permissions.manage_guild: await interaction.response.send_message("❌ Il faut la permission **Gérer le serveur**.", ephemeral=True) return set_config_value(interaction.guild.id, "categories", cle.value, category.id) await interaction.response.send_message( f"✅ Catégorie **{CATEGORY_LABELS.get(cle.value, cle.value)}** associée à **{category.name}**", ephemeral=True ) @config_group.command(name="voir", description="Voir la configuration actuelle du bot sur ce serveur") async def voir_config(self, interaction: discord.Interaction): if not interaction.user.guild_permissions.manage_guild: await interaction.response.send_message("❌ Il faut la permission **Gérer le serveur**.", ephemeral=True) return config = load_guild_config(interaction.guild.id) guild = interaction.guild embed = discord.Embed( title="⚙️ Configuration du bot", color=discord.Color.blue() ) # Roles roles_text = "" for key, label in ROLE_LABELS.items(): rid = config.get("roles", {}).get(key) if rid: role = guild.get_role(int(rid)) roles_text += f"> {label} : {role.mention if role else f'❌ ID {rid} introuvable'}\n" else: roles_text += f"> {label} : ⚠️ Non configuré\n" embed.add_field(name="🎭 Rôles", value=roles_text, inline=False) # Channels channels_text = "" for key, label in CHANNEL_LABELS.items(): cid = config.get("channels", {}).get(key) if cid: ch = guild.get_channel(int(cid)) channels_text += f"> {label} : {ch.mention if ch else f'❌ ID {cid} introuvable'}\n" else: channels_text += f"> {label} : ⚠️ Non configuré\n" embed.add_field(name="📢 Salons", value=channels_text, inline=False) # Categories cats_text = "" for key, label in CATEGORY_LABELS.items(): catid = config.get("categories", {}).get(key) if catid: cat = guild.get_channel(int(catid)) cats_text += f"> {label} : **{cat.name if cat else f'❌ ID {catid} introuvable'}**\n" else: cats_text += f"> {label} : ⚠️ Non configuré\n" embed.add_field(name="📁 Catégories", value=cats_text, inline=False) missing = is_configured(interaction.guild.id) total = len(DEFAULT_CONFIG["roles"]) + len(DEFAULT_CONFIG["channels"]) + len(DEFAULT_CONFIG["categories"]) configured = total - sum(len(v) for v in missing.values()) embed.set_footer(text=f"{configured}/{total} paramètres configurés") await interaction.response.send_message(embed=embed, ephemeral=True) @config_group.command(name="reset", description="Réinitialiser la configuration du bot pour ce serveur") async def reset_config(self, interaction: discord.Interaction): if not interaction.user.guild_permissions.administrator: await interaction.response.send_message("❌ Il faut les permissions **Administrateur**.", ephemeral=True) return save_guild_config(interaction.guild.id, {"roles": {}, "channels": {}, "categories": {}}) await interaction.response.send_message("✅ Configuration réinitialisée.", ephemeral=True) async def setup(bot): await bot.add_cog(GuildConfig(bot))