Lavalink-Client
  • 😁Lavalink Client
  • Basics
    • How it works
    • Get Started
    • Example Discord Bot
  • Documentation
    • LavalinkManager
      • Manager Options
        • ManagerQueueOptions
          • QueueStoreManager
          • QueueChangesWatcher
        • ManagerPlayerOptions
          • RequestTransformer
          • AutoplayFunction
        • BotClientOptions
      • ManagerUtils
    • NodeManager
      • Types
        • LavalinkNode
        • LavalinkNodeOptions
    • Player
      • PlayerOptions
      • PlayerDestroyReasons
      • PlayerTypes
        • RepeatMode
        • PlayerJson
    • Other Types
      • EQBand
      • FilterData
        • KaraokeFilter
      • Track
        • TrackInfo
        • PluginInfo
      • UnresolvedTrack
      • UnresolvedQuery
      • LavalinkTrack
        • Base64
        • LavalinkTrackInfo
      • Payloads
        • GuildShardPayload
        • PlayerEvent
        • TrackStartEvent
        • TrackEndEvent
        • TrackStuckEvent
        • TrackExceptionEvent
        • WebSocketClosedEvent
      • SearchPlatform
        • LavalinkSearchPlatform
        • ClientSearchPlatform
      • SourceNames
        • LavalinkSourceNames
        • LavaSrc SourceNames (lavalink-plugin)
    • Other Utils and Classes
      • DefaultQueueStore
      • MiniMap
  • Requirements
  • Host a Lavalink-Server
    • application.yml Configuration
      • With Spotify, Deezer, Apple Music, etc.
    • Host via pm2
    • Host via systemd
    • Host via screen
  • ⚪Github
  • 📖Lavalink-Support Server
Powered by GitBook
On this page
  • Overview
  • First you have to install the package into your project
  • Then you have to create the Manager
  • Initialize the Manager and listen to the raw Event
  • Play Songs
  • How to add more features?
  1. Basics

Get Started

First steps to get started

PreviousHow it worksNextExample Discord Bot

Last updated 1 year ago

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
    },
});
index.js
const { LavalinkManager } = require("lavalink-client");
const { Client, GatewayIntentBits } = require("discord.js");

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

// 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! }); 
});
index.js
// 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!
index.js
// 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

Install the package
Create the Manager
IMPORTANT: Init the Manager & send the Raw-Event
Search Song(s) and play them