Skip to content

Commit

Permalink
explicitly declare classes, fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Nov 25, 2023
1 parent f6c9c91 commit 9683b3b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 49 deletions.
40 changes: 26 additions & 14 deletions src/Artemis.Plugins.Modules.Discord/DiscordPackets/DiscordEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserVoiceState>), DiscordRpcEvent.VOICE_STATE_DELETE)]
public class DiscordEvent : IDiscordMessage
{
[JsonProperty("evt")]
[JsonProperty("evt")]
public DiscordRpcEvent Event { get; set; }
}

[JsonConverter(typeof(JsonSubtypes), "evt")]
[KnownSubType(typeof(DiscordEvent<Ready>), DiscordRpcEvent.READY)]
[KnownSubType(typeof(DiscordEvent<VoiceSettings>), DiscordRpcEvent.VOICE_SETTINGS_UPDATE)]
[KnownSubType(typeof(DiscordEvent<VoiceConnectionStatus>), DiscordRpcEvent.VOICE_CONNECTION_STATUS)]
[KnownSubType(typeof(DiscordEvent<Notification>), DiscordRpcEvent.NOTIFICATION_CREATE)]
[KnownSubType(typeof(DiscordEvent<SpeakingStartStop>), DiscordRpcEvent.SPEAKING_STOP)]
[KnownSubType(typeof(DiscordEvent<SpeakingStartStop>), DiscordRpcEvent.SPEAKING_START)]
[KnownSubType(typeof(DiscordEvent<VoiceChannelSelect>), DiscordRpcEvent.VOICE_CHANNEL_SELECT)]
[KnownSubType(typeof(DiscordEvent<UserVoiceState>), DiscordRpcEvent.VOICE_STATE_CREATE)]
[KnownSubType(typeof(DiscordEvent<UserVoiceState>), DiscordRpcEvent.VOICE_STATE_UPDATE)]
[KnownSubType(typeof(DiscordEvent<UserVoiceState>), DiscordRpcEvent.VOICE_STATE_DELETE)]
public class DiscordEvent<T> : DiscordEvent
public abstract class DiscordEvent<T> : DiscordEvent
{
#pragma warning disable CS8618
public T Data { get; init; }
public T Data { get; set; }
#pragma warning restore CS8618
}

public sealed class DiscordReadyEvent : DiscordEvent<Ready> { }
public sealed class DiscordVoiceSettingsUpdateEvent : DiscordEvent<VoiceSettings> { }
public sealed class DiscordVoiceConnectionStatusEvent : DiscordEvent<VoiceConnectionStatus> { }
public sealed class DiscordNotificationCreateEvent : DiscordEvent<Notification> { }
public sealed class DiscordSpeakingStartEvent : DiscordEvent<SpeakingStartStop> { }
public sealed class DiscordSpeakingStopEvent : DiscordEvent<SpeakingStartStop> { }
public sealed class DiscordVoiceChannelSelectEvent : DiscordEvent<VoiceChannelSelect> { }
public sealed class DiscordVoiceStateCreateEvent : DiscordEvent<UserVoiceState> { }
public sealed class DiscordVoiceStateUpdateEvent : DiscordEvent<UserVoiceState> { }
public sealed class DiscordVoiceStateDeleteEvent : DiscordEvent<UserVoiceState> { }
Original file line number Diff line number Diff line change
@@ -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<Authorize>), DiscordRpcCommand.AUTHORIZE)]
[KnownSubType(typeof(DiscordResponse<Authenticate>), DiscordRpcCommand.AUTHENTICATE)]
[KnownSubType(typeof(DiscordResponse<VoiceSettings>), DiscordRpcCommand.GET_VOICE_SETTINGS)]
[KnownSubType(typeof(DiscordResponse<Subscription>), DiscordRpcCommand.SUBSCRIBE)]
[KnownSubType(typeof(DiscordResponse<SelectedVoiceChannel>), DiscordRpcCommand.GET_SELECTED_VOICE_CHANNEL)]
public class DiscordResponse<T> : DiscordResponse
public abstract class DiscordResponse<T> : DiscordResponse
{
#pragma warning disable CS8618
public T Data { get; init; }
#pragma warning restore CS8618
}

