-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_cog.py
53 lines (44 loc) · 1.88 KB
/
manage_cog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import Literal
import discord
from discord import app_commands
from discord.ext import commands
from custom_logger import Logger
logger = Logger('gwaff.bot.cogs')
class ManageCogs(commands.Cog):
"""
Note that most of these commands can make the bot load files to execute. Care should be made to ensure only entrusted users have access.
"""
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="managecogs")
@app_commands.checks.has_permissions(manage_guild=True)
async def manage_cogs(
self, interaction: discord.Interaction,
action: Literal["load", "unload", "reload"],
cog: str,
):
"""
Trys to unload a cog (i.e. python file).
Note that most of these commands can make the bot load files to execute. Care should be made to ensure only entrusted users have access.
"""
try:
if action == "load":
await self.bot.load_extension(f"{cog}")
elif action == "unload":
await self.bot.unload_extension(f"{cog}")
elif action == "reload":
await self.bot.reload_extension(f"{cog}")
else:
raise Exception("Unknown Operation")
except Exception as error:
# Many errors can be caught during loading/unloading/reloading the bot, so it would be painful to separate by exception type
await interaction.response.send_message(
f"Error occured {action}ing {cog}: {error}"
)
logger.error(f"Error occured {action}ing {cog}: {error}")
return
await interaction.response.send_message(f"Successfully {action}ed {cog}")
logger.info(f"Successfully {action}ed {cog}")
await self.bot.tree.sync()
async def setup(bot: commands.Bot):
await bot.add_cog(ManageCogs(bot))