lavalink.ts

Easy to use lavalink wrapper with support for filters written in TypeScript.

Usage no npm install needed!

<script type="module">
  import lavalinkTs from 'https://cdn.skypack.dev/lavalink.ts';
</script>

README

Lavalink.ts

This package is mainly focused on making lavalink easier to use as well as its features.
This project is still in beta and might contain stupid bugs or unnecessary code.
Visit the documentation for a more in-depth map of classes and functions of this npm.

Example

const lavalink = require("lavalink.ts")

const manager = new lavalink.LavalinkManager({
    userID: "123456789012345678", // The discord client user ID
    shards: 1 // Amount of shards this bot have.
    send(packet) {
        // This depends on what discord library you are using, for example for 
        // discord.js you can do something like this (d.js v13):
        client.ws.shards.get(client.guilds.cache.get(packet.d.guild_id).shardId).send(packet)
    }
}, []) // Second parameter is an array of nodes.

manager.addNode({
    id: "1", // The unique id for this node.
    password: "youshallnotpass", // The lavalink password.
    port: 2333, // The port used by lavalink.
    host: "mylavalink" // The host url without http.
}) // Optionally you can pass them through addNode method.

// This will fire once all nodes are ready.
manager.once("ready", () => {
    console.log(`All nodes ready!`)
})

manager.connect() // Causes all nodes to connect.

// You also must provide voice state and server updates aswell as guild create events from the gateway 
// Using discord.js v13, it would look like this
client.ws.on("GUILD_CREATE", (data) => manager.handleGuildCreate(data))
client.ws.on("VOICE_SERVER_UPDATE", (data) => manager.handleVoiceServerUpdate(data))
client.ws.on("VOICE_STATE_UPDATE", (data) => manager.handleVoiceStateUpdate(data))

Example using discord.js v13

const Discord = require("discord.js")
const lava = require("lavalink.ts")

const client = new Discord.Client({
    intents: Object.keys(Discord.Intents.FLAGS) // Do not use this in production
})

client.once("ready", () => {
    const manager = new lava.LavalinkManager({
        userID: client.user.id,
        shards: client.ws.shards.size,
        send: (packet) => client.ws.shards.get(client.guilds.cache.get(packet.d.guild_id).shardId). send(packet),
    }, [
        {
            id: "1",
            port: 2333,
            host: "localhost",
            password: "youshallnotpass"
        }
    ])

    manager.once("ready", () => {
        console.log(`Lavalink nodes ready!`)
    })

    console.log(`Discord client ready!`)

    manager.connect()
})

client.on("message", async message => {
    if (message.author.bot || message.channel.type === "DM") return; 

    if (message.content.startsWith("!play")) {
        const query = message.content.split(/ +/).slice(1).join(" ")

        const player = await manager.join({
            guildID: message.guild.id,
            nodeID: "1",
            channelID: message.member.voice.channelId
        })

        const results = await player.search(query)

        player.play(results.tracks[0])
    }
})

client.login("token here")