Discord Player
Common actions

Autocomplete Search

Learn how to add autocomplete search to your command

You can return song suggestions to autocomplete interaction using discord-player. Just apply something similar to this:

command here stands for your command file. Check one of the example bot implementation to understand more.

index.js
client.on("ready", function (readyClient) {
    // Discord Player Initialization
	const { Player } = require("discord-player");
	const player = new Player(readyClient);
});
 
client.on("interactionCreate", async (interaction) => {
    if (interaction.isAutocomplete()) {
        // Handling autocomplete requests
        ...
        await command.autocompleteRun(interaction);
    } else if(interaction.isChatInputCommand()){
        // Handling chat slashCommand  requests
        ...
        await command.execute(interaction);
    }
});
play.js
import { useMainPlayer } from 'discord-player';
 
async function autocompleteRun(interaction) {
  const player = useMainPlayer();
  const query = interaction.options.getString('query', true);
  const results = await player.search(query);
 
  // Returns a list of songs with their title
  return interaction.respond(
    results.tracks.slice(0, 10).map((t) => ({
      name: t.title,
      value: t.url,
    })),
  );
}
 
async function execute(interaction) {
  const player = useMainPlayer();
  const query = interaction.options.getString('query');
  const searchResult = await player.search(query, { requestedBy: interaction.user });
 
  if (!searchResult.hasTracks()) {
    // Check if we found results for this query
    await interaction.reply(`We found no tracks for ${query}!`);
    return;
  } else {
    await player.play(interaction.member.voice.channel, searchResult, {
      nodeOptions: {
        metadata: interaction.channel,
        // You can add more options over here
      },
    });
  }
  await interaction.reply({ content: `Loading your track(s)"}` });
}