Skip to content

Commit

Permalink
Prepare for incoming DSharpPlus IShardOrchestrator changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NycroV committed Aug 13, 2024
1 parent fa61e65 commit b52e265
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
35 changes: 35 additions & 0 deletions src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Lavalink4NET.DSharpPlus;

using System.Reflection;
using global::DSharpPlus;
using global::DSharpPlus.Clients;

/// <summary>
/// An utility for getting internal / private fields from DSharpPlus WebSocket Gateway Payloads.
/// </summary>
public static partial class DSharpPlusUtilities
{
/// <summary>
/// The internal "orchestrator" property info in <see cref="DiscordClient"/>.
/// </summary>
private static readonly FieldInfo orchestratorField =
typeof(DiscordClient).GetField("orchestrator", BindingFlags.NonPublic | BindingFlags.Instance)!;

/// <summary>
/// Gets the amount of shards handled by this client's orchestrator.
/// </summary>
public static int GetConnectedShardCount(this DiscordClient client)
{
var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(client)!;
return orchestrator.ConnectedShardCount;

Check failure on line 24 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

'IShardOrchestrator' does not contain a definition for 'ConnectedShardCount' and no accessible extension method 'ConnectedShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 24 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

'IShardOrchestrator' does not contain a definition for 'ConnectedShardCount' and no accessible extension method 'ConnectedShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 24 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Release)

'IShardOrchestrator' does not contain a definition for 'ConnectedShardCount' and no accessible extension method 'ConnectedShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 24 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Release)

'IShardOrchestrator' does not contain a definition for 'ConnectedShardCount' and no accessible extension method 'ConnectedShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)
}

/// <summary>
/// Gets the total amount of shards connected to this bot.
/// </summary>
public static int GetTotalShardCount(this DiscordClient client)
{
var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(client)!;
return orchestrator.TotalShardCount;

Check failure on line 33 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

'IShardOrchestrator' does not contain a definition for 'TotalShardCount' and no accessible extension method 'TotalShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

'IShardOrchestrator' does not contain a definition for 'TotalShardCount' and no accessible extension method 'TotalShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Release)

'IShardOrchestrator' does not contain a definition for 'TotalShardCount' and no accessible extension method 'TotalShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs

View workflow job for this annotation

GitHub Actions / build (Release)

'IShardOrchestrator' does not contain a definition for 'TotalShardCount' and no accessible extension method 'TotalShardCount' accepting a first argument of type 'IShardOrchestrator' could be found (are you missing a using directive or an assembly reference?)
}
}
30 changes: 3 additions & 27 deletions src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public sealed class DiscordClientWrapper : IDiscordClientWrapper
private readonly ILogger<DiscordClientWrapper> _logger;
private readonly TaskCompletionSource<ClientInformation> _readyTaskCompletionSource;

/// <summary>
/// Re-assign this delegate in the client configuration to change the way the connected shard count is retrived.<br/>
/// You will only need to do this if you are using a custom IShardOrchestrator for your client.
/// </summary>
public Func<Task<int>> GetShardCount { get; set; }

/// <summary>
/// Creates a new instance of <see cref="DiscordClientWrapper"/>.
/// </summary>
Expand All @@ -50,25 +44,6 @@ public DiscordClientWrapper(DiscordClient discordClient, ILogger<DiscordClientWr
_client = discordClient;
_logger = logger;
_readyTaskCompletionSource = new TaskCompletionSource<ClientInformation>(TaskCreationOptions.RunContinuationsAsynchronously);

FieldInfo orchestratorField = typeof(DiscordClient).GetField("orchestrator", BindingFlags.NonPublic | BindingFlags.Instance)!;
var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(discordClient)!;

if (orchestrator is SingleShardOrchestrator)
GetShardCount = () => Task.FromResult(1);

else if (orchestrator is MultiShardOrchestrator multiShardOrchestrator)
{
FieldInfo shardCountField = typeof(MultiShardOrchestrator).GetField("shardCount", BindingFlags.NonPublic | BindingFlags.Instance)!;
GetShardCount = () => Task.Run(() => (int)(uint)shardCountField.GetValue(multiShardOrchestrator)!);
}

else
{
GetShardCount = () => Task.Run(async () => (await discordClient.GetGatewayInfoAsync()).ShardCount);
_logger.LogInformation("The DiscordClient is configured to use a non-default Shard Orchestrator - " +
"make sure that this wrapper's GetShardCount property is configured to properly retrieve the shard count");
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -158,17 +133,18 @@ public ValueTask<ClientInformation> WaitForReadyAsync(CancellationToken cancella
return new(_readyTaskCompletionSource.Task.WaitAsync(cancellationToken));
}

internal async Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs)
internal Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(eventArgs);

var clientInformation = new ClientInformation(
Label: "DSharpPlus",
CurrentUserId: discordClient.CurrentUser.Id,
ShardCount: await GetShardCount());
ShardCount: discordClient.GetConnectedShardCount());

_readyTaskCompletionSource.TrySetResult(clientInformation);
return Task.CompletedTask;
}

internal async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdatedEventArgs voiceServerUpdateEventArgs)
Expand Down

0 comments on commit b52e265

Please sign in to comment.