Setting up DisCatSharp.Lavalink
Configuring Your Client
To begin using DisCatSharp's Lavalink client, you will need to add the DisCatSharp.Lavalink nuget package. Once installed, simply add these namespaces at the top of your bot file:
using DisCatSharp.Net;
using DisCatSharp.Lavalink;
After that, we will need to create a configuration for our extension to use. This is where the special values from the server configuration are used.
var endpoint = new ConnectionEndpoint
{
Hostname = "127.0.0.1", // From your server configuration.
Port = 2333 // From your server configuration
};
var lavalinkConfig = new LavalinkConfiguration
{
Password = "youshallnotpass", // From your server configuration.
RestEndpoint = endpoint,
SocketEndpoint = endpoint
};
Finally, initialize the extension.
var lavalink = Discord.UseLavalink();
Connecting with Lavalink
We are now ready to connect to the server. Call the Lavalink extension's connect method and pass the configuration. Make sure to call this after your Discord client connects. This can be called either directly after your client's connect method or in your client's ready event.
LavalinkNode = await Lavalink.ConnectAsync(lavalinkConfig);
Your main bot file should now look like this:
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using DisCatSharp;
using DisCatSharp.Net;
using DisCatSharp.Lavalink;
namespace FirstLavalinkBot;
public class Program
{
public static DiscordClient Discord { get; internal set; }
public static void Main(string[] args = null)
{
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
}
public static async Task MainAsync(string[] args)
{
Discord = new DiscordClient(new DiscordConfiguration
{
Token = "<token_here>",
TokenType = TokenType.Bot,
MinimumLogLevel = LogLevel.Debug
});
var endpoint = new ConnectionEndpoint
{
Hostname = "127.0.0.1", // From your server configuration.
Port = 2333 // From your server configuration
};
var lavalinkConfig = new LavalinkConfiguration
{
Password = "youshallnotpass", // From your server configuration.
RestEndpoint = endpoint,
SocketEndpoint = endpoint
};
var lavalink = Discord.UseLavalink();
await Discord.ConnectAsync();
await lavalink.ConnectAsync(lavalinkConfig); // Make sure this is after Discord.ConnectAsync().
await Task.Delay(-1);
}
}
We are now ready to start the bot. If everything is configured properly, you should see a Lavalink connection appear in your DisCatSharp console:
[2020-10-10 17:56:07 -04:00] [403 /LavalinkSessionConnected] [Debug] Connection to Lavalink established UwU
And a client connection appear in your Lavalink console:
INFO 4436 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 4436 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
INFO 4436 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
INFO 4436 --- [ XNIO-1 task-1] lavalink.server.io.RequestLoggingFilter : GET /version?trace=false, client=127.0.0.1
INFO 4436 --- [ XNIO-1 task-1] l.server.io.HandshakeInterceptorImpl : Incoming connection from /127.0.0.1:54649
INFO 4436 --- [ XNIO-1 task-1] lavalink.server.io.RequestLoggingFilter : GET /v4/websocket, client=127.0.0.1
INFO 4436 --- [ XNIO-1 task-1] lavalink.server.io.SocketServer : Connection successfully established from DisCatSharp.Lavalink/10.6.6+38c8062e7f88b2e9920a535637d4f3afccbbf205
We are now ready to set up some music commands!