From 2ee5d245406997e171d6694cd0f4de5d49423605 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 15 Dec 2021 20:20:48 -0500 Subject: fix: subcommands inherit their parent's whitelist solves issue with adding decorator to the parent which wouldn't apply to the children --- bot/utils/decorators.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 7a3b14ad..3688327a 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -199,13 +199,29 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo kwargs = default_kwargs.copy() allow_dms = False - # Update kwargs based on override - if hasattr(ctx.command.callback, "override"): + # determine which command's overrides we will use + # as we have groups, we want to ensure that group commands inherit from the parent + overridden_command: Union[commands.Command, commands.Group] = None + for command in [ctx.command, *ctx.command.parents]: + print(command) + if hasattr(command.callback, "override"): + overridden_command = command + break + if overridden_command is not None: + log.debug(f'Command {overridden_command} has overrides') + if overridden_command is not ctx.command: + log.debug( + f"Command '{ctx.command.qualified_name}' inherited overrides " + "from parent command '{overridden_command.qualified_name}'" + ) + + # Update kwargs based on override, if one exists + if overridden_command and hasattr(overridden_command.callback, "override"): # Handle DM invocations - allow_dms = ctx.command.callback.override_dm + allow_dms = overridden_command.callback.override_dm # Remove default kwargs if reset is True - if ctx.command.callback.override_reset: + if overridden_command.callback.override_reset: kwargs = {} log.debug( f"{ctx.author} called the '{ctx.command.name}' command and " @@ -213,9 +229,9 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo ) # Merge overwrites and defaults - for arg in ctx.command.callback.override: + for arg in overridden_command.callback.override: default_value = kwargs.get(arg) - new_value = ctx.command.callback.override[arg] + new_value = overridden_command.callback.override[arg] # Skip values that don't need merging, or can't be merged if default_value is None or isinstance(arg, int): -- cgit v1.2.3 From fb5f3f7aababbfbc6991f5b8aac4c6037c8760d7 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 12 Jan 2022 06:35:38 -0500 Subject: minor: remove print debugging statement --- bot/utils/decorators.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 3688327a..ed03c054 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -203,7 +203,6 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo # as we have groups, we want to ensure that group commands inherit from the parent overridden_command: Union[commands.Command, commands.Group] = None for command in [ctx.command, *ctx.command.parents]: - print(command) if hasattr(command.callback, "override"): overridden_command = command break -- cgit v1.2.3 From 065624e13dc4d08f97ba09adb637085f1a1a05a2 Mon Sep 17 00:00:00 2001 From: arl Date: Sat, 9 Jul 2022 16:05:03 -0400 Subject: Apply suggestions from code review --- bot/utils/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index ed03c054..f39b52e2 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -201,7 +201,7 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo # determine which command's overrides we will use # as we have groups, we want to ensure that group commands inherit from the parent - overridden_command: Union[commands.Command, commands.Group] = None + overridden_command: Optional[commands.Command] = None for command in [ctx.command, *ctx.command.parents]: if hasattr(command.callback, "override"): overridden_command = command -- cgit v1.2.3 From 3bcb96a128fd3ef9343a6df914019e95294974b3 Mon Sep 17 00:00:00 2001 From: Izan Date: Tue, 6 Sep 2022 08:29:27 -0400 Subject: Fix typehint --- bot/utils/commands.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'bot/utils') diff --git a/bot/utils/commands.py b/bot/utils/commands.py index 7c04a25a..2058507e 100644 --- a/bot/utils/commands.py +++ b/bot/utils/commands.py @@ -1,11 +1,7 @@ -from typing import Optional - from rapidfuzz import process -def get_command_suggestions( - all_commands: list[str], query: str, *, cutoff: int = 60, limit: int = 3 -) -> Optional[list]: +def get_command_suggestions(all_commands: list[str], query: str, *, cutoff: int = 60, limit: int = 3) -> list[str]: """Get similar command names.""" results = process.extract(query, all_commands, score_cutoff=cutoff, limit=limit) return [result[0] for result in results] -- cgit v1.2.3 From d8fe28eb20a2f36041021b7756dff32b78b9ae8f Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 17 Sep 2022 12:59:57 +0100 Subject: Split comment over lines evenly --- bot/utils/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index f39b52e2..7ad9d22c 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -199,8 +199,8 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo kwargs = default_kwargs.copy() allow_dms = False - # determine which command's overrides we will use - # as we have groups, we want to ensure that group commands inherit from the parent + # Determine which command's overrides we will use. Group commands will + # inherit from their parents if they don't define their own overrides overridden_command: Optional[commands.Command] = None for command in [ctx.command, *ctx.command.parents]: if hasattr(command.callback, "override"): -- cgit v1.2.3 From 6199fd9d83940aaeee9cc243b4f98732f66734ea Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 17 Sep 2022 13:00:10 +0100 Subject: Remove unnecessary hasattr check --- bot/utils/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 7ad9d22c..0061abd9 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -215,7 +215,7 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo ) # Update kwargs based on override, if one exists - if overridden_command and hasattr(overridden_command.callback, "override"): + if overridden_command: # Handle DM invocations allow_dms = overridden_command.callback.override_dm -- cgit v1.2.3 From d1049ccb60b99b18d80641bd768b00a45301b94d Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 28 Jun 2022 22:37:27 +0100 Subject: Remove reference to no longer used EmptyEmbed var --- bot/utils/pagination.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/utils') diff --git a/bot/utils/pagination.py b/bot/utils/pagination.py index 188b279f..b291f7db 100644 --- a/bot/utils/pagination.py +++ b/bot/utils/pagination.py @@ -5,7 +5,6 @@ from typing import Optional from discord import Embed, Member, Reaction from discord.abc import User -from discord.embeds import EmptyEmbed from discord.ext.commands import Context, Paginator from bot.constants import Emojis @@ -422,7 +421,7 @@ class ImagePaginator(Paginator): # Magic happens here, after page and reaction_type is set embed.description = paginator.pages[current_page] - image = paginator.images[current_page] or EmptyEmbed + image = paginator.images[current_page] or None embed.set_image(url=image) embed.set_footer(text=f"Page {current_page + 1}/{len(paginator.pages)}") -- cgit v1.2.3 From cc2dcdcdbf9e6057053bdb72bba848f4c2e19cbd Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 23 Aug 2022 20:46:14 +0100 Subject: Use extension utils from bot-core --- bot/bot.py | 8 +++++ bot/exts/avatar_modification/avatar_modify.py | 3 +- bot/exts/core/extensions.py | 22 ++++++------- bot/exts/core/internal_eval/_internal_eval.py | 3 +- bot/exts/events/advent_of_code/_cog.py | 3 +- bot/exts/fun/game.py | 3 +- bot/exts/fun/minesweeper.py | 8 ++--- bot/exts/fun/movie.py | 4 +-- bot/exts/fun/snakes/_snakes_cog.py | 3 +- bot/exts/fun/space.py | 4 +-- bot/exts/utilities/colour.py | 3 +- bot/exts/utilities/emoji.py | 8 +++-- bot/exts/utilities/epoch.py | 8 +++-- bot/exts/utilities/githubinfo.py | 3 +- bot/exts/utilities/reddit.py | 3 +- bot/exts/utilities/twemoji.py | 3 +- bot/utils/extensions.py | 45 --------------------------- 17 files changed, 46 insertions(+), 88 deletions(-) delete mode 100644 bot/utils/extensions.py (limited to 'bot/utils') diff --git a/bot/bot.py b/bot/bot.py index 226f9890..9309f50c 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -60,3 +60,11 @@ class Bot(BotBase): # This is not awaited to avoid a deadlock with any cogs that have # wait_until_guild_available in their cog_load method. scheduling.create_task(self.load_extensions(exts)) + + async def invoke_help_command(self, ctx: commands.Context) -> None: + """Invoke the help command or default help command if help extensions is not loaded.""" + if "bot.exts.core.help" in ctx.bot.extensions: + help_command = ctx.bot.get_command("help") + await ctx.invoke(help_command, ctx.command.qualified_name) + return + await ctx.send_help(ctx.command) diff --git a/bot/exts/avatar_modification/avatar_modify.py b/bot/exts/avatar_modification/avatar_modify.py index 3ee70cfd..337f510c 100644 --- a/bot/exts/avatar_modification/avatar_modify.py +++ b/bot/exts/avatar_modification/avatar_modify.py @@ -14,7 +14,6 @@ from discord.ext import commands from bot.bot import Bot from bot.constants import Colours, Emojis from bot.exts.avatar_modification._effects import PfpEffects -from bot.utils.extensions import invoke_help_command from bot.utils.halloween import spookifications log = logging.getLogger(__name__) @@ -89,7 +88,7 @@ class AvatarModify(commands.Cog): async def avatar_modify(self, ctx: commands.Context) -> None: """Groups all of the pfp modifying commands to allow a single concurrency limit.""" if not ctx.invoked_subcommand: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @avatar_modify.command(name="8bitify", root_aliases=("8bitify",)) async def eightbit_command(self, ctx: commands.Context) -> None: diff --git a/bot/exts/core/extensions.py b/bot/exts/core/extensions.py index d809d2b9..586222c8 100644 --- a/bot/exts/core/extensions.py +++ b/bot/exts/core/extensions.py @@ -4,6 +4,7 @@ from collections.abc import Mapping from enum import Enum from typing import Optional +from botcore.utils._extensions import unqualify from discord import Colour, Embed from discord.ext import commands from discord.ext.commands import Context, group @@ -12,7 +13,6 @@ from bot import exts from bot.bot import Bot from bot.constants import Client, Emojis, MODERATION_ROLES, Roles from bot.utils.checks import with_role_check -from bot.utils.extensions import EXTENSIONS, invoke_help_command, unqualify from bot.utils.pagination import LinePaginator log = logging.getLogger(__name__) @@ -46,13 +46,13 @@ class Extension(commands.Converter): argument = argument.lower() - if argument in EXTENSIONS: + if argument in ctx.bot.all_extensions: return argument - elif (qualified_arg := f"{exts.__name__}.{argument}") in EXTENSIONS: + elif (qualified_arg := f"{exts.__name__}.{argument}") in ctx.bot.all_extensions: return qualified_arg matches = [] - for ext in EXTENSIONS: + for ext in ctx.bot.all_extensions: if argument == unqualify(ext): matches.append(ext) @@ -78,7 +78,7 @@ class Extensions(commands.Cog): @group(name="extensions", aliases=("ext", "exts", "c", "cogs"), invoke_without_command=True) async def extensions_group(self, ctx: Context) -> None: """Load, unload, reload, and list loaded extensions.""" - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @extensions_group.command(name="load", aliases=("l",)) async def load_command(self, ctx: Context, *extensions: Extension) -> None: @@ -88,11 +88,11 @@ class Extensions(commands.Cog): If '\*' or '\*\*' is given as the name, all unloaded extensions will be loaded. """ # noqa: W605 if not extensions: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return if "*" in extensions or "**" in extensions: - extensions = set(EXTENSIONS) - set(self.bot.extensions.keys()) + extensions = set(self.bot.all_extensions) - set(self.bot.extensions.keys()) msg = self.batch_manage(Action.LOAD, *extensions) await ctx.send(msg) @@ -105,7 +105,7 @@ class Extensions(commands.Cog): If '\*' or '\*\*' is given as the name, all loaded extensions will be unloaded. """ # noqa: W605 if not extensions: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return blacklisted = "\n".join(UNLOAD_BLACKLIST & set(extensions)) @@ -131,11 +131,11 @@ class Extensions(commands.Cog): If '\*\*' is given as the name, all extensions, including unloaded ones, will be reloaded. """ # noqa: W605 if not extensions: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return if "**" in extensions: - extensions = EXTENSIONS + extensions = self.bot.all_extensions elif "*" in extensions: extensions = set(self.bot.extensions.keys()) | set(extensions) extensions.remove("*") @@ -175,7 +175,7 @@ class Extensions(commands.Cog): """Return a mapping of extension names and statuses to their categories.""" categories = {} - for ext in EXTENSIONS: + for ext in self.bot.all_extensions: if ext in self.bot.extensions: status = Emojis.status_online else: diff --git a/bot/exts/core/internal_eval/_internal_eval.py b/bot/exts/core/internal_eval/_internal_eval.py index 190a15ec..2daf8ef9 100644 --- a/bot/exts/core/internal_eval/_internal_eval.py +++ b/bot/exts/core/internal_eval/_internal_eval.py @@ -9,7 +9,6 @@ from discord.ext import commands from bot.bot import Bot from bot.constants import Client, Roles from bot.utils.decorators import with_role -from bot.utils.extensions import invoke_help_command from ._helpers import EvalContext @@ -154,7 +153,7 @@ class InternalEval(commands.Cog): async def internal_group(self, ctx: commands.Context) -> None: """Internal commands. Top secret!""" if not ctx.invoked_subcommand: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @internal_group.command(name="eval", aliases=("e",)) @with_role(Roles.admins) diff --git a/bot/exts/events/advent_of_code/_cog.py b/bot/exts/events/advent_of_code/_cog.py index 1d8b0ca7..ab5a7a34 100644 --- a/bot/exts/events/advent_of_code/_cog.py +++ b/bot/exts/events/advent_of_code/_cog.py @@ -18,7 +18,6 @@ from bot.exts.events.advent_of_code.views.dayandstarview import AoCDropdownView from bot.utils import members from bot.utils.decorators import InChannelCheckFailure, in_month, whitelist_override, with_role from bot.utils.exceptions import MovedCommandError -from bot.utils.extensions import invoke_help_command log = logging.getLogger(__name__) @@ -122,7 +121,7 @@ class AdventOfCode(commands.Cog): async def adventofcode_group(self, ctx: commands.Context) -> None: """All of the Advent of Code commands.""" if not ctx.invoked_subcommand: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @with_role(Roles.admins) @adventofcode_group.command( diff --git a/bot/exts/fun/game.py b/bot/exts/fun/game.py index 5f56bef7..4730d5b3 100644 --- a/bot/exts/fun/game.py +++ b/bot/exts/fun/game.py @@ -15,7 +15,6 @@ from discord.ext.commands import Cog, Context, group from bot.bot import Bot from bot.constants import STAFF_ROLES, Tokens from bot.utils.decorators import with_role -from bot.utils.extensions import invoke_help_command from bot.utils.pagination import ImagePaginator, LinePaginator # Base URL of IGDB API @@ -267,7 +266,7 @@ class Games(Cog): """ # When user didn't specified genre, send help message if genre is None: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return # Capitalize genre for check diff --git a/bot/exts/fun/minesweeper.py b/bot/exts/fun/minesweeper.py index a48b5051..782fb9d8 100644 --- a/bot/exts/fun/minesweeper.py +++ b/bot/exts/fun/minesweeper.py @@ -11,7 +11,6 @@ from bot.bot import Bot from bot.constants import Client from bot.utils.converters import CoordinateConverter from bot.utils.exceptions import UserNotPlayingError -from bot.utils.extensions import invoke_help_command MESSAGE_MAPPING = { 0: ":stop_button:", @@ -51,13 +50,14 @@ class Game: class Minesweeper(commands.Cog): """Play a game of Minesweeper.""" - def __init__(self): + def __init__(self, bot: Bot): + self.bot = bot self.games: dict[int, Game] = {} @commands.group(name="minesweeper", aliases=("ms",), invoke_without_command=True) async def minesweeper_group(self, ctx: commands.Context) -> None: """Commands for Playing Minesweeper.""" - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @staticmethod def get_neighbours(x: int, y: int) -> Iterator[tuple[int, int]]: @@ -267,4 +267,4 @@ class Minesweeper(commands.Cog): def setup(bot: Bot) -> None: """Load the Minesweeper cog.""" - bot.add_cog(Minesweeper()) + bot.add_cog(Minesweeper(bot)) diff --git a/bot/exts/fun/movie.py b/bot/exts/fun/movie.py index a04eeb41..4418b938 100644 --- a/bot/exts/fun/movie.py +++ b/bot/exts/fun/movie.py @@ -9,7 +9,6 @@ from discord.ext.commands import Cog, Context, group from bot.bot import Bot from bot.constants import Tokens -from bot.utils.extensions import invoke_help_command from bot.utils.pagination import ImagePaginator # Define base URL of TMDB @@ -50,6 +49,7 @@ class Movie(Cog): """Movie Cog contains movies command that grab random movies from TMDB.""" def __init__(self, bot: Bot): + self.bot = bot self.http_session: ClientSession = bot.http_session @group(name="movies", aliases=("movie",), invoke_without_command=True) @@ -73,7 +73,7 @@ class Movie(Cog): try: result = await self.get_movies_data(self.http_session, MovieGenres[genre].value, 1) except KeyError: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return # Check if "results" is in result. If not, throw error. diff --git a/bot/exts/fun/snakes/_snakes_cog.py b/bot/exts/fun/snakes/_snakes_cog.py index 59e57199..96718200 100644 --- a/bot/exts/fun/snakes/_snakes_cog.py +++ b/bot/exts/fun/snakes/_snakes_cog.py @@ -22,7 +22,6 @@ from bot.constants import ERROR_REPLIES, Tokens from bot.exts.fun.snakes import _utils as utils from bot.exts.fun.snakes._converter import Snake from bot.utils.decorators import locked -from bot.utils.extensions import invoke_help_command log = logging.getLogger(__name__) @@ -440,7 +439,7 @@ class Snakes(Cog): @group(name="snakes", aliases=("snake",), invoke_without_command=True) async def snakes_group(self, ctx: Context) -> None: """Commands from our first code jam.""" - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @bot_has_permissions(manage_messages=True) @snakes_group.command(name="antidote") diff --git a/bot/exts/fun/space.py b/bot/exts/fun/space.py index 48ad0f96..0bbe0b33 100644 --- a/bot/exts/fun/space.py +++ b/bot/exts/fun/space.py @@ -11,7 +11,6 @@ from discord.ext.commands import Cog, Context, group from bot.bot import Bot from bot.constants import Tokens from bot.utils.converters import DateConverter -from bot.utils.extensions import invoke_help_command logger = logging.getLogger(__name__) @@ -27,6 +26,7 @@ class Space(Cog): def __init__(self, bot: Bot): self.http_session = bot.http_session + self.bot = bot self.rovers = {} self.get_rovers.start() @@ -50,7 +50,7 @@ class Space(Cog): @group(name="space", invoke_without_command=True) async def space(self, ctx: Context) -> None: """Head command that contains commands about space.""" - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @space.command(name="apod") async def apod(self, ctx: Context, date: Optional[str]) -> None: diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index ee6bad93..5282bc6d 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -13,7 +13,6 @@ from discord.ext import commands from bot import constants from bot.bot import Bot -from bot.exts.core.extensions import invoke_help_command from bot.utils.decorators import whitelist_override THUMBNAIL_SIZE = (80, 80) @@ -99,7 +98,7 @@ class Colour(commands.Cog): extra_colour = ImageColor.getrgb(colour_input) await self.send_colour_response(ctx, extra_colour) except ValueError: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @colour.command() async def rgb(self, ctx: commands.Context, red: int, green: int, blue: int) -> None: diff --git a/bot/exts/utilities/emoji.py b/bot/exts/utilities/emoji.py index fa438d7f..2b2fab8a 100644 --- a/bot/exts/utilities/emoji.py +++ b/bot/exts/utilities/emoji.py @@ -10,7 +10,6 @@ from discord.ext import commands from bot.bot import Bot from bot.constants import Colours, ERROR_REPLIES -from bot.utils.extensions import invoke_help_command from bot.utils.pagination import LinePaginator from bot.utils.time import time_since @@ -20,6 +19,9 @@ log = logging.getLogger(__name__) class Emojis(commands.Cog): """A collection of commands related to emojis in the server.""" + def __init__(self, bot: Bot) -> None: + self.bot = bot + @staticmethod def embed_builder(emoji: dict) -> tuple[Embed, list[str]]: """Generates an embed with the emoji names and count.""" @@ -74,7 +76,7 @@ class Emojis(commands.Cog): if emoji is not None: await ctx.invoke(self.info_command, emoji) else: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @emoji_group.command(name="count", aliases=("c",)) async def count_command(self, ctx: commands.Context, *, category_query: str = None) -> None: @@ -120,4 +122,4 @@ class Emojis(commands.Cog): def setup(bot: Bot) -> None: """Load the Emojis cog.""" - bot.add_cog(Emojis()) + bot.add_cog(Emojis(bot)) diff --git a/bot/exts/utilities/epoch.py b/bot/exts/utilities/epoch.py index 42312dd1..2a21688e 100644 --- a/bot/exts/utilities/epoch.py +++ b/bot/exts/utilities/epoch.py @@ -6,7 +6,6 @@ from dateutil import parser from discord.ext import commands from bot.bot import Bot -from bot.utils.extensions import invoke_help_command # https://discord.com/developers/docs/reference#message-formatting-timestamp-styles STYLES = { @@ -48,6 +47,9 @@ class DateString(commands.Converter): class Epoch(commands.Cog): """Convert an entered time and date to a unix timestamp.""" + def __init__(self, bot: Bot) -> None: + self.bot = bot + @commands.command(name="epoch") async def epoch(self, ctx: commands.Context, *, date_time: DateString = None) -> None: """ @@ -71,7 +73,7 @@ class Epoch(commands.Cog): Times in the dropdown are shown in UTC """ if not date_time: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return if isinstance(date_time, tuple): @@ -135,4 +137,4 @@ class TimestampMenuView(discord.ui.View): def setup(bot: Bot) -> None: """Load the Epoch cog.""" - bot.add_cog(Epoch()) + bot.add_cog(Epoch(bot)) diff --git a/bot/exts/utilities/githubinfo.py b/bot/exts/utilities/githubinfo.py index 046f67df..ed176290 100644 --- a/bot/exts/utilities/githubinfo.py +++ b/bot/exts/utilities/githubinfo.py @@ -12,7 +12,6 @@ from discord.ext import commands from bot.bot import Bot from bot.constants import Colours, ERROR_REPLIES, Emojis, NEGATIVE_REPLIES, Tokens -from bot.exts.core.extensions import invoke_help_command log = logging.getLogger(__name__) @@ -168,7 +167,7 @@ class GithubInfo(commands.Cog): async def github_group(self, ctx: commands.Context) -> None: """Commands for finding information related to GitHub.""" if ctx.invoked_subcommand is None: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: diff --git a/bot/exts/utilities/reddit.py b/bot/exts/utilities/reddit.py index 782583d2..fa50eb36 100644 --- a/bot/exts/utilities/reddit.py +++ b/bot/exts/utilities/reddit.py @@ -15,7 +15,6 @@ from discord.utils import escape_markdown, sleep_until from bot.bot import Bot from bot.constants import Channels, ERROR_REPLIES, Emojis, Reddit as RedditConfig, STAFF_ROLES from bot.utils.converters import Subreddit -from bot.utils.extensions import invoke_help_command from bot.utils.messages import sub_clyde from bot.utils.pagination import ImagePaginator, LinePaginator @@ -302,7 +301,7 @@ class Reddit(Cog): @group(name="reddit", invoke_without_command=True) async def reddit_group(self, ctx: Context) -> None: """View the top posts from various subreddits.""" - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) @reddit_group.command(name="top") async def top_command(self, ctx: Context, subreddit: Subreddit = "r/Python") -> None: diff --git a/bot/exts/utilities/twemoji.py b/bot/exts/utilities/twemoji.py index c915f05b..a4477bc1 100644 --- a/bot/exts/utilities/twemoji.py +++ b/bot/exts/utilities/twemoji.py @@ -9,7 +9,6 @@ from emoji import UNICODE_EMOJI_ENGLISH, is_emoji from bot.bot import Bot from bot.constants import Colours, Roles from bot.utils.decorators import whitelist_override -from bot.utils.extensions import invoke_help_command log = logging.getLogger(__name__) BASE_URLS = { @@ -133,7 +132,7 @@ class Twemoji(commands.Cog): async def twemoji(self, ctx: commands.Context, *raw_emoji: str) -> None: """Sends a preview of a given Twemoji, specified by codepoint or emoji.""" if len(raw_emoji) == 0: - await invoke_help_command(ctx) + await self.bot.invoke_help_command(ctx) return try: codepoint = self.codepoint_from_input(raw_emoji) diff --git a/bot/utils/extensions.py b/bot/utils/extensions.py deleted file mode 100644 index 09192ae2..00000000 --- a/bot/utils/extensions.py +++ /dev/null @@ -1,45 +0,0 @@ -import importlib -import inspect -import pkgutil -from collections.abc import Iterator -from typing import NoReturn - -from discord.ext.commands import Context - -from bot import exts - - -def unqualify(name: str) -> str: - """Return an unqualified name given a qualified module/package `name`.""" - return name.rsplit(".", maxsplit=1)[-1] - - -def walk_extensions() -> Iterator[str]: - """Yield extension names from the bot.exts subpackage.""" - - def on_error(name: str) -> NoReturn: - raise ImportError(name=name) # pragma: no cover - - for module in pkgutil.walk_packages(exts.__path__, f"{exts.__name__}.", onerror=on_error): - if unqualify(module.name).startswith("_"): - # Ignore module/package names starting with an underscore. - continue - - if module.ispkg: - imported = importlib.import_module(module.name) - if not inspect.isfunction(getattr(imported, "setup", None)): - # If it lacks a setup function, it's not an extension. - continue - - yield module.name - - -async def invoke_help_command(ctx: Context) -> None: - """Invoke the help command or default help command if help extensions is not loaded.""" - if "bot.exts.core.help" in ctx.bot.extensions: - help_command = ctx.bot.get_command("help") - await ctx.invoke(help_command, ctx.command.qualified_name) - return - await ctx.send_help(ctx.command) - -EXTENSIONS = frozenset(walk_extensions()) -- cgit v1.2.3