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! discord.js@14.11.0 node v18.16.0
5 Replies
d.js toolkit
d.js toolkit12mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
grass
grass12mo ago
@jp_teixeira
Teixeira
Teixeira12mo ago
I added this at the end of the post discord.js@14.11.0 node v18.16.0
grass
grass12mo ago
theres multiple bullet points * Show your code! i believe thats just how discord handles it though
Teixeira
Teixeira12mo ago
I'm not being able to add the code to the original post can I just send it as a new message here
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);
}
Sadge... Alright thank you
Want results from more Discord servers?
Add your server
More Posts