From 33efd8981dced689a5243a45012fe2fb527a3c24 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Wed, 9 Feb 2022 00:13:15 -0400 Subject: [PATCH] Add support for attachments (#2088) * Enforce valid button styles * support command option type 11 * missing '.' * Added type converter. Co-authored-by: Cat Co-authored-by: CottageDwellingCat <80918250+CottageDwellingCat@users.noreply.github.com> Co-authored-by: FeroxFoxxo Co-authored-by: Cat Co-authored-by: CottageDwellingCat <80918250+CottageDwellingCat@users.noreply.github.com> --- .../Interactions/ApplicationCommandOptionType.cs | 7 ++++++- src/Discord.Net.Interactions/InteractionService.cs | 1 + .../TypeConverters/DefaultEntityTypeConverter.cs | 5 +++++ .../ApplicationCommandInteractionDataResolved.cs | 2 ++ .../Interactions/CommandBase/RestResolvableData.cs | 13 +++++++++++++ .../SlashCommands/RestSlashCommandDataOption.cs | 4 ++++ .../SlashCommands/SocketSlashCommandDataOption.cs | 4 ++++ .../SocketBaseCommand/SocketResolvableData.cs | 13 +++++++++++++ 8 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs index 0f919f1f6..5bb00797b 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs @@ -53,6 +53,11 @@ namespace Discord /// /// A . /// - Number = 10 + Number = 10, + + /// + /// A . + /// + Attachment = 11 } } diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index b394109a5..475622f0b 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -163,6 +163,7 @@ namespace Discord.Interactions { [typeof(IChannel)] = typeof(DefaultChannelConverter<>), [typeof(IRole)] = typeof(DefaultRoleConverter<>), + [typeof(IAttachment)] = typeof(DefaultAttachmentConverter<>), [typeof(IUser)] = typeof(DefaultUserConverter<>), [typeof(IMentionable)] = typeof(DefaultMentionableConverter<>), [typeof(IConvertible)] = typeof(DefaultValueConverter<>), diff --git a/src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs b/src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs index 9107fbf35..fb493ed72 100644 --- a/src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs +++ b/src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs @@ -20,6 +20,11 @@ namespace Discord.Interactions } } + internal class DefaultAttachmentConverter : DefaultEntityTypeConverter where T : class, IAttachment + { + public override ApplicationCommandOptionType GetDiscordType() => ApplicationCommandOptionType.Attachment; + } + internal class DefaultRoleConverter : DefaultEntityTypeConverter where T : class, IRole { public override ApplicationCommandOptionType GetDiscordType ( ) => ApplicationCommandOptionType.Role; diff --git a/src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs b/src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs index 5b4b83e23..690be6cef 100644 --- a/src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs +++ b/src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs @@ -18,5 +18,7 @@ namespace Discord.API public Optional> Roles { get; set; } [JsonProperty("messages")] public Optional> Messages { get; set; } + [JsonProperty("attachments")] + public Optional> Attachments { get; set; } } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs index 710207ef9..9353a8530 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs @@ -19,6 +19,9 @@ namespace Discord.Rest internal readonly Dictionary Messages = new Dictionary(); + internal readonly Dictionary Attachments + = new Dictionary(); + internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model) { var resolved = model.Resolved.Value; @@ -91,6 +94,16 @@ namespace Discord.Rest Messages.Add(message.Id, message); } } + + if (resolved.Attachments.IsSpecified) + { + foreach (var attachment in resolved.Attachments.Value) + { + var discordAttachment = Attachment.Create(attachment.Value); + + Attachments.Add(ulong.Parse(attachment.Key), discordAttachment); + } + } } } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs index bb931f68e..cbb958968 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs @@ -43,6 +43,7 @@ namespace Discord.Rest case ApplicationCommandOptionType.Role: case ApplicationCommandOptionType.Channel: case ApplicationCommandOptionType.Mentionable: + case ApplicationCommandOptionType.Attachment: if (ulong.TryParse($"{model.Value.Value}", out var valueId)) { switch (Type) @@ -80,6 +81,9 @@ namespace Discord.Rest } } break; + case ApplicationCommandOptionType.Attachment: + Value = data.ResolvableData.Attachments.FirstOrDefault(x => x.Key == valueId).Value; + break; default: Value = model.Value.Value; break; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs index 265eda75b..2a44b4e03 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs @@ -39,6 +39,7 @@ namespace Discord.WebSocket case ApplicationCommandOptionType.Role: case ApplicationCommandOptionType.Channel: case ApplicationCommandOptionType.Mentionable: + case ApplicationCommandOptionType.Attachment: if (ulong.TryParse($"{model.Value.Value}", out var valueId)) { switch (Type) @@ -76,6 +77,9 @@ namespace Discord.WebSocket } } break; + case ApplicationCommandOptionType.Attachment: + Value = data.ResolvableData.Attachments.FirstOrDefault(x => x.Key == valueId).Value; + break; default: Value = model.Value.Value; break; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs index c065637ca..d722c5a13 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs @@ -16,6 +16,9 @@ namespace Discord.WebSocket internal readonly Dictionary Messages = new Dictionary(); + internal readonly Dictionary Attachments + = new Dictionary(); + internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T model) { var guild = guildId.HasValue ? discord.GetGuild(guildId.Value) : null; @@ -104,6 +107,16 @@ namespace Discord.WebSocket Messages.Add(message.Id, message); } } + + if (resolved.Attachments.IsSpecified) + { + foreach (var attachment in resolved.Attachments.Value) + { + var discordAttachment = Attachment.Create(attachment.Value); + + Attachments.Add(ulong.Parse(attachment.Key), discordAttachment); + } + } } } }