chore: port over events and update converter to handle them
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user