#discord #discord-api #twilight #lavalink

twilight-lavalink

Lavalink client for the Twilight ecosystem

32 releases (11 breaking)

Uses new Rust 2021

new 0.11.0 May 15, 2022
0.10.1 Mar 20, 2022
0.8.1 Dec 24, 2021
0.7.2 Nov 20, 2021
0.2.1 Nov 2, 2020

#41 in HTTP client

Download history 47/week @ 2022-01-28 6/week @ 2022-02-04 25/week @ 2022-02-11 35/week @ 2022-02-18 4/week @ 2022-02-25 17/week @ 2022-03-04 76/week @ 2022-03-11 149/week @ 2022-03-18 11/week @ 2022-03-25 23/week @ 2022-04-01 17/week @ 2022-04-08 26/week @ 2022-04-15 40/week @ 2022-04-22 254/week @ 2022-04-29 207/week @ 2022-05-06 575/week @ 2022-05-13

1,077 downloads per month
Used in twilight

ISC license

1MB
29K SLoC

twilight-lavalink

codecov badge discord badge github badge license badge rust badge

twilight-lavalink is a client for Lavalink as part of the twilight ecosystem.

It includes support for managing multiple nodes, a player manager for conveniently using players to send events and retrieve information for each guild, and an HTTP module for creating requests using the http crate and providing models to deserialize their responses. It will automatically handle sending voice channel updates to Lavalink by processing events via the [client's process method][Lavalink::process], which you must call with every Voice State Update and Voice Server Update you receive.

Features

http-support

The http-support feature adds support for the http module to return request types from the http crate. This is enabled by default.

TLS

twilight-lavalink has features to enable tokio-tungstenite's TLS features. These features are mutually exclusive. rustls-native-roots is enabled by default.

native

The native feature enables tokio-tungstenite's native-tls feature.

To enable native, do something like this in your Cargo.toml:

[dependencies]
twilight-lavalink = { default-features = false, features = ["native"], version = "0.2" }

rustls-native-roots

The rustls-native-roots feature enables tokio-tungstenite's rustls-tls-native-roots feature, which uses rustls as the TLS backend and rustls-native-certs for root certificates.

This is enabled by default.

rustls-webpki-roots

The rustls-webpki-roots feature enables tokio-tungstenite's rustls-tls-webpki-roots feature, which uses rustls as the TLS backend and webpki-roots for root certificates.

This should be preferred over rustls-native-roots in Docker containers based on scratch.

Examples

Create a client, add a node, and give events to the client to process events:

use futures_util::stream::StreamExt;
use std::{
    env,
    error::Error,
    future::Future,
    net::SocketAddr,
    str::FromStr,
};
use twilight_gateway::{Event, Intents, Shard};
use twilight_http::Client as HttpClient;
use twilight_lavalink::{http::LoadedTracks, model::Play, Lavalink};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let token = env::var("DISCORD_TOKEN")?;
    let lavalink_host = SocketAddr::from_str(&env::var("LAVALINK_HOST")?)?;
    let lavalink_auth = env::var("LAVALINK_AUTHORIZATION")?;
    let shard_count = 1u64;

    let http = HttpClient::new(token.clone());
    let user_id = http.current_user().exec().await?.model().await?.id;

    let lavalink = Lavalink::new(user_id, shard_count);
    lavalink.add(lavalink_host, lavalink_auth).await?;

    let intents = Intents::GUILD_MESSAGES | Intents::GUILD_VOICE_STATES;
    let (shard, mut events) = Shard::new(token, intents).await?;
    shard.start().await?;

    while let Some(event) = events.next().await {
        lavalink.process(&event).await?;
    }

    Ok(())
}

There is also an example of a basic bot located in the root of the twilight repository.

Dependencies

~8–17MB
~338K SLoC