public sealed class DiscordAuthorizeResponse : DiscordResponse<Authorize> { }
public sealed class DiscordAuthenticateResponse : DiscordResponse<Authenticate> { }
public sealed class DiscordGetVoiceSettingsResponse : DiscordResponse<VoiceSettings> { }
public sealed class DiscordSubscribeResponse : DiscordResponse<Subscription> { }
public sealed class DiscordGetSelectedVoiceChannelResponse : DiscordResponse<SelectedVoiceChannel> { }
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
32 changes: 16 additions & 16 deletions src/Artemis.Plugins.Modules.Discord/DiscordRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private async Task AuthorizeAsync()
var authorizeResponse = await SendRequestWithResponseTypeAsync<Authorize>(
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);
Expand Down Expand Up @@ -225,7 +225,7 @@ private async Task<Authenticate> HandleAuthenticationAsync()

private async Task ReadLoop()
{
while (!_cancellationTokenSource.IsCancellationRequested && _transport?.IsConnected == true)
while (!_cancellationTokenSource.IsCancellationRequested && _transport.IsConnected)
{
try
{
Expand Down Expand Up @@ -345,34 +345,34 @@ private void HandleEvent(DiscordEvent discordEvent)
{
switch (discordEvent)
{
case DiscordEvent<Ready> ready:
case DiscordReadyEvent ready:
_readyTcs?.SetResult(ready.Data);
break;
case DiscordEvent<VoiceSettings> voice:
case DiscordVoiceSettingsUpdateEvent voice:
VoiceSettingsUpdated?.Invoke(this, voice.Data);
break;
case DiscordEvent<VoiceConnectionStatus> voiceStatus:
case DiscordVoiceConnectionStatusEvent voiceStatus:
VoiceConnectionStatusUpdated?.Invoke(this, voiceStatus.Data);
break;
case DiscordEvent<Notification> notif:
case DiscordNotificationCreateEvent notif:
NotificationReceived?.Invoke(this, notif.Data);
break;
case DiscordEvent<SpeakingStartStop> stop when stop.Event == DiscordRpcEvent.SPEAKING_STOP:
case DiscordSpeakingStopEvent stop:
SpeakingStopped?.Invoke(this, stop.Data);
break;
case DiscordEvent<SpeakingStartStop> start when start.Event == DiscordRpcEvent.SPEAKING_START:
case DiscordSpeakingStartEvent start:
SpeakingStarted?.Invoke(this, start.Data);
break;
case DiscordEvent<VoiceChannelSelect> voiceSelect:
case DiscordVoiceChannelSelectEvent voiceSelect:
VoiceChannelUpdated?.Invoke(this, voiceSelect.Data);
break;
case DiscordEvent<UserVoiceState> voiceCreate when voiceCreate.Event == DiscordRpcEvent.VOICE_STATE_CREATE:
case DiscordVoiceStateCreateEvent voiceCreate:
VoiceStateCreated?.Invoke(this, voiceCreate.Data);
break;
case DiscordEvent<UserVoiceState> voiceUpdate when voiceUpdate.Event == DiscordRpcEvent.VOICE_STATE_UPDATE:
case DiscordVoiceStateUpdateEvent voiceUpdate:
VoiceStateUpdated?.Invoke(this, voiceUpdate.Data);
break;
case DiscordEvent<UserVoiceState> voiceDelete when voiceDelete.Event == DiscordRpcEvent.VOICE_STATE_DELETE:
case DiscordVoiceStateDeleteEvent voiceDelete:
VoiceStateDeleted?.Invoke(this, voiceDelete.Data);
break;
default:
Expand All @@ -383,7 +383,7 @@ private void HandleEvent(DiscordEvent discordEvent)

private DateTime GetDiscordStartTime()
{
var processNames = new string[]
var processNames = new[]
{
"discord",
"discordptb",
Expand All @@ -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)
{
Expand Down Expand Up @@ -440,7 +440,7 @@ protected virtual void Dispose(bool disposing)
}
}

disposedValue = true;
_disposedValue = true;
}
}

Expand Down

0 comments on commit 9683b3b

Please sign in to comment.