angelobreuer / Lavalink4NET

Lavalink4NET is a Lavalink wrapper with node clustering, caching and custom players for .NET with support for Discord.Net, DSharpPlus, Remora, and NetCord.
https://lavalink4net.angelobreuer.de/
MIT License
150 stars 27 forks source link

Discord.NET NullReferenceException #88

Closed Aaalexer closed 2 years ago

Aaalexer commented 2 years ago

Bug description NullReferenceException while using first constructor (which accepts DiscordShardedClient only) of type DiscordClientWrapper (library - Discord.NET), line 94 in file "DiscordClientWrapper.cs". Constructor is placed in this file on line 52.

Reproduce

using Discord.WebSocket;
using Lavalink4NET.DiscordNet;

var Discord = new DiscordShardedClient();

var Wrapper = new DiscordClientWrapper(Discord);
angelobreuer commented 2 years ago

Hello @Aaalexer,

thank you for your issue! The discord client wrapper tries to retrieve information about the sharding options (e.g., how many shards are used, which shards are served by the current instance, ...). This information is suddenly only available after the client connected to the gateway.

In most use cases, Lavalink4NET is used in combination of dependency injection, where dependencies are constructed as needed. In those scenarios it is normal to use Lavalink4NET after the client is ready.

Try to connect to the gateway, and then wait for the ready event.

As far as I remember this issue came up some while ago, I will add a check in the constructor that notifies the user that the Lavalink client must be initialized after the discord client is ready. I will remove the prefetch of the shard count in the constructor, and add a notice if the user tries to use the Lavalink4NET client before the discord client is ready.