Browse Source

feature: better call control in ParseHttpInteraction (#2330)

* Init

* Fix channelid xmldoc
tags/3.7.1
Armano den Boef GitHub 3 years ago
parent
commit
a890de9304
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 3 deletions
  1. +3
    -3
      src/Discord.Net.Rest/DiscordRestClient.cs
  2. +101
    -0
      src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs

+ 3
- 3
src/Discord.Net.Rest/DiscordRestClient.cs View File

@@ -121,7 +121,7 @@ namespace Discord.Rest
/// A <see cref="RestInteraction"/> that represents the incoming http interaction.
/// </returns>
/// <exception cref="BadSignatureException">Thrown when the signature doesn't match the public key.</exception>
public Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body, Func<InteractionType, bool> doApiCallOnCreation = null)
public Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body, Func<InteractionProperties, bool> doApiCallOnCreation = null)
=> ParseHttpInteractionAsync(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body), doApiCallOnCreation);

/// <summary>
@@ -135,7 +135,7 @@ namespace Discord.Rest
/// A <see cref="RestInteraction"/> that represents the incoming http interaction.
/// </returns>
/// <exception cref="BadSignatureException">Thrown when the signature doesn't match the public key.</exception>
public async Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body, Func<InteractionType, bool> doApiCallOnCreation = null)
public async Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body, Func<InteractionProperties, bool> 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<API.Interaction>(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);
}
}



+ 101
- 0
src/Discord.Net.Rest/Entities/Interactions/InteractionProperties.cs View File

@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.Rest
{
/// <summary>
/// Represents a class that contains data present in all interactions to evaluate against at rest-interaction creation.
/// </summary>
public readonly struct InteractionProperties
{
/// <summary>
/// The type of this interaction.
/// </summary>
public InteractionType Type { get; }

/// <summary>
/// Gets the type of application command this interaction represents.
/// </summary>
/// <remarks>
/// This will be <see langword="null"/> if the <see cref="Type"/> is not <see cref="InteractionType.ApplicationCommand"/>.
/// </remarks>
public ApplicationCommandType? CommandType { get; }

/// <summary>
/// Gets the name of the interaction.
/// </summary>
/// <remarks>
/// This will be <see cref="string.Empty"/> if the <see cref="Type"/> is not <see cref="InteractionType.ApplicationCommand"/>.
/// </remarks>
public string Name { get; } = string.Empty;

/// <summary>
/// Gets the custom ID of the interaction.
/// </summary>
/// <remarks>
/// This will be <see cref="string.Empty"/> if the <see cref="Type"/> is not <see cref="InteractionType.MessageComponent"/> or <see cref="InteractionType.ModalSubmit"/>.
/// </remarks>
public string CustomId { get; } = string.Empty;

/// <summary>
/// Gets the guild ID of the interaction.
/// </summary>
/// <remarks>
/// This will be <see langword="null"/> if this interaction was not executed in a guild.
/// </remarks>
public ulong? GuildId { get; }

/// <summary>
/// Gets the channel ID of the interaction.
/// </summary>
/// <remarks>
/// This will be <see langword="null"/> if this interaction is <see cref="InteractionType.Ping"/>.
/// </remarks>
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;
}
}
}
}

Loading…
Cancel
Save