feat(RESTApi): add create_global_application_command()

This commit is contained in:
Victor Kotlin 2022-06-13 20:02:22 +03:00
parent 53acd07401
commit 06d4853a38
4 changed files with 120 additions and 28 deletions

View file

@ -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,

View file

@ -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|

View file

@ -118,6 +118,7 @@ class ApplicationCommand(APIModelBase):
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

View file

@ -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