21 releases (7 breaking)

0.7.2 Nov 20, 2021
0.6.1 Sep 17, 2021
0.6.0 Jul 31, 2021
0.3.1 Mar 14, 2021
0.2.1 Nov 2, 2020

#31 in HTTP client

Download history 32/week @ 2021-08-09 27/week @ 2021-08-16 5/week @ 2021-08-23 3/week @ 2021-09-06 31/week @ 2021-09-13 18/week @ 2021-09-20 12/week @ 2021-09-27 24/week @ 2021-10-04 27/week @ 2021-10-11 20/week @ 2021-10-18 21/week @ 2021-10-25 37/week @ 2021-11-01 90/week @ 2021-11-08 82/week @ 2021-11-15 25/week @ 2021-11-22

111 downloads per month
Used in twilight

ISC license

1MB
28K 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 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.2" }

rustls

The rustls feature enables async-tungstenite's tokio-rustls 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, 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);
    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–12MB
~252K SLoC

*na