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.
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.
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
:
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
.
Create a [client], add a [node], and give events to the client to [process] events:
```rust,norun use futuresutil::stream::StreamExt; use std::{ env, future::Future, net::SocketAddr, str::FromStr, }; use twilightgateway::{Event, Intents, Shard}; use twilighthttp::Client as HttpClient; use twilight_lavalink::{http::LoadedTracks, model::Play, Lavalink};
async fn main() -> anyhow::Result<()> { let token = env::var("DISCORDTOKEN")?; let lavalinkhost = SocketAddr::fromstr(&env::var("LAVALINKHOST")?)?; let lavalinkauth = env::var("LAVALINKAUTHORIZATION")?; let shard_count = 1u64;
let http = HttpClient::new(token.clone());
let user_id = http.current_user().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.