From 06d4853a38d380b85ca6442d967422b75c3105ed Mon Sep 17 00:00:00 2001 From: Victor Kotlin Date: Mon, 13 Jun 2022 20:02:22 +0300 Subject: [PATCH] feat(RESTApi): add create_global_application_command() --- melisa/core/gateway.py | 4 +- melisa/core/http.py | 2 +- melisa/models/interactions/commands.py | 53 ++++++++------- melisa/rest.py | 89 ++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 28 deletions(-) diff --git a/melisa/core/gateway.py b/melisa/core/gateway.py index 5758291..f094adb 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -85,9 +85,7 @@ class Gateway: "intents": self.intents, "properties": { "$os": sys.platform, - "$browser": "Discord iOS" - if kwargs.get("mobile") is True - else "melisa", + "$browser": "Discord iOS" if kwargs.get("mobile") is True else "melisa", "$device": "melisa", }, "compress": True, diff --git a/melisa/core/http.py b/melisa/core/http.py index 359054f..f77a55b 100644 --- a/melisa/core/http.py +++ b/melisa/core/http.py @@ -203,7 +203,7 @@ class HTTPClient: Optional[:class:`Dict`] JSON response from the Discord API. """ - return await self.__send("POST", route, data=data, headers=headers) + return await self.__send("POST", route, json=data, headers=headers) async def delete(self, route: str, *, headers: dict = None) -> Optional[Dict]: """|coro| diff --git a/melisa/models/interactions/commands.py b/melisa/models/interactions/commands.py index 2cdfa60..1b58ef4 100644 --- a/melisa/models/interactions/commands.py +++ b/melisa/models/interactions/commands.py @@ -88,7 +88,7 @@ class ApplicationCommand(APIModelBase): type: Optional[:class:`~melisa.interactions.commands.ApplicationCommandTypes`] Type of command, defaults to ``1`` application_id: :class:`~melisa.utils.snowflake.Snowflake` - ID of the parent application + ID of the parent application guild_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`] guild id of the command, if not global name: str @@ -97,14 +97,14 @@ class ApplicationCommand(APIModelBase): Localization dictionary for ``name`` field. Values follow the same restrictions as ``name`` description: str - Description for ``CHAT_INPUT`` commands, 1-100 characters. - Empty string for ``USER`` and ``MESSAGE`` commands + Description for ``CHAT_INPUT`` commands, 1-100 characters. + Empty string for ``USER`` and ``MESSAGE`` commands description_localizations: Optional[Dict[str, str]] Localization dictionary for ``description`` field. Values follow the same restrictions as ``description`` options: Optional[List[:class:`~melisa.models.interactions.commands.ApplicationCommandOption`]] - Parameters for the command, max of 25. - Only available for ``CHAT_INPUT`` command type. + Parameters for the command, max of 25. + Only available for ``CHAT_INPUT`` command type. default_member_permissions: Optional[str] Set of permissions represented as a bit set dm_permission: Optional[bool] @@ -112,12 +112,13 @@ class ApplicationCommand(APIModelBase): in DMs with the app, only for globally-scoped commands. By default, commands are visible. default_permission: Optional[bool] - Not recommended for use as field will soon be deprecated. - Indicates whether the command is enabled by default - when the app is added to a guild, defaults to true + Not recommended for use as field will soon be deprecated. + Indicates whether the command is enabled by default + when the app is added to a guild, defaults to true version: :class:`~melisa.utils.snowflake.Snowflake` Autoincrementing version identifier updated during substantial record changes """ + # ToDo: Better Permissions id: Snowflake = None @@ -145,19 +146,23 @@ class ApplicationCommand(APIModelBase): """ self: ApplicationCommand = super().__new__(cls) - self.id = Snowflake(data.get('id', 0)) - self.type = data.get('type', 1) - self.application_id = Snowflake(data.get('application_id')) - self.guild_id = Snowflake(data['guild_id']) if data.get('guild_id') is not None else None - self.name = data.get('name') - self.name_localizations = data.get('name_localizations') - self.description = data.get('description') - self.description_localizations = data.get('description_localizations') - self.options = [ApplicationCommandOption.from_dict(x) for x in data.get('options')] - self.default_member_permissions = data.get('default_member_permissions') - self.dm_permission = data.get('dm_permission', True) - self.default_permission = data.get('default_permission', True) - self.version = Snowflake(data.get('version', 0)) + self.id = Snowflake(data.get("id", 0)) + self.type = data.get("type", 1) + self.application_id = Snowflake(data.get("application_id")) + self.guild_id = ( + Snowflake(data["guild_id"]) if data.get("guild_id") is not None else None + ) + self.name = data.get("name") + self.name_localizations = data.get("name_localizations") + self.description = data.get("description") + self.description_localizations = data.get("description_localizations") + self.options = [ + ApplicationCommandOption.from_dict(x) for x in data.get("options", []) + ] + self.default_member_permissions = data.get("default_member_permissions") + self.dm_permission = data.get("dm_permission", True) + self.default_permission = data.get("default_permission", True) + self.version = Snowflake(data.get("version", 0)) return self @@ -244,9 +249,9 @@ class ApplicationCommandOption(APIModelBase): self.channel_types = [ try_enum(ChannelType, x) for x in data.get("channel_types", []) ] - self.min_value = data.get('min_value') - self.max_value = data.get('max_value') - self.autocomplete = data.get('autocomplete') + self.min_value = data.get("min_value") + self.max_value = data.get("max_value") + self.autocomplete = data.get("autocomplete") return self diff --git a/melisa/rest.py b/melisa/rest.py index d7aa08f..b48b6e4 100644 --- a/melisa/rest.py +++ b/melisa/rest.py @@ -6,6 +6,8 @@ from typing import Union, Optional, List, Dict, Any, AsyncIterator from aiohttp import FormData +from .models.interactions import ApplicationCommandTypes +from .models.interactions.commands import ApplicationCommandOption, ApplicationCommand from .models.message import Embed, File, AllowedMentions, Message from .exceptions import EmbedFieldError from .core.http import HTTPClient @@ -661,6 +663,93 @@ class RESTApp: headers={"X-Audit-Log-Reason": reason}, ) + async def create_global_application_command( + self, + application_id: Union[int, str, Snowflake], + command_type: ApplicationCommandTypes, + name: str, + description: str = None, + *, + name_localizations: Optional[Dict[str, str]] = None, + description_localizations: Optional[Dict[str, str]] = None, + options: Optional[List[ApplicationCommandOption]] = None, + default_member_permissions: Optional[str] = None, + dm_permission: Optional[bool] = None, + default_permission: Optional[bool] = None, + ): + """|coro| + + [**REST API**] Create a new global command. + + Parameters + ---------- + application_id: :class:`~melisa.utils.snowflake.Snowflake` + ID of the parent application + command_type: Optional[:class:`~melisa.interactions.commands.ApplicationCommandTypes`] + Type of command, defaults to ``1`` + name: str + Name of command, 1-32 characters + description: str + Description for ``CHAT_INPUT`` commands, 1-100 characters. + Empty string for ``USER`` and ``MESSAGE`` commands + name_localizations: Optional[Dict[str, str]] + Localization dictionary for ``name`` field. + Values follow the same restrictions as ``name`` + description_localizations: Optional[Dict[str, str]] + Localization dictionary for ``description`` field. + Values follow the same restrictions as ``description`` + options: Optional[List[:class:`~melisa.models.interactions.commands.ApplicationCommandOption`]] + Parameters for the command, max of 25. + Only available for ``CHAT_INPUT`` command type. + default_member_permissions: Optional[str] + Set of permissions represented as a bit set + dm_permission: Optional[bool] + Indicates whether the command is available + in DMs with the app, only for globally-scoped commands. + By default, commands are visible. + default_permission: Optional[bool] + Not recommended for use as field will soon be deprecated. + Indicates whether the command is enabled by default + when the app is added to a guild, defaults to true + + Raises + ------- + HTTPException + The request to perform the action failed with other http exception. + ForbiddenError + You do not have proper permissions to do the actions required. + BadRequestError + You provided a wrong arguments + """ + + data = { + "name": name, + "description": description, + "type": int(command_type), + } + + if name_localizations is not None: + data["name_localizations"] = name_localizations + + if description_localizations is not None: + data["description_localizations"] = description_localizations + + if default_member_permissions is not None: + data["default_member_permissions"] = default_member_permissions + + if options is not None: + data["options"] = [x.to_dict() for x in options] + + if dm_permission is not None: + data["dm_permission"] = dm_permission + + if default_permission is not None: + data["default_permission"] = default_permission + + return ApplicationCommand.from_dict( + await self._http.post(f"/applications/{application_id}/commands", data=data) + ) + class CDNBuilder: """Can be used to build images