npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lavaclient

v5.0.0-rc.3

Published

A simple, easy-to-use, and flexible lavalink client for node.js

Downloads

1,939

Readme

A lightweight and powerful lavalink v4 client for NodeJs.

  • Easy-to-use: lavaclient has a neat and user-friendly promise-based api.
  • Performant: designed to be small and lightweight, it's a great choice for any project.
  • Library Agnostic: lavaclient doesn't require you to use a specific discord library. Use anything you want!

Installation

Node.js LTS (or higher) is required to use lavaclient.

Stable

(p)npm install lavaclient
// or
yarn add lavaclient

Beta (may be outdated)

(p)npm install lavaclient@next
// or
yarn add lavaclient@next

Deno

Take a look at lavadeno :)

Usage

Setup

import { Cluster, Node, type NodeOptions } from "lavaclient";

const info: NodeOptions["info"] = {
    host: "127.0.0.1",
    port: 2333,
    auth: "youshallnotpass"
}

// If you only have a single lavalink node, use the `Node` class.
const lavaclient = new Node({
    info,
    ws: {
        // The client name to use, defaults to some very long string lol.
        clientName: "my very cool bot",
        // Resuming is enabled by default, but you can disable it if you want.
        // The timeout defaults to 1 minute.
        resuming: false | { timeout: 30_000 },
        // The reconnecting options, this is enabled by default.
        // If you want to disable it, set it to `false`.
        reconnecting: {
            // The number of tries to reconnect before giving up, defaults to Infinity.
            tries: 3,
            // The delay in milliseconds between each attempt, defaults to 5 seconds.
            // This can either be a function that accepts the current try and returns the delay, or a static delay.
            delay: (attempt) => attempt * 1_000
        }
    },
    rest: {
        // Whether lavalink should return stack traces for requests that ran into exceptions.
        enableTrace: true,
        // The fetch implementation to use, defaults to node.js built-in fetch.
        fetch: fetch,
        // The user agent to use for requests, defaults to some very long string lol.
        userAgent: "my very cool bot (v1.0.0, <user id>)"
    },
    discord: {
        sendGatewayCommand: (id, payload) => // Send the payload to the Discord Gateway.
    }
});

// If you have multiple lavalink nodes, use the `Cluster` class.
const lavaclient = new Cluster({
    // An array of lavalink node options, this supports the same thing as the `Node` class.
    nodes: [{ info }],
    discord: {
        sendGatewayCommand: (id, payload) => // Send the payload to the Discord Gateway.
    }
});

// Connect to the lavalink node(s).
await lavaclient.connect("1077037369850605599");

Using Players

Creation

const player = lavaclient.players.create("<guild id>");

Destruction

// Passing `true` as the 2nd arg will skip checking if the player exists locally.
await lavaclient.players.destroy("<guild id>", true);

If you want a player to leave the voice channel, you can do this first:

player.voice.disconnect();

Playing Tracks

import { S, getUserData } from "lavaclient";

const results = await lavaclient.api.loadTracks("ytsearch:never gonna give you up");
if (results.loadType === "search") {
    const track = results.data[0];

    // You can pass a track object directly.
    await player.play(track);

    // If you want to pass some user data to the track, you can do this:
    await player.play({
        encoded: track.encoded,
        userData: { requesterId: interaction.user.id },
    });

    // There's also an option to pass a user data schema for type-safe user data values.
    const schema = S.struct({
        requesterId: S.string,
    });

    await player.play({
        encoded: track.encoded,
        userData: { requesterId: interaction.user.id },
        userDataSchema: schema,
    });

    getUserData(player.track, schema); // { requesterId: string }
}

Misc

player.resume()
player.pause()

player.setFilters({ volume: 0.5 });
player.setFilters("volume", 0.5);

player.seek(1000);
player.stop();

// A little experimental but you can transfer a player to a different node.
player.transfer(lavaclient2)

Handling Voice Updates

Lavalink requires you to forward voice server & state updates to it so that it can connect to the voice channel.

Here's a discord.js v14 example:

import { Client, GatewayDispatchEvents } from "discord.js";

const client = new Client({ ... });

client.ws.on(GatewayDispatchEvents.VoiceStateUpdate,  (u) => lavaclient.players.handleVoiceUpdate(u));
client.ws.on(GatewayDispatchEvents.VoiceServerUpdate, (u) => lavaclient.players.handleVoiceUpdate(u));

Support Server