Skip to content

Commit

Permalink
Move MissingApplicationID to top-level discord namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Aug 31, 2024
1 parent 66d7405 commit df4b1c8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 26 deletions.
6 changes: 6 additions & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ class VersionInfo(NamedTuple):

logging.getLogger(__name__).addHandler(logging.NullHandler())

# This is a backwards compatibility hack and should be removed in v3
# Essentially forcing the exception to have different base classes
# In the future, this should only inherit from ClientException
if len(MissingApplicationID.__bases__) == 1:
MissingApplicationID.__bases__ = (app_commands.AppCommandError, ClientException)

del logging, NamedTuple, Literal, VersionInfo
20 changes: 1 addition & 19 deletions discord/app_commands/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing import Any, TYPE_CHECKING, List, Optional, Sequence, Union

from ..enums import AppCommandOptionType, AppCommandType, Locale
from ..errors import DiscordException, HTTPException, _flatten_error_dict
from ..errors import DiscordException, HTTPException, _flatten_error_dict, MissingApplicationID as MissingApplicationID
from ..utils import _human_join

__all__ = (
Expand Down Expand Up @@ -59,11 +59,6 @@

CommandTypes = Union[Command[Any, ..., Any], Group, ContextMenu]

APP_ID_NOT_FOUND = (
'Client does not have an application_id set. Either the function was called before on_ready '
'was called or application_id was not passed to the Client constructor.'
)


class AppCommandError(DiscordException):
"""The base exception type for all application command related errors.
Expand Down Expand Up @@ -422,19 +417,6 @@ def __init__(self, command: Union[Command[Any, ..., Any], ContextMenu, Group]):
super().__init__(msg)


class MissingApplicationID(AppCommandError):
"""An exception raised when the client does not have an application ID set.
An application ID is required for syncing application commands.
This inherits from :exc:`~discord.app_commands.AppCommandError`.
.. versionadded:: 2.0
"""

def __init__(self, message: Optional[str] = None):
super().__init__(message or APP_ID_NOT_FOUND)


def _get_command_error(
index: str,
inner: Any,
Expand Down
2 changes: 1 addition & 1 deletion discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
from typing_extensions import Self

from .abc import Messageable, PrivateChannel, Snowflake, SnowflakeTime
from .app_commands import Command, ContextMenu, MissingApplicationID
from .app_commands import Command, ContextMenu
from .automod import AutoModAction, AutoModRule
from .channel import DMChannel, GroupChannel
from .ext.commands import AutoShardedBot, Bot, Context, CommandError
Expand Down
2 changes: 1 addition & 1 deletion discord/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .utils import SnowflakeList, snowflake_time, MISSING
from .partial_emoji import _EmojiTag, PartialEmoji
from .user import User
from .app_commands.errors import MissingApplicationID
from .errors import MissingApplicationID
from .object import Object

# fmt: off
Expand Down
25 changes: 25 additions & 0 deletions discord/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
'ConnectionClosed',
'PrivilegedIntentsRequired',
'InteractionResponded',
'MissingApplicationID',
)

APP_ID_NOT_FOUND = (
'Client does not have an application_id set. Either the function was called before on_ready '
'was called or application_id was not passed to the Client constructor.'
)


Expand Down Expand Up @@ -278,3 +284,22 @@ class InteractionResponded(ClientException):
def __init__(self, interaction: Interaction):
self.interaction: Interaction = interaction
super().__init__('This interaction has already been responded to before')


class MissingApplicationID(ClientException):
"""An exception raised when the client does not have an application ID set.
An application ID is required for syncing application commands and various
other application tasks such as SKUs or application emojis.
This inherits from :exc:`~discord.app_commands.AppCommandError`
and :class:`~discord.ClientException`.
.. versionadded:: 2.0
.. versionchanged:: 2.5
This is now exported to the ``discord`` namespace and now inherits from :class:`~discord.ClientException`.
"""

def __init__(self, message: Optional[str] = None):
super().__init__(message or APP_ID_NOT_FOUND)
2 changes: 1 addition & 1 deletion discord/sku.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from typing import Optional, TYPE_CHECKING

from . import utils
from .app_commands import MissingApplicationID
from .errors import MissingApplicationID
from .enums import try_enum, SKUType, EntitlementType
from .flags import SKUFlags

Expand Down
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5440,6 +5440,8 @@ The following exceptions are thrown by the library.

.. autoexception:: InteractionResponded

.. autoexception:: MissingApplicationID

.. autoexception:: discord.opus.OpusError

.. autoexception:: discord.opus.OpusNotLoaded
Expand All @@ -5457,6 +5459,7 @@ Exception Hierarchy
- :exc:`ConnectionClosed`
- :exc:`PrivilegedIntentsRequired`
- :exc:`InteractionResponded`
- :exc:`MissingApplicationID`
- :exc:`GatewayNotFound`
- :exc:`HTTPException`
- :exc:`Forbidden`
Expand Down
5 changes: 1 addition & 4 deletions docs/interactions/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,6 @@ Exceptions
.. autoexception:: discord.app_commands.CommandNotFound
:members:

.. autoexception:: discord.app_commands.MissingApplicationID
:members:

.. autoexception:: discord.app_commands.CommandSyncFailure
:members:

Expand All @@ -899,7 +896,7 @@ Exception Hierarchy
- :exc:`~discord.app_commands.CommandAlreadyRegistered`
- :exc:`~discord.app_commands.CommandSignatureMismatch`
- :exc:`~discord.app_commands.CommandNotFound`
- :exc:`~discord.app_commands.MissingApplicationID`
- :exc:`~discord.MissingApplicationID`
- :exc:`~discord.app_commands.CommandSyncFailure`
- :exc:`~discord.HTTPException`
- :exc:`~discord.app_commands.CommandSyncFailure`

0 comments on commit df4b1c8

Please sign in to comment.