Skip to content

Commit f4b7bae

Browse files
authored
fix: send a proper empty response when populating autocomplete with no choices (#756)
* fix: prevent kwargs modification during `for` loop iteration * fix: proper empty choices parsing for autocomplete * pre-commit: black refactor * oops: remove extra debug logging
1 parent 129a6b3 commit f4b7bae

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

interactions/client/context.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -518,32 +518,36 @@ async def populate(self, choices: Union[Choice, List[Choice]]) -> List[Choice]:
518518
"""
519519

520520
async def func():
521-
if choices:
522-
_choices: list = []
523-
if all(isinstance(choice, Choice) for choice in choices):
524-
_choices = [choice._json for choice in choices]
525-
elif all(
526-
isinstance(choice, dict) and all(isinstance(x, str) for x in choice)
527-
for choice in choices
528-
):
529-
_choices = list(choices)
530-
elif isinstance(choices, Choice):
531-
_choices = [choices._json]
532-
else:
533-
raise InteractionException(
534-
6, message="Autocomplete choice items must be of type Choice"
535-
)
536-
537-
await self.client.create_interaction_response(
538-
token=self.token,
539-
application_id=int(self.id),
540-
data={
541-
"type": InteractionCallbackType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT.value,
542-
"data": {"choices": _choices},
543-
},
521+
_choices: Union[list, None] = []
522+
523+
if not choices or (isinstance(choices, list) and len(choices) == 0):
524+
_choices = None
525+
elif isinstance(choices, Choice):
526+
_choices.append(choices._json)
527+
elif isinstance(choices, list) and all(
528+
isinstance(choice, Choice) for choice in choices
529+
):
530+
_choices = [choice._json for choice in choices]
531+
elif all(
532+
isinstance(choice, dict) and all(isinstance(x, str) for x in choice)
533+
for choice in choices
534+
):
535+
_choices = list(choices)
536+
else:
537+
raise InteractionException(
538+
6, message="Autocomplete choice items must be of type Choice"
544539
)
545540

546-
return _choices
541+
await self.client.create_interaction_response(
542+
token=self.token,
543+
application_id=int(self.id),
544+
data={
545+
"type": InteractionCallbackType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT.value,
546+
"data": {"choices": _choices},
547+
},
548+
)
549+
550+
return _choices
547551

548552
return await func()
549553

0 commit comments

Comments
 (0)