Autocomplete failing to load choices

Here is my code:
async function handleAutocomplete(interaction, gamesCache) {
console.log('handleAutocomplete called');
console.log(gamesCache);
const gameName = interaction.options.getString('name');

// Search for games that match the input in the cache
const games = [];
for (const game of gamesCache) {
if (game.title.toLowerCase().includes(gameName.toLowerCase())) {
games.push(game.title);
if (games.length >= 25) break;
}
}

// Return the matching games as autocomplete choices
await interaction.respond( games.map(game => ({ name: game.slice(0, 100), value: game.slice(0, 100) })))
}
async function handleAutocomplete(interaction, gamesCache) {
console.log('handleAutocomplete called');
console.log(gamesCache);
const gameName = interaction.options.getString('name');

// Search for games that match the input in the cache
const games = [];
for (const game of gamesCache) {
if (game.title.toLowerCase().includes(gameName.toLowerCase())) {
games.push(game.title);
if (games.length >= 25) break;
}
}

// Return the matching games as autocomplete choices
await interaction.respond( games.map(game => ({ name: game.slice(0, 100), value: game.slice(0, 100) })))
}
I have a pre-loaded games cache that is roughly 1600 games in an array. The command returns the games if I just hit enter and skip the autocomplete, however I want to list the matches just for a better UX. Atm it just does this whats shown in the pic. BUT hitting enter and just running it shows the games
20 Replies
d.js toolkit
d.js toolkit2y 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.
sexy dark chocolate
Is it logging the "handle Autocomplete called"?
MrHanBrolo
MrHanBroloOP2y ago
no
sexy dark chocolate
Also I don't see you getting the focused option's value anywhere, I hope you're not mistaking "interaction.options.getString()" for that
MrHanBrolo
MrHanBroloOP2y ago
I'm not gonna lie the one thing that confuses me a lot is autocomplete
sexy dark chocolate
Then it's an issue with calling the function, show me how you call the function The guide has a step by step process to help you with autocomplete
MrHanBrolo
MrHanBroloOP2y ago
let me take another look at the guide
sexy dark chocolate
Alright, you can always ask for help here
MrHanBrolo
MrHanBroloOP2y ago
Oh I know, I'm just super tired and might take a break until tomorrow and reply here haha. But, if anything updates before then I'll post progress Yeah I'm kinda stumped right now Even using the example with fixed options that has the guide command, it doesn't load anything
sexy dark chocolate
If it doesnt log the string at the top of the function then the function isn't being called, show me how you call the function
MrHanBrolo
MrHanBroloOP2y ago
I'm assuming you mean this?
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction, gamesCache);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction, gamesCache);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
sexy dark chocolate
Ah there we go You have if(!interaction.isChatInputCommand()) return at the top It'll instantly not run the code if any interaction recieved is not a slash command And you dont call the handle Autocomplete function Is this function attached to the command object? Like command.execute()?
MrHanBrolo
MrHanBroloOP2y ago
I thought autocomplete was a slash command, no? I also don't see what you mean about the isChatInputCommand or, wait thats basically the same yeah Like uh...
sexy dark chocolate
Autocomplete interactions come from a slash command yes but slash commands are chatinputcommand interactions whereas autocomplete options send autocomplete interactions 2 different interactions
MrHanBrolo
MrHanBroloOP2y ago
Ah, okay So I need to add an event handler for that Hm, although... whats the site people use to paste code https://codeshare.io/YLVM6b that'll do
sexy dark chocolate
Then just for testing, do this
if(interaction.isChatInputCommand()) {
// all the code you sent
} else if(interaction.isAutocomplete()) {
//Get the command from client.commands

//command.handleAutocomplete(yourParams)
}
if(interaction.isChatInputCommand()) {
// all the code you sent
} else if(interaction.isAutocomplete()) {
//Get the command from client.commands

//command.handleAutocomplete(yourParams)
}
MrHanBrolo
MrHanBroloOP2y ago
alright, will try
sexy dark chocolate
Not event handler but rather an interaction handler, all you need to do is check the type of interaction recieved then move accordingly As seen in the lil snippet I shown above It can be done in one listener but if you'd like you can create another just for autocomplete interactions After you're done, try it and if it doesn't work, show me the code
MrHanBrolo
MrHanBroloOP2y ago
MrHanBrolo
MrHanBroloOP2y ago
Prayge you sensei
Want results from more Discord servers?
Add your server