Autocompletion in Slash commands
Auto ocmpletion code:
Here is the code in Interacton event:
Any tips on what todo? dosnt seem like the code executes at all depsite autocomplete beeing active?
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?
