3 unstable releases

new 0.1.1 Sep 20, 2020
0.1.0 Sep 13, 2020
0.0.0 Aug 30, 2020

#62 in HTTP client


Used in twilight

ISC license

535KB
14K SLoC

twilight-lavalink

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, 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 async-tungstenite's TLS features. These features are mutually exclusive. rustls is enabled by default.

native

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

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

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

rustls

The rustls feature enables async-tungstenite's async-tls feature, which use rustls as the TLS backend.

This is enabled by default.

Examples

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

use futures_util::stream::StreamExt;
use std::{
    convert::TryInto,
    env,
    error::Error,
    future::Future,
    net::SocketAddr,
    str::FromStr,
};
use twilight_gateway::{Event, Shard};
use twilight_http::Client as HttpClient;
use twilight_lavalink::{http::LoadedTracks, model::Play, Lavalink};
use twilight_model::{
    channel::Message,
    gateway::payload::MessageCreate,
};

#[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);
    let user_id = http.current_user().await?.id;

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

    let mut shard = Shard::new(token);
    shard.start().await?;

    let mut events = shard.events();

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

    Ok(())
}

Dependencies

~8.5MB
~180K SLoC