From 9683b3b7f45a5ac0e622e124675b08c74dc32ff4 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Sat, 25 Nov 2023 01:50:55 +0000 Subject: [PATCH] explicitly declare classes, fix warnings --- .../DiscordPackets/DiscordEvent.cs | 40 ++++++++++++------- .../DiscordPackets/DiscordResponse.cs | 26 +++++------- .../DiscordPackets/IDiscordMessage.cs | 12 ++++-- .../DiscordRpcClient.cs | 32 +++++++-------- 4 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordEvent.cs b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordEvent.cs index 5958c2e..5d9b7dd 100644 --- a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordEvent.cs +++ b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordEvent.cs @@ -6,25 +6,37 @@ namespace Artemis.Plugins.Modules.Discord.DiscordPackets; +[JsonConverter(typeof(JsonSubtypes), "evt")] +[KnownSubType(typeof(DiscordReadyEvent), DiscordRpcEvent.READY)] +[KnownSubType(typeof(DiscordVoiceSettingsUpdateEvent), DiscordRpcEvent.VOICE_SETTINGS_UPDATE)] +[KnownSubType(typeof(DiscordVoiceConnectionStatusEvent), DiscordRpcEvent.VOICE_CONNECTION_STATUS)] +[KnownSubType(typeof(DiscordNotificationCreateEvent), DiscordRpcEvent.NOTIFICATION_CREATE)] +[KnownSubType(typeof(DiscordSpeakingStopEvent), DiscordRpcEvent.SPEAKING_STOP)] +[KnownSubType(typeof(DiscordSpeakingStartEvent), DiscordRpcEvent.SPEAKING_START)] +[KnownSubType(typeof(DiscordVoiceChannelSelectEvent), DiscordRpcEvent.VOICE_CHANNEL_SELECT)] +[KnownSubType(typeof(DiscordVoiceStateCreateEvent), DiscordRpcEvent.VOICE_STATE_CREATE)] +[KnownSubType(typeof(DiscordVoiceStateUpdateEvent), DiscordRpcEvent.VOICE_STATE_UPDATE)] +[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_STATE_DELETE)] public class DiscordEvent : IDiscordMessage { - [JsonProperty("evt")] + [JsonProperty("evt")] public DiscordRpcEvent Event { get; set; } } -[JsonConverter(typeof(JsonSubtypes), "evt")] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.READY)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_SETTINGS_UPDATE)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_CONNECTION_STATUS)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.NOTIFICATION_CREATE)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.SPEAKING_STOP)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.SPEAKING_START)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_CHANNEL_SELECT)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_STATE_CREATE)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_STATE_UPDATE)] -[KnownSubType(typeof(DiscordEvent), DiscordRpcEvent.VOICE_STATE_DELETE)] -public class DiscordEvent : DiscordEvent +public abstract class DiscordEvent : DiscordEvent { #pragma warning disable CS8618 - public T Data { get; init; } + public T Data { get; set; } +#pragma warning restore CS8618 } + +public sealed class DiscordReadyEvent : DiscordEvent { } +public sealed class DiscordVoiceSettingsUpdateEvent : DiscordEvent { } +public sealed class DiscordVoiceConnectionStatusEvent : DiscordEvent { } +public sealed class DiscordNotificationCreateEvent : DiscordEvent { } +public sealed class DiscordSpeakingStartEvent : DiscordEvent { } +public sealed class DiscordSpeakingStopEvent : DiscordEvent { } +public sealed class DiscordVoiceChannelSelectEvent : DiscordEvent { } +public sealed class DiscordVoiceStateCreateEvent : DiscordEvent { } +public sealed class DiscordVoiceStateUpdateEvent : DiscordEvent { } +public sealed class DiscordVoiceStateDeleteEvent : DiscordEvent { } \ No newline at end of file diff --git a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordResponse.cs b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordResponse.cs index 62a0982..fc3e0c3 100644 --- a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordResponse.cs +++ b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordResponse.cs @@ -1,28 +1,22 @@ -using Artemis.Plugins.Modules.Discord.DiscordPackets.CommandData; -using Artemis.Plugins.Modules.Discord.Enums; -using JsonSubTypes; -using Newtonsoft.Json; -using System; -using static JsonSubTypes.JsonSubtypes; +using System; +using Artemis.Plugins.Modules.Discord.DiscordPackets.CommandData; namespace Artemis.Plugins.Modules.Discord.DiscordPackets; public class DiscordResponse : IDiscordMessage { - [JsonProperty("cmd")] - public DiscordRpcCommand Command { get; set; } - public Guid Nonce { get; set; } } -[JsonConverter(typeof(JsonSubtypes), "cmd")] -[KnownSubType(typeof(DiscordResponse), DiscordRpcCommand.AUTHORIZE)] -[KnownSubType(typeof(DiscordResponse), DiscordRpcCommand.AUTHENTICATE)] -[KnownSubType(typeof(DiscordResponse), DiscordRpcCommand.GET_VOICE_SETTINGS)] -[KnownSubType(typeof(DiscordResponse), DiscordRpcCommand.SUBSCRIBE)] -[KnownSubType(typeof(DiscordResponse), DiscordRpcCommand.GET_SELECTED_VOICE_CHANNEL)] -public class DiscordResponse : DiscordResponse +public abstract class DiscordResponse : DiscordResponse { #pragma warning disable CS8618 public T Data { get; init; } +#pragma warning restore CS8618 } + +public sealed class DiscordAuthorizeResponse : DiscordResponse { } +public sealed class DiscordAuthenticateResponse : DiscordResponse { } +public sealed class DiscordGetVoiceSettingsResponse : DiscordResponse { } +public sealed class DiscordSubscribeResponse : DiscordResponse { } +public sealed class DiscordGetSelectedVoiceChannelResponse : DiscordResponse { } \ No newline at end of file diff --git a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/IDiscordMessage.cs b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/IDiscordMessage.cs index a86de83..9c3908b 100644 --- a/src/Artemis.Plugins.Modules.Discord/DiscordPackets/IDiscordMessage.cs +++ b/src/Artemis.Plugins.Modules.Discord/DiscordPackets/IDiscordMessage.cs @@ -6,6 +6,12 @@ namespace Artemis.Plugins.Modules.Discord.DiscordPackets; [JsonConverter(typeof(JsonSubtypes), "cmd")] -[KnownSubType(typeof(DiscordEvent<>), DiscordRpcCommand.DISPATCH)] -[FallBackSubType(typeof(DiscordResponse<>))] -public interface IDiscordMessage { } +[KnownSubType(typeof(DiscordEvent), DiscordRpcCommand.DISPATCH)] +[KnownSubType(typeof(DiscordAuthorizeResponse), DiscordRpcCommand.AUTHORIZE)] +[KnownSubType(typeof(DiscordAuthenticateResponse), DiscordRpcCommand.AUTHENTICATE)] +[KnownSubType(typeof(DiscordGetVoiceSettingsResponse), DiscordRpcCommand.GET_VOICE_SETTINGS)] +[KnownSubType(typeof(DiscordSubscribeResponse), DiscordRpcCommand.SUBSCRIBE)] +[KnownSubType(typeof(DiscordGetSelectedVoiceChannelResponse), DiscordRpcCommand.GET_SELECTED_VOICE_CHANNEL)] +public interface IDiscordMessage +{ +} diff --git a/src/Artemis.Plugins.Modules.Discord/DiscordRpcClient.cs b/src/Artemis.Plugins.Modules.Discord/DiscordRpcClient.cs index 346d55a..25d70a4 100644 --- a/src/Artemis.Plugins.Modules.Discord/DiscordRpcClient.cs +++ b/src/Artemis.Plugins.Modules.Discord/DiscordRpcClient.cs @@ -182,7 +182,7 @@ private async Task AuthorizeAsync() var authorizeResponse = await SendRequestWithResponseTypeAsync( new DiscordRequest(DiscordRpcCommand.AUTHORIZE, ("client_id", _authClient.ClientId), - ("scopes", new string[] { "rpc", "identify", "rpc.notifications.read" })), + ("scopes", new[] { "rpc", "identify", "rpc.notifications.read" })), timeoutMs: 30000); //high timeout so the user has time to click the button await _authClient.GetAccessTokenAsync(authorizeResponse.Data.Code); @@ -225,7 +225,7 @@ private async Task HandleAuthenticationAsync() private async Task ReadLoop() { - while (!_cancellationTokenSource.IsCancellationRequested && _transport?.IsConnected == true) + while (!_cancellationTokenSource.IsCancellationRequested && _transport.IsConnected) { try { @@ -345,34 +345,34 @@ private void HandleEvent(DiscordEvent discordEvent) { switch (discordEvent) { - case DiscordEvent ready: + case DiscordReadyEvent ready: _readyTcs?.SetResult(ready.Data); break; - case DiscordEvent voice: + case DiscordVoiceSettingsUpdateEvent voice: VoiceSettingsUpdated?.Invoke(this, voice.Data); break; - case DiscordEvent voiceStatus: + case DiscordVoiceConnectionStatusEvent voiceStatus: VoiceConnectionStatusUpdated?.Invoke(this, voiceStatus.Data); break; - case DiscordEvent notif: + case DiscordNotificationCreateEvent notif: NotificationReceived?.Invoke(this, notif.Data); break; - case DiscordEvent stop when stop.Event == DiscordRpcEvent.SPEAKING_STOP: + case DiscordSpeakingStopEvent stop: SpeakingStopped?.Invoke(this, stop.Data); break; - case DiscordEvent start when start.Event == DiscordRpcEvent.SPEAKING_START: + case DiscordSpeakingStartEvent start: SpeakingStarted?.Invoke(this, start.Data); break; - case DiscordEvent voiceSelect: + case DiscordVoiceChannelSelectEvent voiceSelect: VoiceChannelUpdated?.Invoke(this, voiceSelect.Data); break; - case DiscordEvent voiceCreate when voiceCreate.Event == DiscordRpcEvent.VOICE_STATE_CREATE: + case DiscordVoiceStateCreateEvent voiceCreate: VoiceStateCreated?.Invoke(this, voiceCreate.Data); break; - case DiscordEvent voiceUpdate when voiceUpdate.Event == DiscordRpcEvent.VOICE_STATE_UPDATE: + case DiscordVoiceStateUpdateEvent voiceUpdate: VoiceStateUpdated?.Invoke(this, voiceUpdate.Data); break; - case DiscordEvent voiceDelete when voiceDelete.Event == DiscordRpcEvent.VOICE_STATE_DELETE: + case DiscordVoiceStateDeleteEvent voiceDelete: VoiceStateDeleted?.Invoke(this, voiceDelete.Data); break; default: @@ -383,7 +383,7 @@ private void HandleEvent(DiscordEvent discordEvent) private DateTime GetDiscordStartTime() { - var processNames = new string[] + var processNames = new[] { "discord", "discordptb", @@ -397,11 +397,11 @@ private DateTime GetDiscordStartTime() #region IDisposable - private bool disposedValue; + private bool _disposedValue; protected virtual void Dispose(bool disposing) { - if (!disposedValue) + if (!_disposedValue) { if (disposing) { @@ -440,7 +440,7 @@ protected virtual void Dispose(bool disposing) } } - disposedValue = true; + _disposedValue = true; } }