From 9f4abb0a1d7d595ae8269f9cfe7a105440f55d36 Mon Sep 17 00:00:00 2001 From: NycroV <83246959+NycroV@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:57:56 -0400 Subject: [PATCH] Change out reflection for dependency injection --- .../DSharpPlusUtilities.cs | 35 ------------------- .../DiscordClientWrapper.cs | 11 +++--- 2 files changed, 7 insertions(+), 39 deletions(-) delete mode 100644 src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs diff --git a/src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs b/src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs deleted file mode 100644 index afbfea71..00000000 --- a/src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace Lavalink4NET.DSharpPlus; - -using System.Reflection; -using global::DSharpPlus; -using global::DSharpPlus.Clients; - -/// -/// An utility for getting internal / private fields from DSharpPlus WebSocket Gateway Payloads. -/// -public static partial class DSharpPlusUtilities -{ - /// - /// The internal "orchestrator" property info in . - /// - private static readonly FieldInfo orchestratorField = - typeof(DiscordClient).GetField("orchestrator", BindingFlags.NonPublic | BindingFlags.Instance)!; - - /// - /// Gets the amount of shards handled by this client's orchestrator. - /// - public static int GetConnectedShardCount(this DiscordClient client) - { - var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(client)!; - return orchestrator.ConnectedShardCount; - } - - /// - /// Gets the total amount of shards connected to this bot. - /// - public static int GetTotalShardCount(this DiscordClient client) - { - var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(client)!; - return orchestrator.TotalShardCount; - } -} diff --git a/src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs b/src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs index dd095220..60a9fb7d 100644 --- a/src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs +++ b/src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs @@ -14,7 +14,6 @@ namespace Lavalink4NET.DSharpPlus; using Lavalink4NET = Clients.Events; using Lavalink4NET.Events; using Microsoft.Extensions.Logging; -using System.Reflection; /// /// Wraps a instance. @@ -28,6 +27,7 @@ public sealed class DiscordClientWrapper : IDiscordClientWrapper public event AsyncEventHandler? VoiceStateUpdated; private readonly DiscordClient _client; + private readonly IShardOrchestrator _shardOrchestrator; private readonly ILogger _logger; private readonly TaskCompletionSource _readyTaskCompletionSource; @@ -35,13 +35,16 @@ public sealed class DiscordClientWrapper : IDiscordClientWrapper /// Creates a new instance of . /// /// The Discord Client to wrap. - /// a logger associated with this wrapper. - public DiscordClientWrapper(DiscordClient discordClient, ILogger logger) + /// The Discord shard orchestrator associated with this client. + /// A logger associated with this wrapper. + public DiscordClientWrapper(DiscordClient discordClient, IShardOrchestrator shardOrchestrator, ILogger logger) { ArgumentNullException.ThrowIfNull(discordClient); + ArgumentNullException.ThrowIfNull(shardOrchestrator); ArgumentNullException.ThrowIfNull(logger); _client = discordClient; + _shardOrchestrator = shardOrchestrator; _logger = logger; _readyTaskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); } @@ -141,7 +144,7 @@ internal Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloa var clientInformation = new ClientInformation( Label: "DSharpPlus", CurrentUserId: discordClient.CurrentUser.Id, - ShardCount: discordClient.GetConnectedShardCount()); + ShardCount: _shardOrchestrator.ConnectedShardCount); _readyTaskCompletionSource.TrySetResult(clientInformation); return Task.CompletedTask;