From 9d924354934d72be1c6827aae716a8832a9cc1f4 Mon Sep 17 00:00:00 2001 From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> Date: Sun, 16 Jun 2024 20:33:40 +0300 Subject: [PATCH] [Feature] Add purchase notification (#2942) --- .../Entities/Messages/GuildProductPurchase.cs | 23 +++++++++++++++++++ .../Entities/Messages/IMessage.cs | 5 ++++ .../Entities/Messages/PurchaseNotification.cs | 23 +++++++++++++++++++ .../Entities/Messages/PurchaseType.cs | 12 ++++++++++ .../API/Common/GuildProductPurchase.cs | 12 ++++++++++ src/Discord.Net.Rest/API/Common/Message.cs | 3 +++ .../API/Common/MessagePurchaseNotification.cs | 12 ++++++++++ .../Entities/Messages/RestMessage.cs | 12 ++++++++++ .../Entities/Messages/SocketMessage.cs | 11 +++++++++ 9 files changed, 113 insertions(+) create mode 100644 src/Discord.Net.Core/Entities/Messages/GuildProductPurchase.cs create mode 100644 src/Discord.Net.Core/Entities/Messages/PurchaseNotification.cs create mode 100644 src/Discord.Net.Core/Entities/Messages/PurchaseType.cs create mode 100644 src/Discord.Net.Rest/API/Common/GuildProductPurchase.cs create mode 100644 src/Discord.Net.Rest/API/Common/MessagePurchaseNotification.cs diff --git a/src/Discord.Net.Core/Entities/Messages/GuildProductPurchase.cs b/src/Discord.Net.Core/Entities/Messages/GuildProductPurchase.cs new file mode 100644 index 00000000..398d1da0 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Messages/GuildProductPurchase.cs @@ -0,0 +1,23 @@ +namespace Discord; + +/// +/// Represents a guild product purchase. +/// +public readonly struct GuildProductPurchase +{ + /// + /// Gets the ID of the listing. + /// + public readonly ulong ListingId; + + /// + /// Gets the name of the product. + /// + public readonly string ProductName; + + internal GuildProductPurchase(ulong listingId, string productName) + { + ListingId = listingId; + ProductName = productName; + } +} diff --git a/src/Discord.Net.Core/Entities/Messages/IMessage.cs b/src/Discord.Net.Core/Entities/Messages/IMessage.cs index 37f98744..f04b778a 100644 --- a/src/Discord.Net.Core/Entities/Messages/IMessage.cs +++ b/src/Discord.Net.Core/Entities/Messages/IMessage.cs @@ -226,6 +226,11 @@ namespace Discord /// MessageRoleSubscriptionData RoleSubscriptionData { get; } + /// + /// Gets the purchase notification for this message. + /// + PurchaseNotification PurchaseNotification { get; } + /// /// Gets the call data of the message. /// diff --git a/src/Discord.Net.Core/Entities/Messages/PurchaseNotification.cs b/src/Discord.Net.Core/Entities/Messages/PurchaseNotification.cs new file mode 100644 index 00000000..5c801c1c --- /dev/null +++ b/src/Discord.Net.Core/Entities/Messages/PurchaseNotification.cs @@ -0,0 +1,23 @@ +namespace Discord; + +/// +/// Represents a purchase notification. +/// +public readonly struct PurchaseNotification +{ + /// + /// Gets the type of the purchase. + /// + public readonly PurchaseType Type; + + /// + /// Gets the purchased product. + /// + public readonly GuildProductPurchase? ProductPurchase; + + internal PurchaseNotification(PurchaseType type, GuildProductPurchase? productPurchase) + { + Type = type; + ProductPurchase = productPurchase; + } +} diff --git a/src/Discord.Net.Core/Entities/Messages/PurchaseType.cs b/src/Discord.Net.Core/Entities/Messages/PurchaseType.cs new file mode 100644 index 00000000..72818f0e --- /dev/null +++ b/src/Discord.Net.Core/Entities/Messages/PurchaseType.cs @@ -0,0 +1,12 @@ +namespace Discord; + +/// +/// Represents the type of purchase notification. +/// +public enum PurchaseType +{ + /// + /// A guild product purchase. + /// + GuildProduct = 0 +} diff --git a/src/Discord.Net.Rest/API/Common/GuildProductPurchase.cs b/src/Discord.Net.Rest/API/Common/GuildProductPurchase.cs new file mode 100644 index 00000000..15b11661 --- /dev/null +++ b/src/Discord.Net.Rest/API/Common/GuildProductPurchase.cs @@ -0,0 +1,12 @@ +using Newtonsoft.Json; + +namespace Discord.API; + +internal class GuildProductPurchase +{ + [JsonProperty("listing_id")] + public ulong ListingId { get; set; } + + [JsonProperty("product_name")] + public string ProductName { get; set; } +} diff --git a/src/Discord.Net.Rest/API/Common/Message.cs b/src/Discord.Net.Rest/API/Common/Message.cs index 8bd46ae6..655b37b8 100644 --- a/src/Discord.Net.Rest/API/Common/Message.cs +++ b/src/Discord.Net.Rest/API/Common/Message.cs @@ -105,6 +105,9 @@ internal class Message [JsonProperty("poll")] public Optional Poll { get; set; } + [JsonProperty("purchase_notification")] + public Optional PurchaseNotification { get; set; } + [JsonProperty("call")] public Optional Call { get; set; } } diff --git a/src/Discord.Net.Rest/API/Common/MessagePurchaseNotification.cs b/src/Discord.Net.Rest/API/Common/MessagePurchaseNotification.cs new file mode 100644 index 00000000..8fb80023 --- /dev/null +++ b/src/Discord.Net.Rest/API/Common/MessagePurchaseNotification.cs @@ -0,0 +1,12 @@ +using Newtonsoft.Json; + +namespace Discord.API; + +internal class MessagePurchaseNotification +{ + [JsonProperty("type")] + public PurchaseType Type { get; set; } + + [JsonProperty("guild_product_purchase")] + public Optional ProductPurchase { get; set; } +} diff --git a/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs b/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs index e694ae99..fb1840ff 100644 --- a/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs +++ b/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs @@ -93,6 +93,9 @@ namespace Discord.Rest /// public MessageRoleSubscriptionData RoleSubscriptionData { get; private set; } + /// + public PurchaseNotification PurchaseNotification { get; private set; } + /// public MessageCallData? CallData { get; private set; } @@ -276,9 +279,18 @@ namespace Discord.Rest if (model.Thread.IsSpecified) Thread = RestThreadChannel.Create(Discord, new RestGuild(Discord, model.Thread.Value.GuildId.Value), model.Thread.Value); + if (model.PurchaseNotification.IsSpecified) + { + PurchaseNotification = new PurchaseNotification(model.PurchaseNotification.Value.Type, + model.PurchaseNotification.Value.ProductPurchase.IsSpecified + ? new GuildProductPurchase(model.PurchaseNotification.Value.ProductPurchase.Value.ListingId, model.PurchaseNotification.Value.ProductPurchase.Value.ProductName) + : null); + } + if (model.Call.IsSpecified) CallData = new MessageCallData(model.Call.Value.Participants, model.Call.Value.EndedTimestamp.ToNullable()); } + /// public async Task UpdateAsync(RequestOptions options = null) { diff --git a/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs b/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs index 440c5f43..9f7dbc43 100644 --- a/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs +++ b/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs @@ -81,6 +81,9 @@ namespace Discord.WebSocket /// public MessageRoleSubscriptionData RoleSubscriptionData { get; private set; } + /// + public PurchaseNotification PurchaseNotification { get; private set; } + /// public SocketThreadChannel Thread { get; private set; } @@ -303,6 +306,14 @@ namespace Discord.WebSocket Thread = guild?.AddOrUpdateChannel(state, model.Thread.Value) as SocketThreadChannel; } + if (model.PurchaseNotification.IsSpecified) + { + PurchaseNotification = new PurchaseNotification(model.PurchaseNotification.Value.Type, + model.PurchaseNotification.Value.ProductPurchase.IsSpecified + ? new GuildProductPurchase(model.PurchaseNotification.Value.ProductPurchase.Value.ListingId, model.PurchaseNotification.Value.ProductPurchase.Value.ProductName) + : null); + } + if (model.Call.IsSpecified) CallData = new MessageCallData(model.Call.Value.Participants, model.Call.Value.EndedTimestamp.ToNullable()); }