createAutocompleteContext method

Future<AutocompleteContext> createAutocompleteContext(
  1. ApplicationCommandAutocompleteInteraction interaction,
  2. ChatCommand command
)

Create an AutocompleteContext from an ApplicationCommandAutocompleteInteraction.

interaction is the interaction event that triggered the autocomplete action and command is the command to which the autocompleted parameter belongs.

Implementation

Future<AutocompleteContext> createAutocompleteContext(
  ApplicationCommandAutocompleteInteraction interaction,
  ChatCommand command,
) async {
  Member? member = interaction.member;
  User user = member?.user ?? interaction.user!;

  Iterable<InteractionOption> expandOptions(List<InteractionOption> options) sync* {
    for (final option in options) {
      yield option;
      if (option.options case final nestedOptions?) {
        yield* expandOptions(nestedOptions);
      }
    }
  }

  final focusedOption = expandOptions(interaction.data.options!)
      .singleWhere((element) => element.isFocused == true);

  return AutocompleteContext(
    commands: commands,
    guild: await interaction.guild?.get(),
    channel: await interaction.channel!.get() as TextChannel,
    member: member,
    user: user,
    command: command,
    client: interaction.manager.client as NyxxGateway,
    interaction: interaction,
    option: focusedOption,
    currentValue: focusedOption.value.toString(),
  );
}