How it works

Graphical Display

Drawing
How a Lavalink Client works (roughly)
  1. First you have to create a manager:

const manager = new LavalinkManager({ nodes: [...], sendToShard: (guildId, payload) => client.guilds.cache.get(guildId)?.shard?.send(payload), });

TIP: I'd recommend adding the manager to the client, like this: client.manager = new LavalinkManager...

  1. Create a player:

const player = manager.createPlayer({ guildId: "...", voiceChannelId: "..." });
  1. Connect the player

await player.connect();
  1. Search with the player:

const result = await player.search({ query: "Adele Hello", source: "youtube" }, interaction.user);
  1. Add the search result tracks / first result track (single) to the queue:

await player.queue.add(result.tracks[0]);
  1. In order to play the next song of the queue:

await player.play();

if you are playing a song, and want to play the next song, you have to run:

await player.skip();

If you want to leave channel, run player.disconnect(), you can then change voice channel and run player.connect() again.

If you want to stop playing and re-create the player, run:

await player.destroy();

To get the Player after it was created somewhere else, use:

const player = manager.getPlayer(guildId)
  1. Wanna know if a track Starts to play / ends playing, queue ends. etc. etc. Then listen to events!

manager.on("trackStart", (player, track) => {
 client.channels.cache.get(player.textChannelId).send(`Started playing ${track.info.title}`})
});
manager.on("queueEnd", (player) => { 
 client.channels.cache.get(player.textChannelId).send(`Nothing left to play`})
}); 

For more information, also such as automated options for manager / players, check out the Documentation Page

Last updated