Skip to content

Commit

Permalink
Merge pull request #165 from NycroV/dev
Browse files Browse the repository at this point in the history
Implement proper shard count retrieval
  • Loading branch information
angelobreuer authored Jul 22, 2024
2 parents 3522391 + 80f104b commit cf979d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Lavalink4NET.DSharpPlus.Nightly/DSharpPlusUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace Lavalink4NET.DSharpPlus;
using System.Collections.Concurrent;
using System.Reflection;
using global::DSharpPlus;
using global::DSharpPlus.Clients;
using global::DSharpPlus.AsyncEvents;
using System.Threading.Tasks;

/// <summary>
/// An utility for getting internal / private fields from DSharpPlus WebSocket Gateway Payloads.
Expand Down Expand Up @@ -54,4 +56,38 @@ public static IClientErrorHandler GetErrorHandler(this DiscordClient client)
/// <param name="asyncEvent">the instance</param>
/// <param name="delegate">the event to register</param>
public static void Register(this AsyncEvent asyncEvent, Delegate @delegate) => asyncEventRegisterMethod.Invoke(asyncEvent, [@delegate]);

/// <summary>
/// The internal "orchestrator" property info in <see cref="DiscordClient"/>.
/// </summary>
// https://github.com/DSharpPlus/DSharpPlus/blob/master/DSharpPlus/Clients/DiscordClient.cs#L47
private static readonly FieldInfo orchestratorField =
typeof(DiscordClient).GetField("orchestrator", BindingFlags.NonPublic | BindingFlags.Instance)!;

/// <summary>
/// The internal "shardCount" property info in <see cref="MultiShardOrchestrator"/>.
/// </summary>
// https://github.com/DSharpPlus/DSharpPlus/blob/master/DSharpPlus/Clients/DiscordClient.cs#L47
private static readonly FieldInfo shardCountField =
typeof(MultiShardOrchestrator).GetField("shardCount", BindingFlags.NonPublic | BindingFlags.Instance)!;

/// <summary>
/// Gets the number of connected shards or this client
/// </summary>
public static async ValueTask<int> GetShardCountAsync(this DiscordClient client)
{
var orchestrator = (IShardOrchestrator)orchestratorField.GetValue(client)!;

if (orchestrator is SingleShardOrchestrator)
return 1;

if (orchestrator is MultiShardOrchestrator multiShardOrchestrator)
return (int)(uint)shardCountField.GetValue(multiShardOrchestrator)!;

// If the orchestrator is neither a Single nor Multi sharded orchestrator, that means it
// is using a custom solution implemented by the end user. There is no way to directly access
// the shard count in this case, so instead we estimate it by using Discord's recommended
// shard amount.
return (await client.GetGatewayInfoAsync()).ShardCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private async Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDo
var clientInformation = new ClientInformation(
Label: "DSharpPlus",
CurrentUserId: discordClient.CurrentUser.Id,
ShardCount: (await discordClient.GetGatewayInfoAsync()).ShardCount);
ShardCount: await discordClient.GetShardCountAsync());

_readyTaskCompletionSource.TrySetResult(clientInformation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackAsTool>False</PackAsTool>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02306" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02324" />
<ProjectReference Include="../Lavalink4NET/Lavalink4NET.csproj" />
</ItemGroup>
<Import Project="../Lavalink4NET.targets" />
Expand Down

0 comments on commit cf979d6

Please sign in to comment.