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

Update Lavalink4NET.DSharpPlus.Nightly to latest build #161

Merged
merged 4 commits into from
Jul 13, 2024
Merged
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
59 changes: 17 additions & 42 deletions src/Lavalink4NET.DSharpPlus.Nightly/DiscordClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Lavalink4NET.DSharpPlus;
using System.Collections.Concurrent;

/// <summary>
/// Wraps a <see cref="DiscordClient"/> or <see cref="DiscordShardedClient"/> instance.
/// Wraps a <see cref="DiscordClient"/> instance.
/// </summary>
public sealed class DiscordClientWrapper : IDiscordClientWrapper
{
Expand All @@ -27,31 +27,24 @@ public sealed class DiscordClientWrapper : IDiscordClientWrapper
/// <inheritdoc/>
public event AsyncEventHandler<L4N.VoiceStateUpdatedEventArgs>? VoiceStateUpdated;

private readonly object _client; // either DiscordShardedClient or DiscordClient
private readonly DiscordClient _client; // sharded clients are now also managed by the same DiscordClient type
private readonly ILogger<DiscordClientWrapper> _logger;
private readonly TaskCompletionSource<ClientInformation> _readyTaskCompletionSource;
private bool _disposed;

private DiscordClientWrapper(object discordClient, ILogger<DiscordClientWrapper> logger)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(logger);

_client = discordClient;
_logger = logger;

_readyTaskCompletionSource = new TaskCompletionSource<ClientInformation>(TaskCreationOptions.RunContinuationsAsynchronously);
}

/// <summary>
/// Creates a new instance of <see cref="DiscordClientWrapper"/>.
/// </summary>
/// <param name="discordClient">The Discord Client to wrap.</param>
/// <param name="logger">a logger associated with this wrapper.</param>
public DiscordClientWrapper(DiscordClient discordClient, ILogger<DiscordClientWrapper> logger)
: this((object)discordClient, logger)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(logger);

_client = discordClient;
_logger = logger;

_readyTaskCompletionSource = new TaskCompletionSource<ClientInformation>(TaskCreationOptions.RunContinuationsAsynchronously);

void AddEventHandler(Type eventArgsType, Delegate eventHandler)
{
Expand All @@ -73,21 +66,6 @@ void AddEventHandler(Type eventArgsType, Delegate eventHandler)
AddEventHandler(typeof(GuildDownloadCompletedEventArgs), new AsyncEventHandler<DiscordClient, GuildDownloadCompletedEventArgs>(OnGuildDownloadCompleted));
}

/// <summary>
/// Creates a new instance of <see cref="DiscordClientWrapper"/>.
/// </summary>
/// <param name="shardedDiscordClient">The Sharded Discord Client to wrap.</param>
/// <param name="logger">a logger associated with this wrapper.</param>
public DiscordClientWrapper(DiscordShardedClient shardedDiscordClient, ILogger<DiscordClientWrapper> logger)
: this((object)shardedDiscordClient, logger)
{
ArgumentNullException.ThrowIfNull(shardedDiscordClient);

shardedDiscordClient.VoiceStateUpdated += OnVoiceStateUpdated;
shardedDiscordClient.VoiceServerUpdated += OnVoiceServerUpdated;
shardedDiscordClient.GuildDownloadCompleted += OnGuildDownloadCompleted;
}

/// <inheritdoc/>
/// <exception cref="ObjectDisposedException">thrown if the instance is disposed</exception>
public async ValueTask<ImmutableArray<ulong>> GetChannelUsersAsync(
Expand All @@ -101,7 +79,7 @@ public async ValueTask<ImmutableArray<ulong>> GetChannelUsersAsync(
DiscordChannel channel;
try
{
channel = await GetClientForGuild(guildId)
channel = await _client
.GetChannelAsync(voiceChannelId)
.ConfigureAwait(false);

Expand Down Expand Up @@ -145,8 +123,6 @@ public async ValueTask SendVoiceUpdateAsync(
{
cancellationToken.ThrowIfCancellationRequested();

var client = GetClientForGuild(guildId);

var payload = new VoiceStateUpdatePayload
{
GuildId = guildId,
Expand All @@ -155,13 +131,17 @@ public async ValueTask SendVoiceUpdateAsync(
IsSelfDeafened = selfDeaf,
};

#pragma warning disable DSP0004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable CS0618 // This method should not be used unless you know what you're doing. Instead, look towards the other explicitly implemented methods which come with client-side validation.

// Jan 23, 2024, OoLunar: We're telling Discord that we're joining a voice channel.
// At the time of writing, both DSharpPlus.VoiceNext and DSharpPlus.VoiceLink™
// use this method to send voice state updates.
await client
.SendPayloadAsync(GatewayOpCode.VoiceStateUpdate, payload)
await _client
.SendPayloadAsync(GatewayOpCode.VoiceStateUpdate, payload, guildId)
.ConfigureAwait(false);

#pragma warning restore DSP0004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning restore CS0618 // This method should not be used unless you know what you're doing. Instead, look towards the other explicitly implemented methods which come with client-side validation.
}

Expand All @@ -172,22 +152,17 @@ public ValueTask<ClientInformation> WaitForReadyAsync(CancellationToken cancella
return new(_readyTaskCompletionSource.Task.WaitAsync(cancellationToken));
}

private DiscordClient GetClientForGuild(ulong guildId) => _client is DiscordClient discordClient
? discordClient
: ((DiscordShardedClient)_client).GetShard(guildId);

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

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

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

private async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdatedEventArgs voiceServerUpdateEventArgs)
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-02304" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02306" />
<ProjectReference Include="../Lavalink4NET/Lavalink4NET.csproj" />
</ItemGroup>
<Import Project="../Lavalink4NET.targets" />
Expand Down
Loading