Skip to content

Commit

Permalink
autocomplete functional
Browse files Browse the repository at this point in the history
  • Loading branch information
tandemdude committed Feb 20, 2024
1 parent 6944424 commit 8103d2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lightbulb/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __new__(cls, cls_name: str, bases: t.Tuple[type, ...], attrs: t.Dict[str, t.
# Iterate through new class attributes to find options and invoke method
for name, item in attrs.items():
if cls._is_option(item):
options[name] = item._data
options[item._data.name] = item._data
elif hasattr(item, constants.COMMAND_INVOKE_METHOD_MARKER):
invoke_method = name

Expand Down
19 changes: 17 additions & 2 deletions lightbulb/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
t.Sequence[t.Tuple[str, str]],
t.Sequence[t.Tuple[str, int]],
t.Sequence[t.Tuple[str, float]],
t.Sequence[str],
t.Sequence[int],
t.Sequence[float],
t.Mapping[str, str],
t.Mapping[str, int],
t.Mapping[str, float],
Expand All @@ -63,7 +66,7 @@ class AutocompleteContext:

_focused: t.Optional[hikari.AutocompleteInteractionOption] = dataclasses.field(init=False, default=None)

@functools.cached_property
@property
def focused(self) -> hikari.AutocompleteInteractionOption:
"""The focused option for the autocomplete interaction."""
if self._focused is not None:
Expand All @@ -80,10 +83,22 @@ def _normalise_choices(choices: AutocompleteResponseT) -> t.Sequence[special_end
return [hikari.impl.AutocompleteChoiceBuilder(name=k, value=v) for k, v in choices.items()]

def _to_command_choice(
item: t.Union[special_endpoints.AutocompleteChoiceBuilder, t.Tuple[str, t.Union[str, int, float]]],
item: t.Union[
special_endpoints.AutocompleteChoiceBuilder,
t.Tuple[str, str],
t.Tuple[str, int],
t.Tuple[str, float],
str,
int,
float,
],
) -> special_endpoints.AutocompleteChoiceBuilder:
if isinstance(item, special_endpoints.AutocompleteChoiceBuilder):
return item

if isinstance(item, (str, int, float)):
return hikari.impl.AutocompleteChoiceBuilder(name=str(item), value=item)

return hikari.impl.AutocompleteChoiceBuilder(name=item[0], value=item[1])

assert not isinstance(choices, collections.abc.Mapping)
Expand Down

0 comments on commit 8103d2b

Please sign in to comment.