Skip to content
MrCakeSlayer edited this page Sep 24, 2022 · 2 revisions

Creating an instance

Creating an instance of the LavalinkNode class

var audioService = new LavalinkNode(new LavalinkNodeOptions
{
    RestUri = "http://localhost:2333/",
    WebSocketUri = "ws://localhost:2333/",
    Password = "youshallnotpass"
}, new DiscordClientWrapper(client));

To start a new connection to a Lavalink node. You have to create an instance of the LavalinkNode class.

See right for an example.

The LavalinkNode constructor requires options and a discord client wrapper (, optional a cache and a logger implementation).

LavalinkNode(LavalinkNodeOptions options, IDiscordClientWrapper client, ILogger logger = null, ILavalinkCache cache = null)`

There is no implementation of an IDiscordClientWrapper in Lavalink4NET. You have to install Lavalink4NET.Discord_NET or Lavalink4NET.DSharpPlus or you have to implement your own Discord Client.

Constructor Parameters

Usage with Dependency Injection (Microsoft.Extensions.DependencyInjection)

using Microsoft.Extensions.DependencyInjection; // NuGet: Microsoft.Extensions.DependencyInjection
using Lavalink4NET;                             // NuGet: Lavalink4NET
using Lavalink4NET.Discord_NET;                 // NuGet: Lavalink4NET.Discord_NET
using Discord;                                  // NuGet: Discord.Net

var serviceProvider = new ServiceCollection()
    // [ Other services ]
    .AddSingleton<DiscordSocketClient>()
    .AddSingleton<IDiscordClientWrapper, DiscordClientWrapper>()
    .AddSingleton<IAudioService, LavalinkNode>()

    .AddSingleton(new LavalinkNodeOptions // consider using a configuration
    {
        RestUri = "http://localhost:2333/",
        WebSocketUri = "ws://localhost:2333/",
        Password = "youshallnotpass"
    })
    // [ Other services ]
    .BuildServiceProvider();

var audioService = serviceProvider.GetRequiredService<IAudioService>();
// do something .. don't forget disposing serviceProvider!
Name Description
options The options for the node. More description below.
client The discord client wrapper for interacting with the Discord API.
logger An optional logger callback interface.
cache An optional cache (to cache track searches / loads)
CacheTime The time how long the request should be cached. Only takes effect when a cache is specified in the constructor.
Decompression A value indicating whether the HTTP client should accept compressed HTTP payloads.
UserAgent The user agent header value; or null to disable the addition of the header.

Options

Example Options

var options = new LavalinkNodeOptions
{
    RestUri = "http://localhost:2333/",
    WebSocketUri = "ws://localhost:2333/",
    Password = "youshallnotpass",
    AllowResuming = true,
    BufferSize = 1024 * 1024 // 1 MiB
    DisconnectOnStop = false,
    ReconnectStrategy = ReconnectStrategies.DefaultStrategy,
    DebugPayloads = false
};
Name Description Default
AllowResuming A value indicating whether it should be tried to resume the Lavalink server session if the connection was lost. true
BufferSize The internal receive buffer size (buffer can overflow and will be stored in a infinity-expanding buffer) 1048576 (1 MiB)
DisconnectOnStop A value indicating whether a player should disconnect if the playing track stops and there are no enqueued. true
ReconnectStrategy The reconnect strategy to use when the connection to the Lavalink server was lost. ReconnectStrategies.DefaultStrategy
WebSocketUri The WebSocket Endpoint URI of the Lavalink server. ws://localhost:2333/
DebugPayloads A value indicating whether the payload I/O should be logged. false
Password The lavalink node password (see your application.yml) youshallnotpass
RestUri The RESTful HTTP API endpoint URI of the Lavalink node(see your application.yml) http://localhost:2333/

Using the Node

Initializing the Lavalink Node

// Get required services
var discordClient = serviceProvider.GetRequiredService<DiscordSocketClient>();
var audioService = serviceProvider.GetRequiredService<IAudioService>();

// Note: Do not hard-code your token, consider using an external file which
// is not being deployed to the VCS. Treat your token as your password.
await _client.LoginAsync(TokenType.Bot, "[Your Token]");
await _client.StartAsync();

// Initialize Lavalink Node connection
discordClient.Ready += () => audioService.InitializeAsync();

Before using the node, you have to initialize it.

LavalinkNode#InitializeAsync()

You must connect to the Discord Gateway first. It is recommended to initialize the node in the Ready event of the discord client.

Okay, done! Before running the program make sure that your lavalink server is running.