From bf6c9181ed43f568713c58afef5905f7ea8597c2 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Fri, 10 Jun 2022 16:59:24 +0300 Subject: [PATCH] feat(interactions): add ApplicationCommandOption --- .flake8 | 2 +- melisa/models/interactions/commands.py | 90 ++++++++++++++++++++++++++ setup.py | 3 +- 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index 9f510d9..baeb85a 100644 --- a/.flake8 +++ b/.flake8 @@ -2,4 +2,4 @@ ignore = D203, H501, F403, F401, C901, W503 exclude = .git, .idea, __pycache__, docs, build, dev, dist, venv, .history, .github, examples max-complexity = 10 -max-line-length = 101 \ No newline at end of file +max-line-length = 120 \ No newline at end of file diff --git a/melisa/models/interactions/commands.py b/melisa/models/interactions/commands.py index 0e28bf4..71a5c8e 100644 --- a/melisa/models/interactions/commands.py +++ b/melisa/models/interactions/commands.py @@ -6,6 +6,7 @@ from __future__ import annotations from enum import IntEnum from typing import Optional, Dict, Union, Any, List +from ..guild.channel import ChannelType from ...utils.conversion import try_enum from ...utils.api_model import APIModelBase @@ -76,6 +77,95 @@ class ApplicationCommandOptionTypes(IntEnum): return self.value +class ApplicationCommandOption(APIModelBase): + """Application Command Option + + .. warning:: + + Required ``options`` must be listed before optional options. + + Attributes + ---------- + type: :class:`~melisa.models.interactions.commands.ApplicationCommandOptionTypes` + Type of option + name: str + 1-32 character name + name_localizations: Dict[str, str] + Localization dictionary for the ``name`` field. + 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] + If the parameter is required or optional--default false + choices: Optional[List[:class:`~melisa.models.interactions.commands.ApplicationCommandOptionChoice`]] + Choices for ``STRING``, ``INTEGER``, and ``NUMBER`` + types for the user to pick from, max 25 + options: Optional[List[ApplicationCommandOption]] + If the option is a subcommand or subcommand group type, + these nested options will be the parameters + channel_types: Optional[List[:class:`~melisa.models.guild.channel.ChannelType`]] + If the option is a channel type, + the channels shown will be restricted to these types + min_value: Optional[int, float] + If the option is an ``int`` or ``float`` type, + the minimum value permitted + max_value: Optional[int, float] + If the option is an ``int`` or ``float`` type, + the maximum value permitted + autocomplete: Optional[bool] + If autocomplete interactions are enabled for this ``str``, + ``int``, or ``float`` type option + """ + + type: ApplicationCommandOptionTypes = None + name: str = None + name_localizations: Dict[str, str] = None + description: str + description_localizations: Dict[str, str] = None + required: Optional[bool] = False + choices: Optional[List[ApplicationCommandOptionChoice]] = None + options: Optional[List[ApplicationCommandOption]] = None + channel_types: Optional[List[ChannelType]] = None + min_value: Optional[int, float] = None + max_value: Optional[int, float] = None + autocomplete: Optional[bool] = False + + @classmethod + def from_dict(cls, data: Dict[str, Any]): + """Generate a ApplicationCommandOption from the given data. + + Parameters + ---------- + data: :class:`dict` + The dictionary to convert into a ApplicationCommandOption. + """ + self: ApplicationCommandOption = super().__new__(cls) + + self.type = try_enum(ApplicationCommandOptionTypes, data.get("type", 0)) + 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.required = data.get("required", False) + self.choices = [ + try_enum(ApplicationCommandOptionChoice, x) for x in data.get("choices", []) + ] + self.options = [ + try_enum(ApplicationCommandOption, x) for x in data.get("options", []) + ] + 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') + + return self + + class ApplicationCommandOptionChoice(APIModelBase): """Application Command Option Choice diff --git a/setup.py b/setup.py index b2bb890..07b2e88 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,8 @@ HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text(encoding="utf8") with open(HERE / "melisa/__init__.py") as file: - version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', file.read(), re.MULTILINE).group(1) + version = re.search( + r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', file.read(), re.MULTILINE).group(1) setuptools.setup( name='melisa',