Skip to content

Commit

Permalink
Potential implementation (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
NycroV committed Jun 25, 2024
1 parent a533a5f commit 5676f77
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
62 changes: 18 additions & 44 deletions src/Lavalink4NET.DSharpPlus/DiscordClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ namespace Lavalink4NET.DSharpPlus;
using global::DSharpPlus.Exceptions;
using global::DSharpPlus.Net.Abstractions;
using Lavalink4NET.Clients;
using Lavalink4NET.Clients.Events;
using L4N = Clients.Events;
using Lavalink4NET.Events;
using Microsoft.Extensions.Logging;

/// <summary>
/// Wraps a <see cref="DiscordClient"/> or <see cref="DiscordShardedClient"/> instance.
/// </summary>
public sealed class DiscordClientWrapper : IDiscordClientWrapper, IDisposable
public sealed class DiscordClientWrapper : IDiscordClientWrapper
{
/// <inheritdoc/>
public event AsyncEventHandler<VoiceServerUpdatedEventArgs>? VoiceServerUpdated;
public event AsyncEventHandler<L4N.VoiceServerUpdatedEventArgs>? VoiceServerUpdated;

/// <inheritdoc/>
public event AsyncEventHandler<VoiceStateUpdatedEventArgs>? VoiceStateUpdated;
public event AsyncEventHandler<L4N.VoiceStateUpdatedEventArgs>? VoiceStateUpdated;

private readonly object _client; // either DiscordShardedClient or DiscordClient
private readonly ILogger<DiscordClientWrapper> _logger;
private readonly TaskCompletionSource<ClientInformation> _readyTaskCompletionSource;
private bool _disposed;

private DiscordClientWrapper(object discordClient, ILogger<DiscordClientWrapper> logger)
{
Expand All @@ -49,11 +48,6 @@ private DiscordClientWrapper(object discordClient, ILogger<DiscordClientWrapper>
public DiscordClientWrapper(DiscordClient discordClient, ILogger<DiscordClientWrapper> logger)
: this((object)discordClient, logger)
{
ArgumentNullException.ThrowIfNull(discordClient);

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

/// <summary>
Expand All @@ -64,11 +58,6 @@ public DiscordClientWrapper(DiscordClient discordClient, ILogger<DiscordClientWr
public DiscordClientWrapper(DiscordShardedClient shardedDiscordClient, ILogger<DiscordClientWrapper> logger)
: this((object)shardedDiscordClient, logger)
{
ArgumentNullException.ThrowIfNull(shardedDiscordClient);

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

/// <inheritdoc/>
Expand Down Expand Up @@ -155,35 +144,14 @@ public ValueTask<ClientInformation> WaitForReadyAsync(CancellationToken cancella
return new(_readyTaskCompletionSource.Task.WaitAsync(cancellationToken));
}

/// <inheritdoc/>
public void Dispose()
{
if (_disposed)
{
return;
}

_disposed = true;

if (_client is DiscordClient discordClient)
{
discordClient.VoiceStateUpdated -= OnVoiceStateUpdated;
discordClient.VoiceServerUpdated -= OnVoiceServerUpdated;
discordClient.GuildDownloadCompleted -= OnGuildDownloadCompleted;
}
else if (_client is DiscordShardedClient shardedClient)
{
shardedClient.VoiceStateUpdated -= OnVoiceStateUpdated;
shardedClient.VoiceServerUpdated -= OnVoiceServerUpdated;
shardedClient.GuildDownloadCompleted -= OnGuildDownloadCompleted;
}
}

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

private Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs)
/// <summary>
/// Called when the Discord client finishes a guild download.
/// </summary>
public Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownloadCompletedEventArgs eventArgs)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(eventArgs);
Expand All @@ -197,7 +165,10 @@ private Task OnGuildDownloadCompleted(DiscordClient discordClient, GuildDownload
return Task.CompletedTask;
}

private async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdateEventArgs voiceServerUpdateEventArgs)
/// <summary>
/// Called when the Discord client's voice server is updated.
/// </summary>
public async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServerUpdatedEventArgs voiceServerUpdateEventArgs)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(voiceServerUpdateEventArgs);
Expand All @@ -206,7 +177,7 @@ private async Task OnVoiceServerUpdated(DiscordClient discordClient, VoiceServer
Token: voiceServerUpdateEventArgs.VoiceToken,
Endpoint: voiceServerUpdateEventArgs.Endpoint);

var eventArgs = new VoiceServerUpdatedEventArgs(
var eventArgs = new L4N.VoiceServerUpdatedEventArgs(
guildId: voiceServerUpdateEventArgs.Guild.Id,
voiceServer: server);

Expand All @@ -215,7 +186,10 @@ await VoiceServerUpdated
.ConfigureAwait(false);
}

private async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUpdateEventArgs voiceStateUpdateEventArgs)
/// <summary>
/// Called when the Discord client's voice state is updated.
/// </summary>
public async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUpdatedEventArgs voiceStateUpdateEventArgs)
{
ArgumentNullException.ThrowIfNull(discordClient);
ArgumentNullException.ThrowIfNull(voiceStateUpdateEventArgs);
Expand All @@ -234,7 +208,7 @@ private async Task OnVoiceStateUpdated(DiscordClient discordClient, VoiceStateUp
SessionId: sessionId);

// invoke event
var eventArgs = new VoiceStateUpdatedEventArgs(
var eventArgs = new L4N.VoiceStateUpdatedEventArgs(
guildId: voiceStateUpdateEventArgs.Guild.Id,
userId: voiceStateUpdateEventArgs.User.Id,
isCurrentUser: voiceStateUpdateEventArgs.User.Id == discordClient.CurrentUser.Id,
Expand Down
4 changes: 2 additions & 2 deletions src/Lavalink4NET.DSharpPlus/Lavalink4NET.DSharpPlus.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<!-- Package Description -->
<Description>High performance Lavalink wrapper for .NET | Add powerful audio playback to your DSharpPlus-based applications with this integration for Lavalink4NET. Suitable for end users developing with DSharpPlus.</Description>
Expand All @@ -10,7 +10,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.4.6" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02297" />
<ProjectReference Include="../Lavalink4NET/Lavalink4NET.csproj" />
</ItemGroup>
<Import Project="../Lavalink4NET.targets" />
Expand Down
20 changes: 16 additions & 4 deletions src/Lavalink4NET.DSharpPlus/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Lavalink4NET.Extensions;

using System;
using System;
using DSharpPlus.Extensions;
using Lavalink4NET.DSharpPlus;
using Microsoft.Extensions.DependencyInjection;

namespace Lavalink4NET.Extensions;

/// <summary>
/// A collection of extension methods for <see cref="IServiceCollection"/>.
/// </summary>
Expand All @@ -17,6 +18,17 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddLavalink(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
return services.AddLavalink<DiscordClientWrapper>();
services.AddLavalink<DiscordClientWrapper>();

services.Configure<DiscordClientWrapper>(client =>
services.ConfigureEventHandlers(events =>
{
events.HandleGuildDownloadCompleted(client.OnGuildDownloadCompleted);
events.HandleVoiceServerUpdated(client.OnVoiceServerUpdated);
events.HandleVoiceStateUpdated(client.OnVoiceStateUpdated);
})
);

return services;
}
}

0 comments on commit 5676f77

Please sign in to comment.