From a890de93044cb5db12e64fce203e718286d8dd83 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Fri, 27 May 2022 13:25:49 +0200 Subject: [PATCH] feature: better call control in ParseHttpInteraction (#2330) * Init * Fix channelid xmldoc --- src/Discord.Net.Rest/DiscordRestClient.cs | 6 +- .../Interactions/InteractionProperties.cs | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs diff --git a/src/Discord.Net.Rest/DiscordRestClient.cs b/src/Discord.Net.Rest/DiscordRestClient.cs index 7cb15bed1..daf7287c7 100644 --- a/src/Discord.Net.Rest/DiscordRestClient.cs +++ b/src/Discord.Net.Rest/DiscordRestClient.cs @@ -121,7 +121,7 @@ namespace Discord.Rest /// A that represents the incoming http interaction. /// /// Thrown when the signature doesn't match the public key. - public Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body, Func doApiCallOnCreation = null) + public Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body, Func doApiCallOnCreation = null) => ParseHttpInteractionAsync(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body), doApiCallOnCreation); /// @@ -135,7 +135,7 @@ namespace Discord.Rest /// A that represents the incoming http interaction. /// /// Thrown when the signature doesn't match the public key. - public async Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body, Func doApiCallOnCreation = null) + public async Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body, Func doApiCallOnCreation = null) { if (!IsValidHttpInteraction(publicKey, signature, timestamp, body)) { @@ -146,7 +146,7 @@ namespace Discord.Rest using (var jsonReader = new JsonTextReader(textReader)) { var model = Serializer.Deserialize(jsonReader); - return await RestInteraction.CreateAsync(this, model, doApiCallOnCreation != null ? doApiCallOnCreation(model.Type) : _apiOnCreation); + return await RestInteraction.CreateAsync(this, model, doApiCallOnCreation is not null ? doApiCallOnCreation(new InteractionProperties(model)) : _apiOnCreation); } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs b/src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs new file mode 100644 index 000000000..03750d7d9 --- /dev/null +++ b/src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Discord.Rest +{ + /// + /// Represents a class that contains data present in all interactions to evaluate against at rest-interaction creation. + /// + public readonly struct InteractionProperties + { + /// + /// The type of this interaction. + /// + public InteractionType Type { get; } + + /// + /// Gets the type of application command this interaction represents. + /// + /// + /// This will be if the is not . + /// + public ApplicationCommandType? CommandType { get; } + + /// + /// Gets the name of the interaction. + /// + /// + /// This will be if the is not . + /// + public string Name { get; } = string.Empty; + + /// + /// Gets the custom ID of the interaction. + /// + /// + /// This will be if the is not or . + /// + public string CustomId { get; } = string.Empty; + + /// + /// Gets the guild ID of the interaction. + /// + /// + /// This will be if this interaction was not executed in a guild. + /// + public ulong? GuildId { get; } + + /// + /// Gets the channel ID of the interaction. + /// + /// + /// This will be if this interaction is . + /// + public ulong? ChannelId { get; } + + internal InteractionProperties(API.Interaction model) + { + Type = model.Type; + CommandType = null; + + if (model.GuildId.IsSpecified) + GuildId = model.GuildId.Value; + else + GuildId = null; + + if (model.ChannelId.IsSpecified) + ChannelId = model.ChannelId.Value; + else + ChannelId = null; + + switch (Type) + { + case InteractionType.ApplicationCommand: + { + var data = (API.ApplicationCommandInteractionData)model.Data; + + CommandType = data.Type; + Name = data.Name; + } + break; + case InteractionType.MessageComponent: + { + var data = (API.MessageComponentInteractionData)model.Data; + + CustomId = data.CustomId; + } + break; + case InteractionType.ModalSubmit: + { + var data = (API.ModalInteractionData)model.Data; + + CustomId = data.CustomId; + } + break; + } + } + } +}