Skip to content

Commit

Permalink
Add support for mappings in choices
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Dec 29, 2023
1 parent cde0b1c commit 41012a7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
11 changes: 7 additions & 4 deletions arc/command/option/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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__(
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions arc/command/option/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__(
Expand Down
4 changes: 2 additions & 2 deletions arc/command/option/int.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__(
Expand Down
4 changes: 2 additions & 2 deletions arc/command/option/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__(
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41012a7

Please sign in to comment.