Setting autocomplete options based on other option's value

I'm trying to make it so one autocomplete option (type)'s choices are based on the value of another option (category) within the same command. When I first set the value of category - which is a non-autocomplete string choice option - and I move to the autocomplete option type it successfully populates its choices according to the value of category. However, when I go back and change the value of category to something else, then move to the type option, the proper corresponding choices only populate correctly if I type at least one letter. If I do not, it populates it with the previously set of choices, corresponding to the first value set for category. I've attached a GIF that will hopefully help understand my issue. Thank you! [email protected] node v18.16.0
2 Replies
Teixeira
TeixeiraOP2y ago
private async _autocompleteAddItem(interaction: Subcommand.AutocompleteInteraction) {
const focusedOption = interaction.options.getFocused(true);
let autocompleteChoices: ApplicationCommandOptionChoiceData<string>[];

if (focusedOption.name === 'type') {
autocompleteChoices = [
{
name: 'Please select an item category first.',
value: 'invalid'
}
];

const itemCategoryIdentifier = interaction.options.getString('category');

if (itemCategoryIdentifier) {
const itemCategory = this.container.application.shops.itemCategories.find(
(itemCategory) => itemCategory.identifier === itemCategoryIdentifier
);

if (!itemCategory) {
throw new TypeError(`Unknown item category identifier ${itemCategoryIdentifier}`);
}

autocompleteChoices = itemCategory.types.map((type) => {
const { name, identifier } = type;

return {
name,
value: identifier
};
});
}
} else {
throw new TypeError(`Unknown autocomplete option "${focusedOption.name}"`);
}

const query = focusedOption.value;
await interaction.respond(new AutocompleteQuery(autocompleteChoices, query).results);
}
private async _autocompleteAddItem(interaction: Subcommand.AutocompleteInteraction) {
const focusedOption = interaction.options.getFocused(true);
let autocompleteChoices: ApplicationCommandOptionChoiceData<string>[];

if (focusedOption.name === 'type') {
autocompleteChoices = [
{
name: 'Please select an item category first.',
value: 'invalid'
}
];

const itemCategoryIdentifier = interaction.options.getString('category');

if (itemCategoryIdentifier) {
const itemCategory = this.container.application.shops.itemCategories.find(
(itemCategory) => itemCategory.identifier === itemCategoryIdentifier
);

if (!itemCategory) {
throw new TypeError(`Unknown item category identifier ${itemCategoryIdentifier}`);
}

autocompleteChoices = itemCategory.types.map((type) => {
const { name, identifier } = type;

return {
name,
value: identifier
};
});
}
} else {
throw new TypeError(`Unknown autocomplete option "${focusedOption.name}"`);
}

const query = focusedOption.value;
await interaction.respond(new AutocompleteQuery(autocompleteChoices, query).results);
}
Favna
Favna2y ago
This wont be sapphire related because the autocomplete stuff is just passing events through from Discord's API. We don't modify the data or anything. It's just geting passed 1:1 from Discord API to DiscordJS to Sapphire. So add some logging and try to figure it out I suppose. Can't tell from the code or from the gif. You could also consider restructuring so the categories can be subcommands so instead of my_shop_item add it would be /add_shop_item <category_subcommand>
Want results from more Discord servers?
Add your server