pub struct Lavalink { /* private fields */ }
Expand description
The lavalink client that manages nodes, players, and processes events from Discord to tie it all together.
Note: You must call the process
method with every Voice State Update
and Voice Server Update event you receive from Discord. It will
automatically forward these events to Lavalink. See its documentation for
more information.
You can retrieve players using the player
method. Players contain
information about the active playing information of a guild and allows you to send events to the
connected node, such as Play
events.
§Using a Lavalink client in multiple tasks
To use a Lavalink client instance in multiple tasks, consider wrapping it in
an std::sync::Arc
or std::rc::Rc
.
Implementations§
Source§impl Lavalink
impl Lavalink
Sourcepub fn new(user_id: Id<UserMarker>, shard_count: u32) -> Self
pub fn new(user_id: Id<UserMarker>, shard_count: u32) -> Self
Create a new Lavalink client instance.
The user ID and number of shards provided may not be modified during
runtime, and the client must be re-created. These parameters are
automatically passed to new nodes created via add
.
See also new_with_resume
, which allows you to specify session resume
capability.
Sourcepub fn new_with_resume(
user_id: Id<UserMarker>,
shard_count: u32,
resume: impl Into<Option<Resume>>,
) -> Self
pub fn new_with_resume( user_id: Id<UserMarker>, shard_count: u32, resume: impl Into<Option<Resume>>, ) -> Self
Sourcepub async fn process(&self, event: &Event) -> Result<(), ClientError>
pub async fn process(&self, event: &Event) -> Result<(), ClientError>
Process an event into the Lavalink client.
Note: calling this method in your event loop is required. See the crate documentation for an example.
This requires the VoiceServerUpdate
and VoiceStateUpdate
events that
you receive from Discord over the gateway to send voice updates to
nodes. For simplicity in some applications’ event loops, any event can
be provided, but they will just be ignored.
The Ready event can optionally be provided to do some cleaning of stalled voice states that never received their voice server update half or vice versa. It is recommended that you process Ready events.
§Errors
Returns a ClientErrorType::NodesUnconfigured
error type if no nodes
have been added to the client when attempting to retrieve a guild’s
player.
Sourcepub async fn add(
&self,
address: SocketAddr,
authorization: impl Into<String>,
) -> Result<(Arc<Node>, IncomingEvents), NodeError>
pub async fn add( &self, address: SocketAddr, authorization: impl Into<String>, ) -> Result<(Arc<Node>, IncomingEvents), NodeError>
Add a new node to be managed by the Lavalink client.
If a node already exists with the provided address, then it will be replaced.
§Errors
See the errors section of Node::connect
.
Sourcepub fn remove(&self, address: SocketAddr) -> Option<(SocketAddr, Arc<Node>)>
pub fn remove(&self, address: SocketAddr) -> Option<(SocketAddr, Arc<Node>)>
Remove a node from the list of nodes being managed by the Lavalink client.
This does not disconnect the node. Use Lavalink::disconnect
instead.
or drop all Node
s.
The node is returned if it existed.
Sourcepub fn disconnect(&self, address: SocketAddr) -> bool
pub fn disconnect(&self, address: SocketAddr) -> bool
Remove a node from the list of nodes being managed by the Lavalink client and terminates the connection.
Use Lavalink::remove
if detaching a node from a Lavalink instance
is required without closing the underlying connection.
Returns whether the node has been removed and disconnected.
Sourcepub async fn best(&self) -> Result<Arc<Node>, ClientError>
pub async fn best(&self) -> Result<Arc<Node>, ClientError>
Determine the “best” node for new players according to available nodes’ penalty scores. Disconnected nodes will not be considered.
Refer to Node::penalty
for how this is calculated.
§Errors
Returns a ClientErrorType::NodesUnconfigured
error type if there are
no connected nodes available in the client.
Sourcepub const fn players(&self) -> &PlayerManager
pub const fn players(&self) -> &PlayerManager
Retrieve an immutable reference to the player manager.
Sourcepub async fn player(
&self,
guild_id: Id<GuildMarker>,
) -> Result<Arc<Player>, ClientError>
pub async fn player( &self, guild_id: Id<GuildMarker>, ) -> Result<Arc<Player>, ClientError>
Retrieve a player for the guild.
Creates a player configured to use the best available node if a player
for the guild doesn’t already exist. Use PlayerManager::get
to only
retrieve and not create.
§Errors
Returns a ClientError
with a ClientErrorType::NodesUnconfigured
type if no node has been configured via add
.