From 0802526adf29b0f2dbe63352f937a4e9fb1dc3c4 Mon Sep 17 00:00:00 2001 From: Earl <84588908+wooslow@users.noreply.github.com> Date: Tue, 11 Mar 2025 22:18:08 +0200 Subject: [PATCH 1/4] feat (abc.py): add user_installed_commands method to retrieve user-installed commands --- discord/abc.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/discord/abc.py b/discord/abc.py index 5f33a65b543d..8bbd0734b073 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -2529,6 +2529,38 @@ def search( most_relevant=most_relevant, ) + async def user_installed_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: + """|coro| + + Returns a list of user-installed commands available. + + .. versionadded:: 2.1 + + 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| From 45087d60eb24035f0fadb6b722f96648db4643ff Mon Sep 17 00:00:00 2001 From: wooslow <84588908+wooslow@users.noreply.github.com> Date: Tue, 11 Mar 2025 22:45:58 +0200 Subject: [PATCH 2/4] refactor: re-name method Co-authored-by: dolfies --- discord/abc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/abc.py b/discord/abc.py index 8bbd0734b073..156f296df922 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -2529,7 +2529,7 @@ def search( most_relevant=most_relevant, ) - async def user_installed_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: + async def user_application_commands(self) -> List[Union[SlashCommand, UserCommand, MessageCommand]]: """|coro| Returns a list of user-installed commands available. From 1c0c531667b02b93c684442b4b742bb2b34a325f Mon Sep 17 00:00:00 2001 From: Earl <84588908+wooslow@users.noreply.github.com> Date: Tue, 11 Mar 2025 22:55:01 +0200 Subject: [PATCH 3/4] feat(guild.py/client.py): add method user_application_commands --- discord/abc.py | 4 ++++ discord/client.py | 37 +++++++++++++++++++++++++++++++++++++ discord/guild.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/discord/abc.py b/discord/abc.py index 156f296df922..576cab335ecd 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -2536,6 +2536,10 @@ async def user_application_commands(self) -> List[Union[SlashCommand, UserComman .. versionadded:: 2.1 + .. note:: + + Method returns only user-installed commands. + Raises ------ ~discord.HTTPException 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/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| From 63a780ca9b6626fffb01312bfeca8b358321af6b Mon Sep 17 00:00:00 2001 From: Earl <84588908+wooslow@users.noreply.github.com> Date: Tue, 11 Mar 2025 23:16:31 +0200 Subject: [PATCH 4/4] feat: add integration_types and global_popularity_rank fields --- discord/commands.py | 2 ++ 1 file changed, 2 insertions(+) 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?