Get Started

First steps to get started

Overview

First you have to install the package into your project

👉 via NPM
npm install --save lavalink-client

Dev Version: (Current)

npm install tomato6966/lavalink-client
👉 via YARN
yarn add lavalink-client

Dev Version: (Current)

yarn add tomato6966/lavalink-client

Then you have to create the Manager

index.ts
import { LavalinkManager } from "lavalink-client";
import { Client, GatewayIntentBits } from "discord.js";

// create bot client
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildVoiceStates
    ]
}) as Client & { lavalink: LavalinkManager }; 

// create lavalink client
client.lavalink = new LavalinkManager({
    nodes: [
        { // Important to have at least 1 node
            authorization: "yourverystrongpassword",
            host: "localhost",
            port: 2333,
            id: "testnode"
        }
    ],
    sendToShard: (guildId, payload) =>
        client.guilds.cache.get(guildId)?.shard?.send(payload),
    client: {
        id: process.env.CLIENT_ID,
        username: "TESTBOT",
    },
    // everything down below is optional
    autoSkip: true,
    playerOptions: {
        clientBasedPositionUpdateInterval: 150,
        defaultSearchPlatform: "ytmsearch",
        volumeDecrementer: 0.75,
        //requesterTransformer: requesterTransformer,
        onDisconnect: {
            autoReconnect: true, 
            destroyPlayer: false 
        },
        onEmptyQueue: {
            destroyAfterMs: 30_000, 
            //autoPlayFunction: autoPlayFunction,
        }
    },
    queueOptions: {
        maxPreviousTracks: 25
    },
});

Initialize the Manager and listen to the raw Event

index.ts
// above the lavalinkManager + client were created
client.on("raw", d => client.lavalink.sendRawData(d));
client.on("ready", async () => {
    console.log("Discord Bot is ready to be Used!");
    await client.lavalink.init({ ...client.user! }); 
});

Play Songs

index.ts
// create player
const player = await client.lavalink.createPlayer({
    guildId: guild.id, 
    voiceChannelId: voice.id, 
    textChannelId: text.id, 
    // optional configurations:
    selfDeaf: true, 
    selfMute: false, 
    volume: 100
}); 

// connect the player to it's vc
await player.connect();

// search a query (query-search, url search, identifier search, etc.)
const res = await player.search({
    query: `Elton John`,
    // source: `soundcloud`,
}, interaction.user); 

// add the first result
await player.queue.add(res.tracks[0]); 

// only play if the player isn't playing something, 
if(!player.playing) await player.play(); // you can provide a specific track, or let the manager choose the track from the queue!

How to add more features?

If you want to display things like "started playing a track", "queue ended", check out all possible events!

Important! Before creating a player, make sure that at least 1 node is connected to the lavalink node

Last updated