Skip to content

Commit

Permalink
fix: apply changes from code review
Browse files Browse the repository at this point in the history
Changes made:
- strip extension names before processing
- use existing `utils.commands.available_subcommands` instead of `ctx.send_help` for consistency (this may be changed back later in ib-ai#36)
- Typehints for `ctx`
- Docstring formatting consistency
- Logging of load/unload/reloading of extensions
  • Loading branch information
n0Oo0Oo0b committed May 27, 2024
1 parent cf09663 commit 89a1385
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions cogs/dev.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import logging
import textwrap
import contextlib
from typing import Literal, Optional
Expand All @@ -7,9 +8,17 @@
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
Expand Down Expand Up @@ -97,29 +106,40 @@ async def sync(self, ctx: commands.Context, guilds: commands.Greedy[discord.Obje
async def extensions(self, ctx: commands.Context):
"""Utilities for extensions"""
if ctx.invoked_subcommand is None:
await ctx.send_help("ext")
await available_subcommands(ctx)

@extensions.command(name="list")
async def ext_list(self, ctx):
"""Lists currently loaded extensions"""
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, ext_name: ext_converter):
"""Loads an extension"""
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, ext_name: ext_converter):
"""Unloads an extensions"""
async def ext_unload(self, ctx: commands.Context, ext_name: ext_converter):
"""
Unloads an extensions.
"""
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, ext_name: ext_converter):
"""Reloads an extension"""
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}`")

Expand Down

0 comments on commit 89a1385

Please sign in to comment.