Lavalink4NET icon indicating copy to clipboard operation
Lavalink4NET copied to clipboard

Lavalink4NET is a Lavalink wrapper with node clustering, caching and custom players for .NET with support for Discord.Net, DSharpPlus, Remora, and NetCord.

CodeFactor.io GitHub tag (latest SemVer) GitHub issues Azure DevOps builds (branch)

Lavalink4NET is a Lavalink wrapper with node clustering, caching and custom players for .NET with support for Discord.Net and DSharpPlus.

Features

  • 🔌 Asynchronous Interface
  • ⚖️ Node Clustering / Load Balancing
  • ✳️ Extensible
  • 🎤 Lyrics
  • 🗳️ Queueing / Voting-System
  • 🎵 Track Decoding and Encoding
  • 🔄 Auto-Reconnect and Resuming
  • 📝 Logging (optional)
  • Request Caching (optional)
  • ⏱️ Inactivity Tracking (optional)
  • 🖋️ Supports Lavalink plugins
  • 🎶 Custom players
  • 🖼️ Artwork resolution
  • 🎚️ Audio filter support
  • 📊 Statistics tracking support
  • Compatible with DSharpPlus and Discord.Net.

   and a lot more...

Lavalink4NET Support Server Banner

NuGet

Prerequisites

  • One or more Lavalink Nodes
  • .NET Core >= 2.0

Getting Started

You can use Lavalink4NET in 3 different modes: Cluster, Node and Rest.

  • Cluster is useful for combining a bunch of nodes to one to load balance.
  • Node is useful when you have a small discord bot with one Lavalink Node.
  • Rest is useful when you only want to resolve tracks.
Using the constructor

Here is an example, how you can create the AudioService for a single node:

var audioService = new LavalinkNode(new LavalinkNodeOptions
{
	RestUri = "http://localhost:8080/",
	WebSocketUri = "ws://localhost:8080/",
	Password = "youshallnotpass"
}, new DiscordClientWrapper(client));
Usage with Dependency Injection / IoC (recommended)
var serviceProvider = new ServiceCollection()
	.AddSingleton<IAudioService, LavalinkNode>()	
	.AddSingleton<IDiscordClientWrapper, DiscordClientWrapper>();
	.AddSingleton(new LavalinkNodeOptions {[...]})
	[...]
	.BuildServiceProvider();
	
var audioService = serviceProvider.GetRequiredService<IAudioService>();

// Do not forget disposing the service provider!

Lookup the LavalinkNodeOptions in the application.yml of your Lavalink Node(s).

Initializing the node

Before using the service you have to initialize it, this can be done using the Ready event of your discord client implementation:

client.Ready += () => audioService.InitializeAsync();
Joining a voice channel and playing a track
// get player
var player = _audioService.GetPlayer<LavalinkPlayer>(guildId) 
    ?? await _audioService.JoinAsync<LavalinkPlayer>(guildId, voiceChannelId);

// resolve a track from youtube
var myTrack = await _audioService.GetTrackAsync("<search query>", SearchMode.YouTube);

// play track
await player.PlayAsync(myTrack);

For more documentation, see: Lavalink4NET Wiki or you could also take a look at Upcoming Features.