From 41012a7729e124ddea52f1ddd14bb43c3f9ef3a8 Mon Sep 17 00:00:00 2001 From: hypergonial <46067571+hypergonial@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:10:19 +0100 Subject: [PATCH] Add support for mappings in choices --- arc/command/option/base.py | 11 +++++++---- arc/command/option/float.py | 4 ++-- arc/command/option/int.py | 4 ++-- arc/command/option/str.py | 4 ++-- docs/changelog.md | 1 + 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/arc/command/option/base.py b/arc/command/option/base.py index 26e4c38..45524e2 100644 --- a/arc/command/option/base.py +++ b/arc/command/option/base.py @@ -89,7 +89,7 @@ class OptionWithChoicesParams(OptionParams[ChoiceT], t.Generic[ChoiceT, ClientT] The name of the option in different locales description_localizations : Mapping[hikari.Locale, str] The description of the option in different locales - choices : t.Sequence[ChoiceT | hikari.CommandChoice] | None + choices : t.Sequence[ChoiceT | hikari.CommandChoice] | t.Mapping[str, ChoiceT] | None The choices for the option. If provided, these will be the only valid values for the option. autocomplete_with : AutocompleteCallbackT[ClientT, ChoiceT] | None The callback for autocompleting the option. @@ -105,7 +105,7 @@ def __init__( *, name_localizations: t.Mapping[hikari.Locale, str] | None = None, description_localizations: t.Mapping[hikari.Locale, str] | None = None, - choices: t.Sequence[ChoiceT | hikari.CommandChoice] | None = None, + choices: t.Sequence[ChoiceT | hikari.CommandChoice] | t.Mapping[str, ChoiceT] | None = None, autocomplete_with: AutocompleteCallbackT[ClientT, ChoiceT] | None = None, ) -> None: super().__init__( @@ -118,7 +118,7 @@ def __init__( self._autocomplete_with = autocomplete_with @property - def choices(self) -> t.Sequence[ChoiceT | hikari.CommandChoice] | None: + def choices(self) -> t.Sequence[ChoiceT | hikari.CommandChoice] | t.Mapping[str, ChoiceT] | None: """The choices for the option. If provided, these will be the only valid values for the option.""" return self._choices @@ -194,7 +194,7 @@ def _to_dict(self) -> dict[str, Any]: class OptionWithChoices(CommandOptionBase[ChoiceT, ClientT, ParamsT]): """An option that can have choices or be autocompleted.""" - choices: t.Sequence[ChoiceT | hikari.CommandChoice] | None = None + choices: t.Sequence[ChoiceT | hikari.CommandChoice] | t.Mapping[str, ChoiceT] | None = None """The choices for the option.""" autocomplete_with: AutocompleteCallbackT[ClientT, ChoiceT] | None = None @@ -204,6 +204,9 @@ def _choices_to_command_choices(self) -> t.Sequence[hikari.CommandChoice] | None if self.choices is None: return None + if isinstance(self.choices, t.Mapping): + return [hikari.CommandChoice(name=str(name), value=value) for name, value in self.choices.items()] + return [ hikari.CommandChoice(name=str(choice), value=choice) if not isinstance(choice, hikari.CommandChoice) diff --git a/arc/command/option/float.py b/arc/command/option/float.py index c357c17..3e6e980 100644 --- a/arc/command/option/float.py +++ b/arc/command/option/float.py @@ -31,7 +31,7 @@ class FloatParams(OptionWithChoicesParams[float, ClientT]): The minimum value of the option max : float | None The maximum value of the option - choices : t.Sequence[int | hikari.CommandChoice] | None + choices : t.Sequence[float | hikari.CommandChoice] | t.Mapping[str, float] | None The choices for the option. If provided, these will be the only valid values for the option. autocomplete_with : AutocompleteCallbackT[ClientT, int] | None The callback that is invoked when the user autocompletes the option @@ -49,7 +49,7 @@ def __init__( description_localizations: t.Mapping[hikari.Locale, str] = {}, min: float | None = None, max: float | None = None, - choices: t.Sequence[float | hikari.CommandChoice] | None = None, + choices: t.Sequence[float | hikari.CommandChoice] | t.Mapping[str, float] | None = None, autocomplete_with: AutocompleteCallbackT[ClientT, float] | None = None, ) -> None: super().__init__( diff --git a/arc/command/option/int.py b/arc/command/option/int.py index 527d117..d885e75 100644 --- a/arc/command/option/int.py +++ b/arc/command/option/int.py @@ -31,7 +31,7 @@ class IntParams(OptionWithChoicesParams[int, ClientT]): The minimum value of the option max : int | None The maximum value of the option - choices : t.Sequence[int | hikari.CommandChoice] | None + choices : t.Sequence[int | hikari.CommandChoice] | t.Mapping[str, int] | None The choices for the option. If provided, these will be the only valid values for the option. autocomplete_with : AutocompleteCallbackT[ClientT, int] | None The callback that is invoked when the user autocompletes the option @@ -49,7 +49,7 @@ def __init__( description_localizations: t.Mapping[hikari.Locale, str] | None = None, min: int | None = None, max: int | None = None, - choices: t.Sequence[int | hikari.CommandChoice] | None = None, + choices: t.Sequence[int | hikari.CommandChoice] | t.Mapping[str, int] | None = None, autocomplete_with: AutocompleteCallbackT[ClientT, int] | None = None, ) -> None: super().__init__( diff --git a/arc/command/option/str.py b/arc/command/option/str.py index 6ebe36a..056c148 100644 --- a/arc/command/option/str.py +++ b/arc/command/option/str.py @@ -31,7 +31,7 @@ class StrParams(OptionWithChoicesParams[str, ClientT]): The minimum length of the option max_length : int | None The maximum length of the option - choices : t.Sequence[str | hikari.CommandChoice] | None + choices : t.Sequence[str | hikari.CommandChoice] | t.Mapping[str, str] | None The choices for the option. If provided, these will be the only valid values for the option. autocomplete_with : AutocompleteCallbackT[ClientT, str] | None The callback that is invoked when the user autocompletes the option @@ -49,7 +49,7 @@ def __init__( description_localizations: t.Mapping[hikari.Locale, str] | None = None, min_length: int | None = None, max_length: int | None = None, - choices: t.Sequence[str | hikari.CommandChoice] | None = None, + choices: t.Sequence[str | hikari.CommandChoice] | t.Mapping[str, str] | None = None, autocomplete_with: AutocompleteCallbackT[ClientT, str] | None = None, ) -> None: super().__init__( diff --git a/docs/changelog.md b/docs/changelog.md index 74fb303..1a81be4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -11,6 +11,7 @@ Here you can find all the changelogs for `hikari-arc`. ## v0.1.4 +- Add support for passing mappings to `choices=` when specifying option params. - Improve handling missing responses via REST by adding `NoResponseIssuedError`. ## v0.1.3