chore: port over events and update converter to handle them
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
namespace StevesBot.Worker.Tests.Unit;
|
||||
|
||||
public class LockReleaserTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldCreateInstance()
|
||||
{
|
||||
using var semaphore = new SemaphoreSlim(1);
|
||||
using var lockReleaser = new LockReleaser(semaphore);
|
||||
|
||||
lockReleaser.Should().NotBeNull();
|
||||
lockReleaser.Should().BeOfType<LockReleaser>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledAndSemaphoreIsNull_ItShouldThrowArgumentNullException()
|
||||
{
|
||||
var act = static () => new LockReleaser(null!);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenCalled_ItShouldReleaseSemaphore()
|
||||
{
|
||||
using var semaphore = new SemaphoreSlim(0, 1);
|
||||
using var lockReleaser = new LockReleaser(semaphore);
|
||||
|
||||
lockReleaser.Dispose();
|
||||
|
||||
semaphore.CurrentCount.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenCalledMultipleTimes_ItShouldReleaseSemaphoreOnlyOnce()
|
||||
{
|
||||
using var semaphore = new SemaphoreSlim(0, 1);
|
||||
using var lockReleaser = new LockReleaser(semaphore);
|
||||
|
||||
lockReleaser.Dispose();
|
||||
lockReleaser.Dispose();
|
||||
|
||||
semaphore.CurrentCount.Should().Be(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace StevesBot.Worker.Discord;
|
||||
|
||||
internal static class DiscordIntents
|
||||
{
|
||||
public const int Guilds = 1 << 0;
|
||||
public const int GuildMembers = 1 << 1;
|
||||
public const int GuildModeration = 1 << 2;
|
||||
public const int GuildExpressions = 1 << 3;
|
||||
public const int GuildIntegrations = 1 << 4;
|
||||
public const int GuildWebhooks = 1 << 5;
|
||||
public const int GuildInvites = 1 << 6;
|
||||
public const int GuildVoiceStates = 1 << 7;
|
||||
public const int GuildPresences = 1 << 8;
|
||||
public const int GuildMessages = 1 << 9;
|
||||
public const int GuildMessageReactions = 1 << 10;
|
||||
public const int GuildMessageTyping = 1 << 11;
|
||||
public const int DirectMessages = 1 << 12;
|
||||
public const int DirectMessageReactions = 1 << 13;
|
||||
public const int DirectMessageTyping = 1 << 14;
|
||||
public const int MessageContent = 1 << 15;
|
||||
public const int GuildScheduledEvents = 1 << 16;
|
||||
public const int AutoModerationConfiguration = 1 << 20;
|
||||
public const int AutoModerationExecution = 1 << 21;
|
||||
public const int GuildMessagePolls = 1 << 24;
|
||||
public const int DirectMessagePolls = 1 << 25;
|
||||
public const int All = Guilds
|
||||
| GuildMembers
|
||||
| GuildModeration
|
||||
| GuildExpressions
|
||||
| GuildIntegrations
|
||||
| GuildWebhooks
|
||||
| GuildInvites
|
||||
| GuildVoiceStates
|
||||
| GuildPresences
|
||||
| GuildMessages
|
||||
| GuildMessageReactions
|
||||
| GuildMessageTyping
|
||||
| DirectMessages
|
||||
| DirectMessageReactions
|
||||
| DirectMessageTyping
|
||||
| MessageContent
|
||||
| GuildScheduledEvents
|
||||
| AutoModerationConfiguration
|
||||
| AutoModerationExecution
|
||||
| GuildMessagePolls
|
||||
| DirectMessagePolls;
|
||||
}
|
||||
@@ -12,13 +12,26 @@ internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
||||
|
||||
return op switch
|
||||
{
|
||||
DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options),
|
||||
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
||||
DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize<HeartbeatAckDiscordEvent>(root.GetRawText(), options),
|
||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), options)
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DiscordEvent value, JsonSerializerOptions options)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
||||
|
||||
private static DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options)
|
||||
{
|
||||
var type = root.GetProperty("t").GetString();
|
||||
|
||||
return type switch
|
||||
{
|
||||
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), options),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal static class DiscordEventTypes
|
||||
{
|
||||
public const string Ready = "READY";
|
||||
}
|
||||
@@ -2,5 +2,8 @@ namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal static class DiscordOpCodes
|
||||
{
|
||||
public const int Dispatch = 0;
|
||||
public const int Heartbeat = 1;
|
||||
public const int HeartbeatAck = 11;
|
||||
public const int Hello = 10;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal record HeartbeatAckDiscordEvent : DiscordEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal record HeartbeatDiscordEvent : DiscordEvent
|
||||
{
|
||||
public HeartbeatDiscordEvent(int? sequence)
|
||||
{
|
||||
OpCode = DiscordOpCodes.Heartbeat;
|
||||
Sequence = sequence;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal record ReadyDiscordEvent : DiscordEvent
|
||||
{
|
||||
[JsonPropertyName("d")]
|
||||
public new ReadyData Data { get; init; } = new ReadyData();
|
||||
}
|
||||
|
||||
internal record ReadyData
|
||||
{
|
||||
[JsonPropertyName("v")]
|
||||
public int Version { get; init; }
|
||||
|
||||
[JsonPropertyName("session_id")]
|
||||
public string SessionId { get; init; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("resume_gateway_url")]
|
||||
public string ResumeGatewayUrl { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -7,6 +7,8 @@ internal sealed class LockReleaser : IDisposable
|
||||
|
||||
public LockReleaser(SemaphoreSlim semaphore)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(semaphore);
|
||||
|
||||
_semaphore = semaphore;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user