diff --git a/disnake/app_commands.py b/disnake/app_commands.py index b92c7a0b74..a276f601da 100644 --- a/disnake/app_commands.py +++ b/disnake/app_commands.py @@ -468,7 +468,7 @@ def __init__( name: LocalizedRequired, dm_permission: Optional[bool] = None, default_member_permissions: Optional[Union[Permissions, int]] = None, - id: int = None, + id: Optional[int] = None, ): self.type: ApplicationCommandType = enum_if_int(ApplicationCommandType, type) @@ -605,7 +605,7 @@ def __init__( name: LocalizedRequired, dm_permission: Optional[bool] = None, default_member_permissions: Optional[Union[Permissions, int]] = None, - id: int = None, + id: Optional[int] = None, ): super().__init__( type=ApplicationCommandType.user, @@ -693,7 +693,7 @@ def __init__( name: LocalizedRequired, dm_permission: Optional[bool] = None, default_member_permissions: Optional[Union[Permissions, int]] = None, - id: int = None, + id: Optional[int] = None, ): super().__init__( type=ApplicationCommandType.message, @@ -799,7 +799,7 @@ def __init__( options: Optional[List[Option]] = None, dm_permission: Optional[bool] = None, default_member_permissions: Optional[Union[Permissions, int]] = None, - id: int = None, + id: Optional[int] = None, ): super().__init__( type=ApplicationCommandType.chat_input, diff --git a/disnake/ext/commands/errors.py b/disnake/ext/commands/errors.py index 7bbdf53d55..9d783a4e4f 100644 --- a/disnake/ext/commands/errors.py +++ b/disnake/ext/commands/errors.py @@ -1016,7 +1016,9 @@ class CommandRegistrationError(ClientException): Whether the name that conflicts is an alias of the command we try to add. """ - def __init__(self, name: str, *, alias_conflict: bool = False, guild_id: int = None) -> None: + def __init__( + self, name: str, *, alias_conflict: bool = False, guild_id: Optional[int] = None + ) -> None: self.name: str = name self.alias_conflict: bool = alias_conflict self.guild_id: Optional[int] = guild_id diff --git a/disnake/ext/commands/flags.py b/disnake/ext/commands/flags.py index ae51d7bf32..b3f4d260bc 100644 --- a/disnake/ext/commands/flags.py +++ b/disnake/ext/commands/flags.py @@ -1,32 +1,10 @@ -""" -The MIT License (MIT) - -Copyright (c) 2015-2021 Rapptz -Copyright (c) 2021-present Disnake Development - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -""" +# SPDX-License-Identifier: MIT + from __future__ import annotations from typing import TYPE_CHECKING -from disnake.flags import BaseFlags, alias_flag_value, all_flags_value, fill_with_flags, flag_value +from disnake.flags import BaseFlags, alias_flag_value, all_flags_value, flag_value if TYPE_CHECKING: from typing_extensions import Self @@ -34,7 +12,6 @@ __all__ = ("ApplicationCommandSyncFlags",) -@fill_with_flags() class ApplicationCommandSyncFlags(BaseFlags): """Controls the library's application command syncing policy. diff --git a/disnake/ext/commands/interaction_bot_base.py b/disnake/ext/commands/interaction_bot_base.py index b445cd701a..c8672bc009 100644 --- a/disnake/ext/commands/interaction_bot_base.py +++ b/disnake/ext/commands/interaction_bot_base.py @@ -142,7 +142,7 @@ class InteractionBotBase(CommonBotBase): def __init__( self, *, - command_sync: ApplicationCommandSyncFlags = None, + command_sync: Optional[ApplicationCommandSyncFlags] = None, test_guilds: Optional[Sequence[int]] = None, **options: Any, ): @@ -178,9 +178,7 @@ def __init__( self._schedule_app_command_preparation() def application_commands_iterator(self) -> Iterable[InvokableApplicationCommand]: - return chain( - self._all_app_commands.values(), - ) + yield from self._all_app_commands.values() @property def all_app_commands(self) -> List[InvokableApplicationCommand]: @@ -371,7 +369,7 @@ def add_message_command(self, message_command: InvokableMessageCommand) -> None: ] = message_command def remove_slash_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[InvokableSlashCommand]: """Removes an :class:`InvokableSlashCommand` from the internal list of slash commands. @@ -400,7 +398,7 @@ def remove_slash_command( return command def remove_user_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[InvokableUserCommand]: """Removes an :class:`InvokableUserCommand` from the internal list of user commands. @@ -422,7 +420,7 @@ def remove_user_command( return command def remove_message_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[InvokableMessageCommand]: """Removes an :class:`InvokableMessageCommand` from the internal list of message commands. @@ -445,24 +443,36 @@ def remove_message_command( @overload def get_app_command( - self, name: str, type: Literal[ApplicationCommandType.chat_input], *, guild_id: int = None + self, + name: str, + type: Literal[ApplicationCommandType.chat_input], + *, + guild_id: Optional[int] = None, ) -> Optional[InvokableSlashCommand]: ... @overload def get_app_command( - self, name: str, type: Literal[ApplicationCommandType.message], *, guild_id: int = None + self, + name: str, + type: Literal[ApplicationCommandType.message], + *, + guild_id: Optional[int] = None, ) -> Optional[InvokableMessageCommand]: ... @overload def get_app_command( - self, name: str, type: Literal[ApplicationCommandType.user], *, guild_id: int = None + self, + name: str, + type: Literal[ApplicationCommandType.user], + *, + guild_id: Optional[int] = None, ) -> Optional[InvokableUserCommand]: ... def get_app_command( - self, name: str, type: ApplicationCommandType, *, guild_id: int = None + self, name: str, type: ApplicationCommandType, *, guild_id: Optional[int] = None ) -> Optional[InvokableApplicationCommand]: # this does not get commands by ID, use (some other method) to do that if not isinstance(name, str): @@ -473,7 +483,7 @@ def get_app_command( return command def get_slash_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[Union[InvokableSlashCommand, SubCommandGroup, SubCommand]]: """Works like ``Bot.get_command``, but for slash commands. @@ -515,7 +525,7 @@ def get_slash_command( return group.children.get(chain[2]) def get_user_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[InvokableUserCommand]: """Gets an :class:`InvokableUserCommand` from the internal list of user commands. @@ -533,7 +543,7 @@ def get_user_command( return self.get_app_command(name, ApplicationCommandType.user, guild_id=guild_id) def get_message_command( - self, name: str, *, guild_id: int = None + self, name: str, *, guild_id: Optional[int] = None ) -> Optional[InvokableMessageCommand]: """Gets an :class:`InvokableMessageCommand` from the internal list of message commands. diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index 7dd9976eeb..f284e6313a 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -176,7 +176,11 @@ def root_parent(self) -> InvokableSlashCommand: @property def parents(self) -> Tuple[InvokableSlashCommand]: - """Tuple[:class:`InvokableSlashCommand`]: Returns all parents of this group. + """Tuple[:class:`InvokableSlashCommand`]: Retrieves the parents of this command. + + If the command has no parents then it returns an empty :class:`tuple`. + + For example in commands ``/a b test``, the parents are ``(b, a)``. .. versionadded:: 2.6 """ @@ -195,25 +199,10 @@ def mention(self) -> str: # todo: add docs and make ID non-nullable return f"" - # todo: refactor this class to make this not optional @property - def parent(self) -> Optional[InvokableSlashCommand]: + def parent(self) -> InvokableSlashCommand: return self._parent - @property - def parents( - self, - ) -> Tuple[InvokableSlashCommand]: - """Tuple[:class:`InvokableSlashCommand`]: Retrieves the parents of this command. - - If the command has no parents then it returns an empty :class:`tuple`. - - For example in commands ``/a b test``, the parents are ``(b, a)``. - - .. versionadded:: 2.6 - """ - return (self.parent,) # type: ignore - def sub_command( self, name: LocalizedOptional = None, @@ -344,7 +333,7 @@ def root_parent(self) -> InvokableSlashCommand: .. versionadded:: 2.6 """ - return self.parent.parent if isinstance(self.parent, SubCommandGroup) else self.parent + return self._parent._parent if isinstance(self._parent, SubCommandGroup) else self._parent @property def parents( @@ -357,10 +346,10 @@ def parents( .. versionadded:: 2.6 """ - # here I'm not using 'self.parent.parents + (self.parent,)' because it causes typing issues - if isinstance(self.parent, SubCommandGroup): - return (self.parent, self.parent.parent) - return (self.parent,) + # here I'm not using 'self._parent._parents + (self._parent,)' because it causes typing issues + if isinstance(self._parent, SubCommandGroup): + return (self._parent, self._parent._parent) + return (self._parent,) @property def description(self) -> str: @@ -382,29 +371,9 @@ def mention(self) -> str: return f"" @property - def parent(self) -> Optional[Union[InvokableSlashCommand, SubCommandGroup]]: + def parent(self) -> Union[InvokableSlashCommand, SubCommandGroup]: return self._parent - @property - def parents( - self, - ) -> Union[Tuple[InvokableSlashCommand], Tuple[SubCommandGroup, InvokableSlashCommand]]: - """Union[Tuple[:class:`InvokableSlashCommand`], Tuple[:class:`SubCommandGroup`, :class:`InvokableSlashCommand`]]: Retrieves the parents of this command. - - If the command has no parents then it returns an empty :class:`tuple`. - - For example in commands ``/a b test``, the parents are ``(b, a)``. - - .. versionadded:: 2.6 - """ - entries = [] - command = self - while command.parent is not None: # type: ignore - command = command.parent # type: ignore - entries.append(command) - - return tuple(entries) - async def _call_autocompleter( self, param: str, inter: ApplicationCommandInteraction, user_input: str ) -> Optional[Choices]: @@ -560,6 +529,13 @@ def parents(self) -> Tuple[()]: """ return () + @property + def parent(self) -> None: + """``None``: This is for consistency with :class:`SubCommand` and :class:`SubCommandGroup`. + + .. versionadded:: 2.6 + """ + def _ensure_assignment_on_copy(self, other: SlashCommandT) -> SlashCommandT: super()._ensure_assignment_on_copy(other) if self.connectors != other.connectors: diff --git a/test_bot/__main__.py b/test_bot/__main__.py index 5e084fdcb3..3e4e7babae 100644 --- a/test_bot/__main__.py +++ b/test_bot/__main__.py @@ -32,7 +32,7 @@ def __init__(self): command_prefix=Config.prefix, intents=disnake.Intents.all(), help_command=None, # type: ignore - sync_commands_debug=Config.sync_commands_debug, + command_sync=commands.ApplicationCommandSyncFlags.all(), strict_localization=Config.strict_localization, test_guilds=Config.test_guilds, reload=Config.auto_reload,