Dynamically update command choices without using autocomplete

The title says it all, I don't want to have to use autocomplete as I've found it can be pretty buggy. I'd like to be able to do something like this:
const { gamesArray } = require("./browse.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Play a game.")
.addStringOption((option) =>
option
.setName("id")
.setDescription("The ID of the game to play.")
.setRequired(true)
.addChoices(...gamesArray)
),
const { gamesArray } = require("./browse.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Play a game.")
.addStringOption((option) =>
option
.setName("id")
.setDescription("The ID of the game to play.")
.setRequired(true)
.addChoices(...gamesArray)
),
but currently it just says "No options available" (gamesArray is formatted correctly)
15 Replies
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Inbestigator
InbestigatorOP13mo ago
Please ping
Squid
Squid13mo ago
If you log gamesArray in that commmand file, is it a non-empty array?
Inbestigator
InbestigatorOP13mo ago
where do you want me to (outside or in the execute function)
Squid
Squid13mo ago
Outside, right above module.exports = ..., where it'll be the same value as when you pass it into addChoices()
Inbestigator
InbestigatorOP13mo ago
it's empty, but that's ok, my question is more how can I have the choices update as the array gets longer without AC
Inbestigator
InbestigatorOP13mo ago
I've gotten it to work (buggily) like this:
No description
Inbestigator
InbestigatorOP13mo ago
randomly it won't recognize the interaction
Squid
Squid13mo ago
That's the issue you should be focusing on Sounds like in your interactionCreate listener, you sometimes don't call command.autocomplete(...), such as by returning before that code is reached
Inbestigator
InbestigatorOP13mo ago
No description
Inbestigator
InbestigatorOP13mo ago
it's unhappy about this bit:
No description
Squid
Squid13mo ago
That means you're taking more than 3 seconds between receiving the interaction and your response reaching discord's servers Do you make any database calls or other intensive function calls in your interactionCreate listener?
Inbestigator
InbestigatorOP13mo ago
no
} else if (interaction.isAutocomplete()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) return;
try {
await command.autocomplete(interaction, client);
} catch (error) {
console.error(error);
}
}
} else if (interaction.isAutocomplete()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) return;
try {
await command.autocomplete(interaction, client);
} catch (error) {
console.error(error);
}
}
and gamesArray is just an array that's saved in the other file, it isn't being fetched each time
Squid
Squid13mo ago
That means it's just a hardware issue Your network is probably too slow since it's likely that your processing speed is fast enough
Inbestigator
InbestigatorOP13mo ago
ah and you can't defer sadge I guess I'll just have to live with it or host on better hardware
Want results from more Discord servers?
Add your server