staticky-bot/cogs/admin.py

121 lines
4.2 KiB
Python

import discord
from discord import app_commands
from discord.app_commands import Choice
from discord.ext import commands
from classes.my_cog import MyCog
class CogGroup(app_commands.Group, name="cog", description="Gérer les cogs"):
def __init__(self, bot: commands.Bot):
super().__init__()
self.bot = bot
@app_commands.command(name="load")
async def cog_load(self, interaction: discord.Interaction, extension: str):
"""
Charge une extension
:param interaction: discord interaction
:param extension: Nom de l'extension
"""
try:
await self.bot.load_extension(f"cogs.{extension.lower()}")
await interaction.response.send_message(f"Extension `{extension}` chargée !")
except commands.ExtensionAlreadyLoaded:
await interaction.response.send_message(f"Extension `{extension}` déjà chargée...")
except commands.ExtensionNotFound:
await interaction.response.send_message(f"Extension `{extension}` non trouvée...")
except Exception as e:
print(e)
await interaction.response.send_message(f"Une erreur inconnue est survenue:\n`{e}`")
@app_commands.command(name="unload")
async def cog_unload(self, interaction: discord.Interaction, extension: str):
"""
Décharge une extension
:param interaction: discord interaction
:param extension: Nom de l'extension
:return:
"""
try:
await self.bot.unload_extension(f"cogs.{extension.lower()}")
await interaction.response.send_message(f"Extension `{extension}` déchargée !")
except commands.ExtensionNotLoaded:
await interaction.response.send_message(f"Extension `{extension}` non chargée...")
except commands.ExtensionNotFound:
await interaction.response.send_message(f"Extension `{extension}` non trouvée...")
except Exception as e:
print(e)
await interaction.response.send_message(f"Une erreur inconnue est survenue:\n`{e}`")
@app_commands.command(name="reload")
async def cog_reload(self, interaction: discord.Interaction, extension: str):
"""
Recharge une extension
:param interaction: discord interaction
:param extension: Nom de l'extension
"""
async def fun():
await self.bot.load_extension(f"cogs.{extension.lower()}")
await interaction.response.send_message(f"Extension `{extension}` rechargée !")
try:
await self.bot.unload_extension(f"cogs.{extension.lower()}")
await fun()
except (commands.ExtensionAlreadyLoaded, commands.ExtensionNotLoaded):
await fun()
except commands.ExtensionNotFound:
await interaction.response.send_message(f"Extension `{extension}` non trouvée...")
except Exception as e:
print(e)
await interaction.response.send_message(f"Une erreur inconnue est survenue:\n`{e}`")
@app_commands.command(name="list")
async def cog_list(self, interaction: discord.Interaction):
"""
Voir la liste des extensions
:param interaction: discord interaction
"""
enabled = [str(ext) for ext in self.bot.extensions]
total_list = self.bot.initial_extensions
disabled = list(set(total_list).difference(enabled))
msg = "✅ **Enabled**\n"
for ext in enabled:
msg += f"* `{ext[5:]}`\n"
msg += "\n❌ **Disabled**\n"
for ext in disabled:
msg += f"* `{ext[5:]}`\n"
embed = discord.Embed(title="Liste des extensions",
description=msg,
color=discord.Color.blurple())
await interaction.response.send_message(embed=embed)
class Admin(MyCog):
def __init__(self, bot):
super().__init__(bot)
self.bot = bot
# Add cogs commands group to cog
self.bot.tree.add_command(CogGroup(self.bot))
async def cog_check(self, ctx):
admins = self.bot.owner_ids
return ctx.author.id in admins
async def setup(bot):
await bot.add_cog(Admin(bot))