From eb6a198cab737c3e2fb097feaadff1f7a6934a03 Mon Sep 17 00:00:00 2001 From: Victor Kotlin Date: Tue, 14 Jun 2022 16:16:47 +0300 Subject: [PATCH] feat(RESTApp): add get_global_application_commands() method --- melisa/core/http.py | 13 +++++++-- melisa/models/guild/guild.py | 2 +- melisa/models/message/colors.py | 17 ----------- melisa/rest.py | 50 +++++++++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/melisa/core/http.py b/melisa/core/http.py index f77a55b..08d17e6 100644 --- a/melisa/core/http.py +++ b/melisa/core/http.py @@ -184,7 +184,12 @@ class HTTPClient: return await self.__send("GET", route, params=params) async def post( - self, route: str, *, headers: dict = None, data: Optional[Dict] = None + self, + route: str, + *, + headers: dict = None, + json: Optional[Dict] = None, + data=None, ) -> Optional[Dict]: """|coro| Sends a POST request to a Discord REST API endpoint. @@ -193,7 +198,9 @@ class HTTPClient: ---------- route : :class:`str` The endpoint to send the request to. - data : Dict + json : Dict + Json data to post + data : Any Data to post headers : :class:`dict` Custom request headers @@ -203,7 +210,7 @@ class HTTPClient: Optional[:class:`Dict`] JSON response from the Discord API. """ - return await self.__send("POST", route, json=data, headers=headers) + return await self.__send("POST", route, json=json, data=data, headers=headers) async def delete(self, route: str, *, headers: dict = None) -> Optional[Dict]: """|coro| diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index 6b36de3..1b46993 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -470,7 +470,7 @@ class Guild(APIModelBase): for channel in data.get("channels", []): channel = _choose_channel_type(channel) - self.channels[Snowflake(int(channel.id))] = channel + self.channels[str(channel.id)] = channel for role in data.get("roles", []): role["guild_id"] = self.id diff --git a/melisa/models/message/colors.py b/melisa/models/message/colors.py index 0c4d9b5..7dde2c1 100644 --- a/melisa/models/message/colors.py +++ b/melisa/models/message/colors.py @@ -137,20 +137,3 @@ class Color: def default(cls: typing.Type[CT]) -> CT: """A factory method that returns a :class:`Colour` with a value of ``0``.""" return cls(0) - - @classmethod - def random( - cls: typing.Type[CT], - *, - seed: typing.Optional[typing.Union[int, str, float, bytes, bytearray]] = None, - ) -> CT: - """A factory method that returns a :class:`Colour` with a random hue. - - Parameters - ---------- - seed: Optional[Union[:class:`int`, :class:`str`, - :class:`float`, :class:`bytes`, :class:`bytearray`]] - The seed to initialize the RNG with. If ``None`` is passed the default RNG is used. - """ - rand = random if seed is None else random.Random(seed) - return cls.from_hsv(rand.random(), 1, 1) diff --git a/melisa/rest.py b/melisa/rest.py index b48b6e4..c704d95 100644 --- a/melisa/rest.py +++ b/melisa/rest.py @@ -197,19 +197,22 @@ class RESTApp: # ToDo: add file checks if embeds is None: - embeds = [embed.to_dict()] if embed is not None else [] + embeds = [embed] if embed is not None else [] if files is None: files = [file] if file is not None else [] - payload = {"content": str(content) if content is not None else None} + payload = { + "content": str(content) if content is not None else None, + "embeds": [], + } for _embed in embeds: - if embed.total_length() > 6000: + if _embed.total_length() > 6000: raise EmbedFieldError.characters_from_desc( "Embed", embed.total_length(), 6000 ) + payload["embeds"].append(_embed.to_dict()) - payload["embeds"] = embeds payload["tts"] = tts # ToDo: add auto allowed_mentions from client @@ -663,6 +666,43 @@ class RESTApp: headers={"X-Audit-Log-Reason": reason}, ) + async def get_global_application_commands( + self, + application_id: Union[int, str, Snowflake], + *, + with_localizations: Optional[bool] = False, + ): + """|coro| + + [**REST API**] Fetch all of the global commands for your application. + + Parameters + ---------- + application_id: :class:`~melisa.utils.snowflake.Snowflake` + ID of the parent application + with_localizations: Optional[bool] + Whether to include full localization dictionaries + (``name_localizations`` and ``description_localizations``) in + the returned objects, instead of the ``name_localized` + and ``description_localized fields``. Default ``False``. + + 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 + """ + + return [ + ApplicationCommand.from_dict(x) + for x in await self._http.get( + f"/applications/{application_id}/commands?with_localizations={with_localizations}" + ) + ] + async def create_global_application_command( self, application_id: Union[int, str, Snowflake], @@ -747,7 +787,7 @@ class RESTApp: data["default_permission"] = default_permission return ApplicationCommand.from_dict( - await self._http.post(f"/applications/{application_id}/commands", data=data) + await self._http.post(f"/applications/{application_id}/commands", json=data) )