Skip to content

Commit

Permalink
feat: dev command for managing extensions (#34)
Browse files Browse the repository at this point in the history
* feat: dev command for managing extensions

* fix: limit ext command to administrators
* fix: add cog check to `cogs.dev`
* fix: more descriptive response for `extension load/unload/reload`
* fix: use check from `utils.checks`
* fix: apply changes from code review

* docs: update docstring for sync command
  • Loading branch information
n0Oo0Oo0b authored Jul 3, 2024
1 parent 6dbc76f commit 2a4c0f8
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion cogs/dev.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import io
import logging
import textwrap
import contextlib
from typing import Literal, Optional

import discord
from discord.ext import commands

from utils.checks import cogify, admin_command
from utils.commands import available_subcommands


logger = logging.getLogger(__name__)


def ext_converter(argument: str):
"""
A converter for an extension name.
"""
argument = argument.strip()
if not argument.startswith("cogs."):
argument = f"cogs.{argument}"
return argument


class Dev(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

cog_check = cogify(admin_command())

@commands.command()
async def guilddata(self, ctx: commands.Context):
"""
Expand Down Expand Up @@ -58,7 +77,7 @@ async def sync(
spec: Optional[Literal["~", "*", "^"]] = None,
) -> None:
"""
List your reminders.
Syncs app commands to Discord.
"""
if not guilds:
if spec == "~":
Expand Down Expand Up @@ -89,6 +108,49 @@ async def sync(

await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")

@commands.group(aliases=['ext'])
async def extensions(self, ctx: commands.Context):
"""
Utilities for managing loaded extensions.
"""
if ctx.invoked_subcommand is None:
await available_subcommands(ctx)

@extensions.command(name="list")
async def ext_list(self, ctx: commands.Context):
"""
Lists currently loaded extensions.
"""
ext_list = "\n".join(f"- {ext}" for ext in self.bot.extensions)
await ctx.send(f"List of loaded extensions:\n{ext_list}")

@extensions.command(name="load")
async def ext_load(self, ctx: commands.Context, ext_name: ext_converter):
"""
Loads an extension.
"""
logger.info(f"Loading extension {ext_name}")
await self.bot.load_extension(ext_name)
await ctx.send(f"Successfully loaded extension `{ext_name}`")

@extensions.command(name="unload")
async def ext_unload(self, ctx: commands.Context, ext_name: ext_converter):
"""
Unloads an extension.
"""
logger.info(f"Unloading extension {ext_name}")
await self.bot.unload_extension(ext_name)
await ctx.send(f"Successfully unloaded extension `{ext_name}`")

@extensions.command(name="reload")
async def ext_reload(self, ctx: commands.Context, ext_name: ext_converter):
"""
Reloads an extension.
"""
logger.info(f"Reloading extension {ext_name}")
await self.bot.reload_extension(ext_name)
await ctx.send(f"Successfully reloaded extension `{ext_name}`")


async def setup(bot: commands.Bot):
await bot.add_cog(Dev(bot))

0 comments on commit 2a4c0f8

Please sign in to comment.