aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils
diff options
context:
space:
mode:
authorGravatar Xithrius <[email protected]>2022-11-02 02:07:29 -0700
committerGravatar GitHub <[email protected]>2022-11-02 02:07:29 -0700
commit43a2acf5ee4eb354ce3dfaeef9504eee9b9b46b4 (patch)
treecbdfeb08f8d582aa98acec6a529f0fa3dcd7933c /bot/utils
parentAppeased the formatter (diff)
parentMerge pull request #1137 from DivyanshuBist/bug-issue1122-message-of-type-None (diff)
Merge branch 'main' into main
Diffstat (limited to 'bot/utils')
-rw-r--r--bot/utils/commands.py6
-rw-r--r--bot/utils/decorators.py27
-rw-r--r--bot/utils/extensions.py45
-rw-r--r--bot/utils/pagination.py3
4 files changed, 23 insertions, 58 deletions
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]
diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py
index 8954e016..442eb841 100644
--- a/bot/utils/decorators.py
+++ b/bot/utils/decorators.py
@@ -199,13 +199,28 @@ 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. 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"):
+ 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:
# 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 +228,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):
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())
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)}")