Why isn't the autocomplete working the autocomplete function isn't even getting called.

Why isn't the autocomplete working the autocomplete function isn't even getting called. Code:
const { ApplicationCommandOptionType, EmbedBuilder } = require("discord.js");
const formatDuration = require("../../../structures/FormatDuration.js");

module.exports = {
name: "play",
description: "Play your favorite songs.",
category: "Music",
options: [
{
name: "query",
description: "Provide song name or url. (Supports YouTube, SoundCloud, Deezer and Spotify.)",
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true, // Add autocomplete property for the 'query' option
},
],
permissions: {
bot: ["Speak", "Connect"],
channel: ["Speak", "Connect"],
user: [],
},
settings: {
inVc: true,
sameVc: false,
player: false,
current: false,
owner: false,
premium: false,
},
autocomplete: async (client, interaction) => {
console.log('Autocomplete function called!');
let query = await interaction.options.getString('query') || " ";
if (query.includes("youtu") || query.includes("youtube") || query.includes("youtu.be")) return;
let max = 7;
const result = await client.poru.resolve(query, "youtube");
const { tracks } = result;
const trackd = tracks.slice(0, max);

let results = trackd.map((track) => `${track.info.title}`);
await interaction.respond(
results.map(choice => ({ name: choice, value: choice })),
);
},

run: async (client, interaction, player) => {
await interaction.deferReply({ ephemeral: false });

const song = interaction.options.getString("query");

const embed = new EmbedBuilder().setColor(client.color);

if (player && interaction.member.voice.channelId !== interaction.guild.members.me.voice.channelId) {
embed.setDescription(`<:icons8x128:1177502387863294042> | You must be on the same voice channel as mine to use this command.`).setTimestamp();

return interaction.editReply({ embeds: [embed] });
}

const res = await client.poru.resolve({ query: song, requester: interaction.user });
const { loadType, tracks, playlistInfo } = res;

if (loadType === "LOAD_FAILED" || loadType === "NO_MATCHES") {
embed.setDescription(`<:icons8x128:1177502387863294042> | Song was not found or Failed to load song!`);

return interaction.editReply({ embeds: [embed] });
}

if (!player) {
player = await client.poru.createConnection({
guildId: interaction.guild.id,
voiceChannel: interaction.member.voice.channel.id,
textChannel: interaction.channel.id,
deaf: true,
});
}

if (player.state !== "CONNECTED") player.connect();

if (loadType === "PLAYLIST_LOADED") {
for (const track of tracks) {
player.queue.add(track);
}

embed.setDescription(`<:icons8tick128:1177501732914339840> | **[${playlistInfo.name}](${song})** • \`${tracks.length}\` tracks • ${interaction.user}`);

if (!player.isPlaying && !player.isPaused) player.play();
} else if (loadType === "SEARCH_RESULT" || loadType === "TRACK_LOADED") {
const track = tracks[0];

player.queue.add(track);

embed.setDescription(
`<:icons8tick128:1177501732914339840> | **[${track.info.title ? track.info.title : "Unknown"}](${track.info.uri})** • \`${
track.info.isStream ? "LIVE" : formatDuration(track.info.length)
}\` • ${interaction.user}`,
);

if (!player.isPlaying && !player.isPaused) player.play();
}

await interaction.editReply({ embeds: [embed] });
},
};
const { ApplicationCommandOptionType, EmbedBuilder } = require("discord.js");
const formatDuration = require("../../../structures/FormatDuration.js");

