Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading