diff --git a/discord/abc.py b/discord/abc.py index 5f33a65b543d..576cab335ecd 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -2529,6 +2529,42 @@ def search( most_relevant=most_relevant, ) + async def user_application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: + """|coro| + + Returns a list of user-installed commands available. + + .. versionadded:: 2.1 + + .. note:: + + Method returns only user-installed commands. + + Raises + ------ + ~discord.HTTPException + Getting the commands failed. + + Returns + ------- + List[Union[:class:`~discord.SlashCommand`, :class:`~discord.UserCommand`, :class:`~discord.MessageCommand`]] + A list of user-installed commands. + """ + state = self._state + data = await state.http.user_application_command_index() + + cmds = data['application_commands'] + apps = {int(app['id']): state.create_integration_application(app) for app in data.get('applications') or []} + + result = [] + + for cmd in cmds: + _, cls = _command_factory(cmd['type']) + application = apps.get(int(cmd['application_id'])) + result.append(cls(state=state, data=cmd, application=application)) + + return result + async def application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: """|coro| diff --git a/discord/client.py b/discord/client.py index 94e9a20326db..e9531ea65889 100644 --- a/discord/client.py +++ b/discord/client.py @@ -100,6 +100,7 @@ from .affinity import * from .oauth2 import OAuth2Authorization, OAuth2Token from .experiment import UserExperiment, GuildExperiment +from .commands import SlashCommand, UserCommand, MessageCommand, _command_factory if TYPE_CHECKING: from typing_extensions import Self @@ -859,6 +860,42 @@ async def _async_setup_hook(self) -> None: self._ready = asyncio.Event() + async def user_application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: + """|coro| + + Returns a list of user-installed commands available. + + .. versionadded:: 2.1 + + .. note:: + + Method returns only user-installed commands. + + Raises + ------ + ~discord.HTTPException + Getting the commands failed. + + Returns + ------- + List[Union[:class:`~discord.SlashCommand`, :class:`~discord.UserCommand`, :class:`~discord.MessageCommand`]] + A list of user-installed commands. + """ + state = self._connection + data = await state.http.user_application_command_index() + + cmds = data['application_commands'] + apps = {int(app['id']): state.create_integration_application(app) for app in data.get('applications') or []} + + result = [] + + for cmd in cmds: + _, cls = _command_factory(cmd['type']) + application = apps.get(int(cmd['application_id'])) + result.append(cls(state=state, data=cmd, application=application)) + + return result + async def setup_hook(self) -> None: """|coro| diff --git a/discord/commands.py b/discord/commands.py index 6b56095e4f59..58c062fb1a08 100644 --- a/discord/commands.py +++ b/discord/commands.py @@ -224,6 +224,8 @@ def __init__( self.application_id: int = int(data['application_id']) self.id: int = int(data['id']) self.version = int(data['version']) + self.integration_types = data.get('integration_types', []) + self.global_popularity_rank = data.get('global_popularity_rank', 0) self._default_member_permissions = _get_as_snowflake(data, 'default_member_permissions') dm_permission = data.get('dm_permission') # Null means true? diff --git a/discord/guild.py b/discord/guild.py index 741345e32547..29eb4d1f9402 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -3361,6 +3361,42 @@ def convert(d): return [convert(d) for d in data] + async def user_application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: + """|coro| + + Returns a list of user-installed commands available. + + .. versionadded:: 2.1 + + .. note:: + + Method returns only user-installed commands. + + Raises + ------ + ~discord.HTTPException + Getting the commands failed. + + Returns + ------- + List[Union[:class:`~discord.SlashCommand`, :class:`~discord.UserCommand`, :class:`~discord.MessageCommand`]] + A list of user-installed commands. + """ + state = self._state + data = await state.http.user_application_command_index() + + cmds = data['application_commands'] + apps = {int(app['id']): state.create_integration_application(app) for app in data.get('applications') or []} + + result = [] + + for cmd in cmds: + _, cls = _command_factory(cmd['type']) + application = apps.get(int(cmd['application_id'])) + result.append(cls(state=state, data=cmd, application=application)) + + return result + async def application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: """|coro|