Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Improve message deserialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew4850 committed May 1, 2022
1 parent 4fd9208 commit e6b1f96
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/API/Discord.API/Models/Enums/Messages/ComponentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Quarrel © 2022

using System;
using System.Collections.Generic;
using System.Text;

namespace Discord.API.Models.Enums.Messages
{
public enum ComponentType
{
ActionRow = 1,
Button = 2,
SelectMenu = 3,
TextInput = 4
}
}
17 changes: 17 additions & 0 deletions src/API/Discord.API/Models/Enums/Messages/InterationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Quarrel © 2022

using System;
using System.Collections.Generic;
using System.Text;

namespace Discord.API.Models.Enums.Messages
{
public enum InterationType
{
PING = 1,
APPLICATION_COMMAND = 2,
MESSAGE_COMPONENT = 3,
APPLICATION_COMMAND_AUTOCOMPLETE = 4,
MODAL_SUBMIT = 5,
}
}
57 changes: 57 additions & 0 deletions src/API/Discord.API/Models/Enums/Messages/MessageFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Quarrel © 2022

using System;
using System.Collections.Generic;
using System.Text;

namespace Discord.API.Models.Enums.Messages
{
public enum MessageFlags
{
/// <summary>
/// This message has been published to subscribed channels (via Channel Following)
/// </summary>
CROSSPOSTED = 0,

/// <summary>
/// This message originated from a message in another channel (via Channel Following)
/// </summary>
IS_CROSSPOST = 1,

/// <summary>
/// Do not include any embeds when serializing this message
/// </summary>
SUPPRESS_EMBEDS = 1 << 2,

/// <summary>
/// The source message for this crosspost has been deleted (via Channel Following)
/// </summary>
SOURCE_MESSAGE_DELETED = 1 << 3,

/// <summary>
/// This message came from the urgent message system
/// </summary>
URGENT = 1 << 4,

/// <summary>
/// This message has an associated thread, with the same id as the message
/// </summary>
HAS_THREAD = 1 << 5,

/// <summary>
/// This message is only visible to the user who invoked the Interaction
/// </summary>
EPHEMERAL = 1 << 6,

/// <summary>
/// This message is an Interaction Response and the bot is "thinking"
/// </summary>
LOADING = 1 << 7,

/// <summary>
/// This message failed to mention some roles and add their members to the thread
/// </summary>
FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8,
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace Discord.API.Models.Enums.Stickers
/// </summary>
public enum StickerFormatType
{
/// <summary>
/// Not specified.
/// </summary>
None = 0,

/// <summary>
/// PNG file.
/// </summary>
Expand Down
77 changes: 77 additions & 0 deletions src/API/Discord.API/Models/Json/Applications/JsonApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Quarrel © 2022

using Discord.API.Models.Json.Users;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Discord.API.Models.Json.Applications
{
internal class JsonApplication
{
[JsonPropertyName("id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public ulong Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("icon")]
public string? Icon { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("rpc_origins")]
public List<string>? RpcOrigins { get; set; }

[JsonPropertyName("bot_public")]
public bool BotPublic { get; set; }

[JsonPropertyName("bot_require_code_grant")]
public bool BotRequireCodeGrant { get; set; }

[JsonPropertyName("terms_of_service_url")]
public string? TermsOfServiceUrl { get; set; }

[JsonPropertyName("privacy_policy_url")]
public string? PrivacyPolicyUrl { get; set; }

[JsonPropertyName("owner")]
public JsonUser? Owner { get; set; }

[JsonPropertyName("verify_key")]
public string VerifyKey { get; set; }

/*
[JsonPropertyName("team")]
public JsonTeam? Team { get; set; }
*/

[JsonPropertyName("guild_id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public ulong? GuildId { get; set; }

[JsonPropertyName("primary_sku_id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public ulong? PrimarySkuId { get; set; }

[JsonPropertyName("slug")]
public string? Slug { get; set; }

[JsonPropertyName("cover_image")]
public string? CoverImage { get; set; }

[JsonPropertyName("flags")]
public int? Flags { get; set; }

[JsonPropertyName("tags")]
public List<string>? Tags { get; set; }

/*
[JsonPropertyName("install_params")]
public JsonInstallParams? InstallParams { get; set; }
*/

[JsonPropertyName("custom_install_url")]
public string? CustomInstallUrl { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/API/Discord.API/Models/Json/Messages/JsonComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Quarrel © 2022

using Discord.API.Models.Enums.Messages;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Discord.API.Models.Json.Messages
{
internal class JsonComponent
{
[JsonPropertyName("type")]
public ComponentType Type { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/API/Discord.API/Models/Json/Messages/JsonMessage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Quarrel © 2022

using Discord.API.Models.Enums.Messages;
using Discord.API.Models.Json.Applications;
using Discord.API.Models.Json.Channels;
using Discord.API.Models.Json.Messages.Embeds;
using Discord.API.Models.Json.Reactions;
using Discord.API.Models.Json.Stickers;
using Discord.API.Models.Json.Users;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

// JSON models don't need to respect standard nullable rules.
Expand Down Expand Up @@ -77,10 +81,31 @@ internal class JsonMessage
[JsonPropertyName("activity")]
public JsonMessageActivity? Activity { get; set; }

[JsonPropertyName("application")]
public JsonApplication? Application { get; set; }

[JsonPropertyName("application_id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public long? ApplicationId { get; set; }

[JsonPropertyName("message_reference")]
public JsonMessageReference? Reference { get; set; }

[JsonPropertyName("flags")]
public MessageFlags? Flags { get; set; }

[JsonPropertyName("referenced_message")]
public JsonMessage? ReferencedMessage { get; set; }

[JsonPropertyName("interaction")]
public JsonMessageInteraction? Interaction { get; set; }

[JsonPropertyName("thread")]
public JsonChannel? Thread { get; set; }

[JsonPropertyName("components")]
public List<JsonMessageComponent>? Components { get; set; }

[JsonPropertyName("sticker_items")]
public List<JsonMessageStickerItem>? StickerItems { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/API/Discord.API/Models/Json/Messages/JsonMessageComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Quarrel © 2022

using Discord.API.Models.Enums.Messages;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Discord.API.Models.Json.Messages
{
internal class JsonMessageComponent
{
[JsonPropertyName("type")]
public ComponentType Type { get; set; }

[JsonPropertyName("components")]
public List<JsonComponent> Components { get; set; }
}
}
26 changes: 26 additions & 0 deletions src/API/Discord.API/Models/Json/Messages/JsonMessageInteraction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Quarrel © 2022

using Discord.API.Models.Enums.Messages;
using Discord.API.Models.Json.Users;
using System.Text.Json.Serialization;

namespace Discord.API.Models.Json.Messages
{
internal class JsonMessageInteraction
{
[JsonPropertyName("id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public ulong Id { get; set; }

[JsonPropertyName("type")]
public InterationType Type { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("user")]
public JsonUser User { get; set; }

[JsonPropertyName("member")]
public JsonGuildMember? Mmber { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/API/Discord.API/Models/Json/Messages/JsonMessageStickerItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Quarrel © 2022

using Discord.API.Models.Enums.Stickers;
using System.Text.Json.Serialization;

namespace Discord.API.Models.Json.Messages
{
internal class JsonMessageStickerItem
{
[JsonPropertyName("id"), JsonNumberHandling(Constants.ReadWriteAsString)]
public ulong Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("format_type")]
public StickerFormatType FormatType { get; set; }
}
}
8 changes: 6 additions & 2 deletions src/Quarrel.Client/Models/Messages/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal Message(JsonMessage jsonMessage, QuarrelClient context) :
Timestamp = jsonMessage.Timestamp ?? DateTimeOffset.MinValue;
EditedTimestamp = jsonMessage.EditedTimestamp;
Content = jsonMessage.Content ?? string.Empty;
Flags = jsonMessage.Flags;

if (jsonMessage.Author is not null)
{
Expand All @@ -44,7 +45,7 @@ internal Message(JsonMessage jsonMessage, QuarrelClient context) :
}
else
{
Mentions = new User[0];
Mentions = Array.Empty<User>();
}

if (jsonMessage.Attachments is not null)
Expand All @@ -57,7 +58,7 @@ internal Message(JsonMessage jsonMessage, QuarrelClient context) :
}
else
{
Attachments = new Attachment[0];
Attachments = Array.Empty<Attachment>();
}
}

Expand Down Expand Up @@ -94,5 +95,8 @@ internal Message(JsonMessage jsonMessage, QuarrelClient context) :

/// <inheritdoc/>
public Attachment[] Attachments { get; private set; }

/// <inheritdoc/>
public MessageFlags? Flags { get; private set; }
}
}

0 comments on commit e6b1f96

Please sign in to comment.