Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cog groups to repo #138

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions uqcsbot/manage_cogs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from typing import Literal
from typing import Literal, List, Dict

import discord
from discord import app_commands
from discord.ext import commands

COG_GROUPS: Dict[str, List[str]] = {
"cron": ["working_on", "holidays"],
"db": ["starboard", "remindme", "advent", "minecraft"],
}


class ManageCogs(commands.Cog):
"""
Expand All @@ -21,7 +26,9 @@ def __init__(self, bot: commands.Bot):
async def manage_cogs(
self,
interaction: discord.Interaction,
action: Literal["load", "unload", "reload"],
action: Literal[
"load", "unload", "reload", "load-all", "unload-all", "reload-all"
],
cog: str,
):
"""
Expand All @@ -36,6 +43,24 @@ async def manage_cogs(
await self.bot.unload_extension(f"uqcsbot.{cog}")
case "reload":
await self.bot.reload_extension(f"uqcsbot.{cog}")
case "load-all":
if (cogs := COG_GROUPS.get(cog)) is not None:
for x in cogs:
await self.bot.load_extension(f"uqcsbot.{x}")
else:
raise RuntimeError(f"Unknown cog group {cog}")
case "unload-all":
if (cogs := COG_GROUPS.get(cog)) is not None:
for x in cogs:
await self.bot.unload_extension(f"uqcsbot.{x}")
else:
raise RuntimeError(f"Unknown cog group {cog}")
case "reload-all":
if (cogs := COG_GROUPS.get(cog)) is not None:
for x in cogs:
await self.bot.reload_extension(f"uqcsbot.{x}")
else:
raise RuntimeError(f"Unknown cog group {cog}")
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(
Expand Down