feat(interactions): Options localization

This commit is contained in:
grey-cat-1908 2022-07-30 18:57:50 +03:00
parent 9b1d710bce
commit b6bf4dc1a8
3 changed files with 55 additions and 35 deletions

View file

@ -173,8 +173,11 @@ class SlashCommand(PartialApplicationCommand):
self.guild_id = ( self.guild_id = (
Snowflake(data["guild_id"]) if data.get("guild_id") is not None else None 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") name = data.get("name")
name_localizations = data.get("name_localizations")
self.name = LocalizedField(name, name_localizations)
description = data.get("description") description = data.get("description")
description_localizations = data.get("description_localizations") description_localizations = data.get("description_localizations")
@ -202,16 +205,10 @@ class SlashCommandOption(APIModelBase):
---------- ----------
type: :class:`~melisa.models.interactions.commands.SlashCommandOptionType` type: :class:`~melisa.models.interactions.commands.SlashCommandOptionType`
Type of option Type of option
name: str name: :class:`~melisa.models.interactions.i18n.LocalizedField`
1-32 character name Name of option
name_localizations: Dict[str, str] description: :class:`~melisa.models.interactions.i18n.LocalizedField`
Localization dictionary for the ``name`` field. Description of option
Values follow the same restrictions as ``name``
description: str
1-100 character description
description_localizations: Dict[str, str]
Localization dictionary for the ``description`` field.
Values follow the same restrictions as ``description``
required: Optional[bool] required: Optional[bool]
If the parameter is required or optional--default false If the parameter is required or optional--default false
choices: Optional[List[:class:`~melisa.models.interactions.commands.SlashCommandOptionChoice`]] choices: Optional[List[:class:`~melisa.models.interactions.commands.SlashCommandOptionChoice`]]
@ -235,10 +232,8 @@ class SlashCommandOption(APIModelBase):
""" """
type: SlashCommandOptionType = None type: SlashCommandOptionType = None
name: str = None name: LocalizedField = None
name_localizations: Dict[str, str] = None description: LocalizedField = None
description: str
description_localizations: Dict[str, str] = None
required: Optional[bool] = False required: Optional[bool] = False
choices: Optional[List[SlashCommandOptionChoice]] = None choices: Optional[List[SlashCommandOptionChoice]] = None
options: Optional[List[SlashCommandOption]] = None options: Optional[List[SlashCommandOption]] = None
@ -259,10 +254,17 @@ class SlashCommandOption(APIModelBase):
self: SlashCommandOption = super().__new__(cls) self: SlashCommandOption = super().__new__(cls)
self.type = try_enum(SlashCommandOptionType, data.get("type", 0)) self.type = try_enum(SlashCommandOptionType, data.get("type", 0))
self.name = data.get("name")
self.name_localizations = data.get("name_localizations") name = data.get("name")
self.description = data.get("description") name_localizations = data.get("name_localizations")
self.description_localizations = data.get("description_localizations")
self.name = LocalizedField(name, name_localizations)
description = data.get("description")
description_localizations = data.get("description_localizations")
self.description = LocalizedField(description, description_localizations)
self.required = data.get("required", False) self.required = data.get("required", False)
self.choices = [ self.choices = [
try_enum(SlashCommandOptionChoice, x) for x in data.get("choices", []) try_enum(SlashCommandOptionChoice, x) for x in data.get("choices", [])
@ -288,21 +290,13 @@ class SlashCommandOptionChoice(APIModelBase):
Attributes Attributes
---------- ----------
name: :class:`str` name: :class:`~melisa.models.interactions.i18n.LocalizedField`
1-100 character choice name Name of option choice
name_localizations: Optional[Dict[:class:`str`, :class:`str`]]
Dictionary with keys in
`available locales <https://discord.com/developers/docs/reference#locales>`_
Localization dictionary for the name field.
Values follow the same restrictions as name
value: Union[:class:`str`, :class:`int`, :class:`float`] value: Union[:class:`str`, :class:`int`, :class:`float`]
Value for the choice, up to 100 characters if string Value for the choice, up to 100 characters if string
""" """
name: str = None name: LocalizedField = None
name_localizations: Optional[Dict[str, str]] = None
value: Union[str, int, float] = None value: Union[str, int, float] = None
@classmethod @classmethod
@ -316,8 +310,11 @@ class SlashCommandOptionChoice(APIModelBase):
""" """
self: SlashCommandOptionChoice = super().__new__(cls) self: SlashCommandOptionChoice = super().__new__(cls)
self.name = data.get("name") name = data.get("name")
self.name_localizations = data.get("name_localizations") name_localizations = data.get("name_localizations")
self.name = LocalizedField(name, name_localizations)
self.value = data.get("value") self.value = data.get("value")
return self return self

View file

@ -7,8 +7,22 @@ from typing import Dict
class LocalizedField: class LocalizedField:
"""
Represents a field, that could be localized
original: str original: str
localizations: str Value of non-localized field
localizations: Optional[Dict[str, str]]
Dictionary with keys in
`available locales <https://discord.com/developers/docs/reference#locales>`_
Localization dictionary for the name field.
Values follow the same restrictions as name
"""
original: str
localizations: Optional[Dict[str, str]]
def __init__( def __init__(
self, self,

View file

@ -957,7 +957,16 @@ class RESTApp:
data["default_member_permissions"] = default_member_permissions data["default_member_permissions"] = default_member_permissions
if options is not None: if options is not None:
data["options"] = [x.to_dict() for x in options] for option in options:
option_data = option.to_dict()
option_data["name"] = option.name.original
option_data["description"] = option.description.original
if option.name.localizations is not None:
option_data["name_localizations"] = option.name.localizations
if option.description.localizations is not None:
option_data["description_localizations"] = option.description.localizations
if dm_permission is not None: if dm_permission is not None:
data["dm_permission"] = dm_permission data["dm_permission"] = dm_permission