module.exports = {
name: "play",
description: "Play your favorite songs.",
category: "Music",
options: [
{
name: "query",
description: "Provide song name or url. (Supports YouTube, SoundCloud, Deezer and Spotify.)",
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true, // Add autocomplete property for the 'query' option
},
],
permissions: {
bot: ["Speak", "Connect"],
channel: ["Speak", "Connect"],
user: [],
},
settings: {
inVc: true,
sameVc: false,
player: false,
current: false,
owner: false,
premium: false,
},
autocomplete: async (client, interaction) => {
console.log('Autocomplete function called!');
let query = await interaction.options.getString('query') || " ";
if (query.includes("youtu") || query.includes("youtube") || query.includes("youtu.be")) return;
let max = 7;
const result = await client.poru.resolve(query, "youtube");
const { tracks } = result;
const trackd = tracks.slice(0, max);

let results = trackd.map((track) => `${track.info.title}`);
await interaction.respond(
results.map(choice => ({ name: choice, value: choice })),
);
},

run: async (client, interaction, player) => {
await interaction.deferReply({ ephemeral: false });

const song = interaction.options.getString("query");

const embed = new EmbedBuilder().setColor(client.color);

if (player && interaction.member.voice.channelId !== interaction.guild.members.me.voice.channelId) {
embed.setDescription(`<:icons8x128:1177502387863294042> | You must be on the same voice channel as mine to use this command.`).setTimestamp();

return interaction.editReply({ embeds: [embed] });
}

const res = await client.poru.resolve({ query: song, requester: interaction.user });
const { loadType, tracks, playlistInfo } = res;

if (loadType === "LOAD_FAILED" || loadType === "NO_MATCHES") {
embed.setDescription(`<:icons8x128:1177502387863294042> | Song was not found or Failed to load song!`);

return interaction.editReply({ embeds: [embed] });
}

if (!player) {
player = await client.poru.createConnection({
guildId: interaction.guild.id,
voiceChannel: interaction.member.voice.channel.id,
textChannel: interaction.channel.id,
deaf: true,
});
}

if (player.state !== "CONNECTED") player.connect();

if (loadType === "PLAYLIST_LOADED") {
for (const track of tracks) {
player.queue.add(track);
}

embed.setDescription(`<:icons8tick128:1177501732914339840> | **[${playlistInfo.name}](${song})** • \`${tracks.length}\` tracks • ${interaction.user}`);

if (!player.isPlaying && !player.isPaused) player.play();
} else if (loadType === "SEARCH_RESULT" || loadType === "TRACK_LOADED") {
const track = tracks[0];

player.queue.add(track);

embed.setDescription(
`<:icons8tick128:1177501732914339840> | **[${track.info.title ? track.info.title : "Unknown"}](${track.info.uri})** • \`${
track.info.isStream ? "LIVE" : formatDuration(track.info.length)
}\` • ${interaction.user}`,
);

if (!player.isPlaying && !player.isPaused) player.play();
}

await interaction.editReply({ embeds: [embed] });
},
};
21 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 staff
SpecialSauce
SpecialSauce13mo ago
Where do you call autocomplete()?
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
No description
SpecialSauce
SpecialSauce13mo ago
That’s where you define it You should be calling it somewhere in your interaction handler. When you receive an autocomplete interaction
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
isn't the autocomplete: async (client, interaction) supposed to do that?
SpecialSauce
SpecialSauce13mo ago
That’s where the logic is How and when does that code run? You should be calling the function somewhere. Otherwise it’s just defined and unused
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
this loads the commands
const { readdirSync } = require("node:fs");
const path = require("node:path");

module.exports = (client) => {
const data = [];

readdirSync("./src/commands/Slash/").forEach((dir) => {
const commands = readdirSync(`./src/commands/Slash/${dir}/`).filter((file) => file.endsWith(".js"));

for (const file of commands) {
const pull = require(path.join(__dirname, `../commands/Slash/${dir}/${file}`));

if (pull.name) {
client.slashCommands.set(pull.name, pull);
data.push(pull);
} else {
continue;
}
}
});

client.on("ready", async () => {
await client.application.commands.set(data);

console.log(`[INFO] ${client.slashCommands.size} Slash Commands Loaded!`);
});
};
const { readdirSync } = require("node:fs");
const path = require("node:path");

module.exports = (client) => {
const data = [];

readdirSync("./src/commands/Slash/").forEach((dir) => {
const commands = readdirSync(`./src/commands/Slash/${dir}/`).filter((file) => file.endsWith(".js"));

for (const file of commands) {
const pull = require(path.join(__dirname, `../commands/Slash/${dir}/${file}`));

if (pull.name) {
client.slashCommands.set(pull.name, pull);
data.push(pull);
} else {
continue;
}
}
});

client.on("ready", async () => {
await client.application.commands.set(data);

console.log(`[INFO] ${client.slashCommands.size} Slash Commands Loaded!`);
});
};
SpecialSauce
SpecialSauce13mo ago
Do you have an interaction create event listener?
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
no but it works exept the autocomplete
SpecialSauce
SpecialSauce13mo ago
What works? Logging in? That’s all I see that could be working. And don’t redeploy commands in the ready event. You only need to deploy them when you change a commands parameters, add, or remove a command.
d.js docs
d.js docs13mo ago
guide Creating Your Bot: Command handling read more
SpecialSauce
SpecialSauce13mo ago
This is probably a good section to review.
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
here's the error
No description
SpecialSauce
SpecialSauce13mo ago
Again you’re not responding to the interaction. Does any other command work?
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
every command work exept the autocomplete
SpecialSauce
SpecialSauce13mo ago
So show your interaction create event handler
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
to be honest i'm not the one who wrote the code and the person who wrote the code made it so hard to find a single piece of code. there's like 100 files of stuff in this
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
GitHub
GitHub - adh319/Lunox: Simply powerfull Discord Music Bot that used...
Simply powerfull Discord Music Bot that used Poru Lavalink client & DiscordJS v14 - GitHub - adh319/Lunox: Simply powerfull Discord Music Bot that used Poru Lavalink client & DiscordJS v14
Abiru Ekanayakaツ
Abiru EkanayakaツOP13mo ago
i even wrote two sperate commands and put it in the commands/slash folder and they just worked
SpecialSauce
SpecialSauce13mo ago
Autocomplete interactions are a little different. They require you to handle them separately and use respond() to send the list of options to discord. If you have an event handler you may have a separate interactionCreate file that handles executing commands. You would need to handle the autocomplete interactions in there.
d.js docs
d.js docs13mo ago
guide Slash Commands: Autocomplete - Responding to autocomplete interactions read more
Want results from more Discord servers?
Add your server