From 44e129bc66ef71844676743c99f20560658469da Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Tue, 28 Mar 2023 02:49:59 +0200 Subject: [PATCH] Add redbot.core.app_commands namespace (#6006) --- .github/labeler.yml | 5 +++ redbot/core/app_commands/__init__.py | 61 ++++++++++++++++++++++++++++ redbot/core/bot.py | 5 ++- redbot/core/commands/commands.py | 5 ++- redbot/core/core_commands.py | 4 +- redbot/core/tree.py | 13 +++--- tests/core/test_app_commands.py | 23 +++++++++++ tests/core/test_commands.py | 9 ---- 8 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 redbot/core/app_commands/__init__.py create mode 100644 tests/core/test_app_commands.py diff --git a/.github/labeler.yml b/.github/labeler.yml index 8f31f0d33..aa1a8b41c 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -134,6 +134,11 @@ - redbot/core/bank.py # Docs - docs/framework_bank.rst +"Category: Core - API - App Commands Package": + # Source + - redbot/core/app_commands/* + # Tests + - tests/core/test_app_commands.py "Category: Core - API - Commands Package": # Source - any: diff --git a/redbot/core/app_commands/__init__.py b/redbot/core/app_commands/__init__.py new file mode 100644 index 000000000..cd5b2ddb8 --- /dev/null +++ b/redbot/core/app_commands/__init__.py @@ -0,0 +1,61 @@ +########## SENSITIVE SECTION WARNING ########### +################################################ +# Any edits of any of the exported names # +# may result in a breaking change. # +# Ensure no names are removed without warning. # +################################################ + +### DEP-WARN: Check this *every* discord.py update +from discord.app_commands import ( + AllChannels as AllChannels, + AppCommand as AppCommand, + AppCommandChannel as AppCommandChannel, + AppCommandError as AppCommandError, + AppCommandGroup as AppCommandGroup, + AppCommandPermissions as AppCommandPermissions, + AppCommandThread as AppCommandThread, + Argument as Argument, + BotMissingPermissions as BotMissingPermissions, + Command as Command, + CommandAlreadyRegistered as CommandAlreadyRegistered, + CommandInvokeError as CommandInvokeError, + CommandLimitReached as CommandLimitReached, + CommandNotFound as CommandNotFound, + CommandOnCooldown as CommandOnCooldown, + CommandSignatureMismatch as CommandSignatureMismatch, + CommandSyncFailure as CommandSyncFailure, + CommandTree as CommandTree, + ContextMenu as ContextMenu, + Cooldown as Cooldown, + Group as Group, + GuildAppCommandPermissions as GuildAppCommandPermissions, + MissingAnyRole as MissingAnyRole, + MissingApplicationID as MissingApplicationID, + MissingPermissions as MissingPermissions, + MissingRole as MissingRole, + Namespace as Namespace, + NoPrivateMessage as NoPrivateMessage, + Parameter as Parameter, + Range as Range, + Transform as Transform, + Transformer as Transformer, + TransformerError as TransformerError, + TranslationContext as TranslationContext, + TranslationContextLocation as TranslationContextLocation, + TranslationContextTypes as TranslationContextTypes, + TranslationError as TranslationError, + Translator as Translator, + autocomplete as autocomplete, + check as check, + CheckFailure as CheckFailure, + Choice as Choice, + choices as choices, + command as command, + context_menu as context_menu, + default_permissions as default_permissions, + describe as describe, + guild_only as guild_only, + guilds as guilds, + locale_str as locale_str, + rename as rename, +) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index 4590f5304..40c42896c 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -59,7 +59,8 @@ from .utils._internal_utils import send_to_owners_with_prefix_replaced if TYPE_CHECKING: from discord.ext.commands.hybrid import CommandCallback, ContextT, P - from discord import app_commands + + from redbot.core import app_commands _T = TypeVar("_T") @@ -1728,7 +1729,7 @@ class Red( raise TypeError("command type must be one of chat_input, message, user") async with cfg as curr_commands: if len(curr_commands) >= limit: - raise discord.app_commands.CommandLimitReached(None, limit, type=command_type) + raise app_commands.CommandLimitReached(None, limit, type=command_type) if command_name not in curr_commands: curr_commands[command_name] = None diff --git a/redbot/core/commands/commands.py b/redbot/core/commands/commands.py index 09415f5e5..3790d2f0e 100644 --- a/redbot/core/commands/commands.py +++ b/redbot/core/commands/commands.py @@ -46,6 +46,7 @@ from discord.ext.commands import ( from .errors import ConversionFailure from .requires import PermState, PrivilegeLevel, Requires, PermStateAllowedStates +from .. import app_commands from ..i18n import Translator _T = TypeVar("_T") @@ -1093,7 +1094,7 @@ class HybridGroup(Group, DPYHybridGroup[_CogT, _P, _T]): def hybrid_command( - name: Union[str, discord.app_commands.locale_str] = discord.utils.MISSING, + name: Union[str, app_commands.locale_str] = discord.utils.MISSING, *, with_app_command: bool = True, **attrs: Any, @@ -1113,7 +1114,7 @@ def hybrid_command( def hybrid_group( - name: Union[str, discord.app_commands.locale_str] = discord.utils.MISSING, + name: Union[str, app_commands.locale_str] = discord.utils.MISSING, *, with_app_command: bool = True, **attrs: Any, diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index 59e48de50..cd14a8084 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -18,7 +18,7 @@ import pip import traceback from pathlib import Path from collections import defaultdict -from redbot.core import data_manager +from redbot.core import app_commands, data_manager from redbot.core.utils.menus import menu from redbot.core.utils.views import SetApiView from redbot.core.commands import GuildConverter, RawUserIdConverter @@ -2005,7 +2005,7 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic): try: await self.bot.enable_app_command(command_name, raw_type) - except discord.app_commands.CommandLimitReached: + except app_commands.CommandLimitReached: await ctx.send(_("The command limit has been reached. Disable a command first.")) return diff --git a/redbot/core/tree.py b/redbot/core/tree.py index 01f5853de..94bb76676 100644 --- a/redbot/core/tree.py +++ b/redbot/core/tree.py @@ -1,22 +1,23 @@ import discord from discord.abc import Snowflake from discord.utils import MISSING -from discord.app_commands import ( - Command, - Group, - ContextMenu, + +from .app_commands import ( AppCommand, AppCommandError, BotMissingPermissions, CheckFailure, + Command, CommandAlreadyRegistered, CommandInvokeError, CommandNotFound, CommandOnCooldown, + CommandTree, + ContextMenu, + Group, NoPrivateMessage, TransformerError, ) - from .i18n import Translator from .utils.chat_formatting import humanize_list, inline @@ -31,7 +32,7 @@ log = logging.getLogger("red") _ = Translator(__name__, __file__) -class RedTree(discord.app_commands.CommandTree): +class RedTree(CommandTree): """A container that holds application command information. Internally does not actually add commands to the tree unless they are diff --git a/tests/core/test_app_commands.py b/tests/core/test_app_commands.py new file mode 100644 index 000000000..a763ab946 --- /dev/null +++ b/tests/core/test_app_commands.py @@ -0,0 +1,23 @@ +import inspect + +import pytest +from discord import app_commands as dpy_app_commands + +from redbot.core import app_commands + + +def test_dpy_app_commands_reexports(): + dpy_attrs = set() + for attr_name, attr_value in dpy_app_commands.__dict__.items(): + if attr_name.startswith("_") or inspect.ismodule(attr_value): + continue + + dpy_attrs.add(attr_name) + + missing_attrs = dpy_attrs - set(app_commands.__dict__.keys()) + + if missing_attrs: + pytest.fail( + "redbot.core.app_commands is missing these names from discord.app_commands: " + + ", ".join(missing_attrs) + ) diff --git a/tests/core/test_commands.py b/tests/core/test_commands.py index 62d45c723..0aca8d06a 100644 --- a/tests/core/test_commands.py +++ b/tests/core/test_commands.py @@ -50,15 +50,6 @@ def test_dpy_commands_reexports(): dpy_attrs.add(attr_name) missing_attrs = dpy_attrs - set(commands.__dict__.keys()) - # temporarily ignore things related to app commands as the work on that is done separately - missing_attrs -= { - "GroupCog", - "HybridGroup", - "hybrid_group", - "hybrid_command", - "HybridCommand", - "HybridCommandError", - } if missing_attrs: pytest.fail(