GuildUpdatePacket and tests
This commit is contained in:
parent
073e17c96b
commit
c65c371307
@ -16,6 +16,67 @@ public class JsonTests
|
||||
_testOutputHelper = testOutputHelper;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GuildUpdateDeserialize()
|
||||
{
|
||||
string src = """
|
||||
{
|
||||
"op": 0,
|
||||
"t": "GUILD_UPDATE",
|
||||
"s": 3,
|
||||
"d":{
|
||||
"id": "197038439483310086",
|
||||
"name": "Discord Testers",
|
||||
"icon": "f64c482b807da4f539cff778d174971c",
|
||||
"description": "The official place to report Discord Bugs!",
|
||||
"splash": null,
|
||||
"discovery_splash": null,
|
||||
"features": [
|
||||
"ANIMATED_ICON",
|
||||
"VERIFIED",
|
||||
"NEWS",
|
||||
"VANITY_URL",
|
||||
"DISCOVERABLE",
|
||||
"MORE_EMOJI",
|
||||
"INVITE_SPLASH",
|
||||
"BANNER",
|
||||
"COMMUNITY"
|
||||
],
|
||||
"emojis": [],
|
||||
"banner": "9b6439a7de04f1d26af92f84ac9e1e4a",
|
||||
"owner_id": "73193882359173120",
|
||||
"application_id": null,
|
||||
"region": null,
|
||||
"afk_channel_id": null,
|
||||
"afk_timeout": 300,
|
||||
"system_channel_id": null,
|
||||
"widget_enabled": true,
|
||||
"widget_channel_id": null,
|
||||
"verification_level": 3,
|
||||
"roles": [],
|
||||
"default_message_notifications": 1,
|
||||
"mfa_level": 1,
|
||||
"explicit_content_filter": 2,
|
||||
"max_presences": 40000,
|
||||
"max_members": 250000,
|
||||
"vanity_url_code": "discord-testers",
|
||||
"premium_tier": 3,
|
||||
"premium_subscription_count": 33,
|
||||
"system_channel_flags": 0,
|
||||
"preferred_locale": "en-US",
|
||||
"rules_channel_id": "441688182833020939",
|
||||
"public_updates_channel_id": "281283303326089216",
|
||||
"safety_alerts_channel_id": "281283303326089216",
|
||||
"nsfw_level": 0
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
GatewayPacket? gateway_packet = JsonSerializer.Deserialize(src, SourceGenerationContext.Default.GatewayPacket);
|
||||
Assert.IsType<GuildUpdatePacket>(gateway_packet);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GuildCreateDeserialize()
|
||||
{
|
||||
@ -69,6 +130,7 @@ public class JsonTests
|
||||
"safety_alerts_channel_id": "281283303326089216",
|
||||
"joined_at": "2024-06-27T11:59:36Z",
|
||||
"large": false,
|
||||
"nsfw_level": 0,
|
||||
"member_count": 69,
|
||||
"voice_states": [
|
||||
{
|
||||
@ -132,7 +194,7 @@ public class JsonTests
|
||||
|
||||
GatewayPacket? gateway_packet = JsonSerializer.Deserialize(src, SourceGenerationContext.Default.GatewayPacket);
|
||||
Assert.IsType<GuildCreatePacket>(gateway_packet);
|
||||
Assert.IsType<GuildData>((gateway_packet as GuildCreatePacket)!.Data);
|
||||
Assert.IsType<GuildCreateData>((gateway_packet as GuildCreatePacket)!.Data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ namespace Discord.API;
|
||||
[JsonDerivedType(typeof(ChannelDeletePacket))]
|
||||
[JsonDerivedType(typeof(ReadyPacket))]
|
||||
[JsonDerivedType(typeof(GuildCreatePacket))]
|
||||
[JsonDerivedType(typeof(GuildUpdatePacket))]
|
||||
internal abstract class DispatchPacket : GatewayPacket
|
||||
{
|
||||
public override Opcode Op => Opcode.Dispatch;
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
internal class GuildUpdatePacket : DispatchPacket
|
||||
{
|
||||
public override string Event => "GUILD_UPDATE";
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("d")]
|
||||
public required GuildUpdateData Data { get; init; }
|
||||
}
|
||||
@ -54,6 +54,9 @@ internal class GatewayPacketConverter : JsonConverter<GatewayPacket>
|
||||
SourceGenerationContext.Default.ChannelDeletePacket),
|
||||
"GUILD_CREATE" => json_doc.Deserialize(
|
||||
SourceGenerationContext.Default.GuildCreatePacket),
|
||||
"GUILD_UPDATE" => json_doc.Deserialize(
|
||||
SourceGenerationContext.Default.GuildUpdatePacket
|
||||
),
|
||||
_ => throw new NotSupportedException($"Packet {event_name} is not supported in json deserialization")
|
||||
};
|
||||
default:
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
using System.Runtime.InteropServices.JavaScript;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
[JsonSourceGenerationOptions(IgnoreReadOnlyFields = false,
|
||||
IgnoreReadOnlyProperties = false,
|
||||
IncludeFields = true,
|
||||
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower,
|
||||
Converters = [typeof(GatewayPacketConverter), typeof(UnavailableGuildDataConverter)],
|
||||
Converters = [
|
||||
typeof(GatewayPacketConverter),
|
||||
typeof(UnavailableGuildDataConverter),
|
||||
typeof(GuildDataConverter)
|
||||
],
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||
)]
|
||||
[JsonSerializable(typeof(GatewayPacket))]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user