From 5676f77c776e8750e28dfe1c67b5e039ddce2bf4 Mon Sep 17 00:00:00 2001 From: Nycro Date: Tue, 25 Jun 2024 15:47:25 -0400 Subject: [PATCH] Potential implementation (untested) --- .../DiscordClientWrapper.cs | 62 ++++++------------- .../Lavalink4NET.DSharpPlus.csproj | 4 +- .../ServiceCollectionExtensions.cs | 20 ++++-- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/Lavalink4NET.DSharpPlus/DiscordClientWrapper.cs b/src/Lavalink4NET.DSharpPlus/DiscordClientWrapper.cs index 9c2d3770..62a3e734 100644 --- a/src/Lavalink4NET.DSharpPlus/DiscordClientWrapper.cs +++ b/src/Lavalink4NET.DSharpPlus/DiscordClientWrapper.cs @@ -10,25 +10,24 @@ namespace Lavalink4NET.DSharpPlus; using global::DSharpPlus.Exceptions; using global::DSharpPlus.Net.Abstractions; using Lavalink4NET.Clients; -using Lavalink4NET.Clients.Events; +using L4N = Clients.Events; using Lavalink4NET.Events; using Microsoft.Extensions.Logging; /// /// Wraps a or instance. /// -public sealed class DiscordClientWrapper : IDiscordClientWrapper, IDisposable +public sealed class DiscordClientWrapper : IDiscordClientWrapper { /// - public event AsyncEventHandler? VoiceServerUpdated; + public event AsyncEventHandler? VoiceServerUpdated; /// - public event AsyncEventHandler? VoiceStateUpdated; + public event AsyncEventHandler? VoiceStateUpdated; private readonly object _client; // either DiscordShardedClient or DiscordClient private readonly ILogger _logger; private readonly TaskCompletionSource _readyTaskCompletionSource; - private bool _disposed; private DiscordClientWrapper(object discordClient, ILogger logger) { @@ -49,11 +48,6 @@ private DiscordClientWrapper(object discordClient, ILogger public DiscordClientWrapper(DiscordClient discordClient, ILogger logger) : this((object)discordClient, logger) { - ArgumentNullException.ThrowIfNull(discordClient); - - discordClient.VoiceStateUpdated += OnVoiceStateUpdated; - discordClient.VoiceServerUpdated += OnVoiceServerUpdated; - discordClient.GuildDownloadCompleted += OnGuildDownloadCompleted; } /// @@ -64,11 +58,6 @@ public DiscordClientWrapper(DiscordClient discordClient, ILogger logger) : this((object)shardedDiscordClient, logger) { - ArgumentNullException.ThrowIfNull(shardedDiscordClient); - - shardedDiscordClient.VoiceStateUpdated += OnVoiceStateUpdated; - shardedDiscordClient.VoiceServerUpdated += OnVoiceServerUpdated; - shardedDiscordClient.GuildDownloadCompleted += OnGuildDownloadCompleted; } /// @@ -155,35 +144,14 @@ public ValueTask WaitForReadyAsync(CancellationToken cancella return new(_readyTaskCompletionSource.Task.WaitAsync(cancellationToken)); } - /// - public void Dispose() - { - if (_disposed) - { - return; - } - - _disposed = true; - - if (_client is DiscordClient discordClient) - { - discordClient.VoiceStateUpdated -= OnVoiceStateUpdated; - discordClient.VoiceServerUpdated -= OnVoiceServerUpdated; - discordClient.GuildDownloadCompleted -= OnGuildDownloadCompleted; - } - else if (_client is DiscordShardedClient shardedClient) - { - shardedClient.VoiceStateUpdated -= OnVoiceStateUpdated; - shardedClient.VoiceServerUpdated -= OnVoiceServerUpdated; - shardedClient.GuildDownloadCompleted -= OnGuildDownloadCompleted; - } - } - private DiscordClient GetClientForGuild(ulong guildId) => _client is DiscordClient discordClient ? discordClient : ((DiscordShardedClient)_client).GetShard(guildId); - private Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs) + /// + /// Called when the Discord client finishes a guild download. + /// + public Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs) { ArgumentNullException.ThrowIfNull(discordClient); ArgumentNullException.ThrowIfNull(eventArgs); @@ -197,7 +165,10 @@ private Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownload return Task.CompletedTask; } - private async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdateEventArgs voiceServerUpdateEventArgs) + /// + /// Called when the Discord client's voice server is updated. + /// + public async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdatedEventArgs voiceServerUpdateEventArgs) { ArgumentNullException.ThrowIfNull(discordClient); ArgumentNullException.ThrowIfNull(voiceServerUpdateEventArgs); @@ -206,7 +177,7 @@ private async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServer Token: voiceServerUpdateEventArgs.VoiceToken, Endpoint: voiceServerUpdateEventArgs.Endpoint); - var eventArgs = new VoiceServerUpdatedEventArgs( + var eventArgs = new L4N.VoiceServerUpdatedEventArgs( guildId: voiceServerUpdateEventArgs.Guild.Id, voiceServer: server); @@ -215,7 +186,10 @@ await VoiceServerUpdated .ConfigureAwait(false); } - private async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUpdateEventArgs voiceStateUpdateEventArgs) + /// + /// Called when the Discord client's voice state is updated. + /// + public async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUpdatedEventArgs voiceStateUpdateEventArgs) { ArgumentNullException.ThrowIfNull(discordClient); ArgumentNullException.ThrowIfNull(voiceStateUpdateEventArgs); @@ -234,7 +208,7 @@ private async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUp SessionId: sessionId); // invoke event - var eventArgs = new VoiceStateUpdatedEventArgs( + var eventArgs = new L4N.VoiceStateUpdatedEventArgs( guildId: voiceStateUpdateEventArgs.Guild.Id, userId: voiceStateUpdateEventArgs.User.Id, isCurrentUser: voiceStateUpdateEventArgs.User.Id == discordClient.CurrentUser.Id, diff --git a/src/Lavalink4NET.DSharpPlus/Lavalink4NET.DSharpPlus.csproj b/src/Lavalink4NET.DSharpPlus/Lavalink4NET.DSharpPlus.csproj index b74b8d78..534680ad 100644 --- a/src/Lavalink4NET.DSharpPlus/Lavalink4NET.DSharpPlus.csproj +++ b/src/Lavalink4NET.DSharpPlus/Lavalink4NET.DSharpPlus.csproj @@ -1,7 +1,7 @@  Library - net6.0;net7.0;net8.0 + net8.0 latest High performance Lavalink wrapper for .NET | Add powerful audio playback to your DSharpPlus-based applications with this integration for Lavalink4NET. Suitable for end users developing with DSharpPlus. @@ -10,7 +10,7 @@ true - + diff --git a/src/Lavalink4NET.DSharpPlus/ServiceCollectionExtensions.cs b/src/Lavalink4NET.DSharpPlus/ServiceCollectionExtensions.cs index f127068f..4fdd2c91 100644 --- a/src/Lavalink4NET.DSharpPlus/ServiceCollectionExtensions.cs +++ b/src/Lavalink4NET.DSharpPlus/ServiceCollectionExtensions.cs @@ -1,9 +1,10 @@ -namespace Lavalink4NET.Extensions; - -using System; +using System; +using DSharpPlus.Extensions; using Lavalink4NET.DSharpPlus; using Microsoft.Extensions.DependencyInjection; +namespace Lavalink4NET.Extensions; + /// /// A collection of extension methods for . /// @@ -17,6 +18,17 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddLavalink(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); - return services.AddLavalink(); + services.AddLavalink(); + + services.Configure(client => + services.ConfigureEventHandlers(events => + { + events.HandleGuildDownloadCompleted(client.OnGuildDownloadCompleted); + events.HandleVoiceServerUpdated(client.OnVoiceServerUpdated); + events.HandleVoiceStateUpdated(client.OnVoiceStateUpdated); + }) + ); + + return services; } }