Skip to content

Commit ab50202

Browse files
committed
Compatibility with OptionChoices in autocomplete
1 parent d4f5576 commit ab50202

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

discord/commands/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ async def invoke_autocomplete_callback(self, ctx: AutocompleteContext):
565565
result = await result
566566

567567
choices = [
568-
o if isinstance(o, OptionChoice) else OptionChoice(o)
568+
o if isinstance(o, OptionChoice)
569+
else (OptionChoice(*o) if isinstance(o, tuple) else OptionChoice(o))
569570
for o in result
570571
][:25]
571572
return await ctx.interaction.response.send_autocomplete_result(choices=choices)

discord/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,26 @@ async def autocomplete_callback(ctx: AutocompleteContext) -> V:
11111111
if asyncio.iscoroutine(_values):
11121112
_values = await _values
11131113

1114-
gen = (val for val in _values if str(val).lower().startswith(str(ctx.value or "").lower()))
1114+
def check(item: Any) -> bool:
1115+
"""Comparator method
1116+
1117+
Parameters
1118+
----------
1119+
x : str
1120+
parameter
1121+
1122+
Returns
1123+
-------
1124+
bool
1125+
if valid
1126+
"""
1127+
if isinstance(item, tuple):
1128+
item, _ = item
1129+
if hasattr(item, "to_dict"):
1130+
item = item.to_dict()["name"]
1131+
return str(item).lower().startswith(str(ctx.value or "").lower())
1132+
1133+
gen = (val for val in _values if check(val))
11151134
return iter(itertools.islice(gen, 25))
11161135

11171136
return autocomplete_callback

0 commit comments

Comments
 (0)