Autocompletion in Slash commands

Auto ocmpletion code:
const D = require("discord.js")
const fs = require("node:fs/promises")
module.exports = {
data: new D.SlashCommandBuilder()
.setName("querydata")
.setDescription("query the db for result")
.addStringOption(option =>
option.setName("q")
.setDescription("query data in memory")
.setAutocomplete(true)),

async autocomplete(Interaction) {
try {
console.log("\n Autocompletion started...")
const focusedvalue = Interaction.options.getFocused()
let files = []
const readdir = await fs.readdir("./Memory")
readdir.forEach(element => {
files.push(element)
console.log(`\n Pushing file: ${element}`)
});

const filteresdata = files.filter(filess => filess.startsWith(focusedvalue))
await Interaction.respond(
filteresdata.map(filess => ({ name: filess, value: filess }))
)
} catch (e) {
console.log(`\n Autocomplete function failed with error ${e}`)
return
}
},

async execute(Interaction) {
console.log(`\n Autocomplete finished`)
}
}
const D = require("discord.js")
const fs = require("node:fs/promises")
module.exports = {
data: new D.SlashCommandBuilder()
.setName("querydata")
.setDescription("query the db for result")
.addStringOption(option =>
option.setName("q")
.setDescription("query data in memory")
.setAutocomplete(true)),

async autocomplete(Interaction) {
try {
console.log("\n Autocompletion started...")
const focusedvalue = Interaction.options.getFocused()
let files = []
const readdir = await fs.readdir("./Memory")
readdir.forEach(element => {
files.push(element)
console.log(`\n Pushing file: ${element}`)
});

const filteresdata = files.filter(filess => filess.startsWith(focusedvalue))
await Interaction.respond(
filteresdata.map(filess => ({ name: filess, value: filess }))
)
} catch (e) {
console.log(`\n Autocomplete function failed with error ${e}`)
return
}
},

async execute(Interaction) {
console.log(`\n Autocomplete finished`)
}
}
Here is the code in Interacton event:
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

try {
if (interaction.isAutocomplete()) {
await command.autocomplete(interaction)
return
}
await command.execute(interaction);
} catch (e) {
console.log(`\n InteractionCreate event failed with code: ${e}`)
return
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

try {
if (interaction.isAutocomplete()) {
await command.autocomplete(interaction)
return
}
await command.execute(interaction);
} catch (e) {
console.log(`\n InteractionCreate event failed with code: ${e}`)
return
}
});
Any tips on what todo? dosnt seem like the code executes at all depsite autocomplete beeing active?
5 Replies
d.js toolkit
d.js toolkit10mo 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!
duck
duck10mo ago
if (!interaction.isChatInputCommand()) return;
an interaction won't be both a slash command interaction and an autocomplete interaction
U
UOP10mo ago
Now i just get "command.autocomplete is not a function" any tips there aswell?
duck
duck10mo ago
sounds like the object command doesn't have a method autocomplete seeing as this method exists in the first file you've shown, I'd guess the issue has more to do with how you're reading and storing the commands in <Client>.commands but I'm unable to diagnose further with just the information provided
U
UOP10mo ago
This is the command handler, it should hopefully add the command to the collection, but it dosnt seem to read autocomplete function i assume this is where the problem lays, however i'm a bit unsure why ot only imports the execute command?
module.exports = async function CMH(client, token) {
try {
return new Promise(async(OK, ERR) => {
client.commands = new D.Collection()
let RCMH_Commands = []

const commandFiles = fs.readdirSync("./Commands/");
for (const file of commandFiles) {
const command = require(`./${file}`)
if ('autocomplete' in command && 'data' in command || 'data' in command && 'execute' in command) {
console.log(`[INFO] Importing Command: ${file}.js`)
client.commands.set(command.data.name, command);
RCMH_Commands.push(command.data.toJSON())
} else {
console.log(`[WARNING] The command ./Commands/${file} is missing a required "data" or "execute" property.`);
}
}
await RCMH(RCMH_Commands, token)
OK(client.commands)
})
} catch (e) {
console.log(`Error in Command handler ${e} Terminating program`)
process.exit()
}

}
module.exports = async function CMH(client, token) {
try {
return new Promise(async(OK, ERR) => {
client.commands = new D.Collection()
let RCMH_Commands = []

const commandFiles = fs.readdirSync("./Commands/");
for (const file of commandFiles) {
const command = require(`./${file}`)
if ('autocomplete' in command && 'data' in command || 'data' in command && 'execute' in command) {
console.log(`[INFO] Importing Command: ${file}.js`)
client.commands.set(command.data.name, command);
RCMH_Commands.push(command.data.toJSON())
} else {
console.log(`[WARNING] The command ./Commands/${file} is missing a required "data" or "execute" property.`);
}
}
await RCMH(RCMH_Commands, token)
OK(client.commands)
})
} catch (e) {
console.log(`Error in Command handler ${e} Terminating program`)
process.exit()
}

}
I fixed it, apprently it was checking the wrong interaction name so it was checking the wrong command, thanks for the help
Want results from more Discord servers?
Add your server