Browse Source

Add support for attachments (#2088)

* Enforce valid button styles

* support command option type 11

* missing '.'

* Added type converter.

Co-authored-by: Cat <lumitydev@gmail.com>
Co-authored-by: CottageDwellingCat <80918250+CottageDwellingCat@users.noreply.github.com>

Co-authored-by: FeroxFoxxo <feroxfoxxo@gmail.com>
Co-authored-by: Cat <lumitydev@gmail.com>
Co-authored-by: CottageDwellingCat <80918250+CottageDwellingCat@users.noreply.github.com>
tags/3.3.0
Quin Lynch GitHub 3 years ago
parent
commit
33efd8981d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 1 deletions
  1. +6
    -1
      src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs
  2. +1
    -0
      src/Discord.Net.Interactions/InteractionService.cs
  3. +5
    -0
      src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs
  4. +2
    -0
      src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs
  5. +13
    -0
      src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs
  6. +4
    -0
      src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs
  7. +4
    -0
      src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs
  8. +13
    -0
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs

+ 6
- 1
src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOptionType.cs View File

@@ -53,6 +53,11 @@ namespace Discord
/// <summary> /// <summary>
/// A <see cref="double"/>. /// A <see cref="double"/>.
/// </summary> /// </summary>
Number = 10
Number = 10,

/// <summary>
/// A <see cref="Discord.Attachment"/>.
/// </summary>
Attachment = 11
} }
} }

+ 1
- 0
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -163,6 +163,7 @@ namespace Discord.Interactions
{ {
[typeof(IChannel)] = typeof(DefaultChannelConverter<>), [typeof(IChannel)] = typeof(DefaultChannelConverter<>),
[typeof(IRole)] = typeof(DefaultRoleConverter<>), [typeof(IRole)] = typeof(DefaultRoleConverter<>),
[typeof(IAttachment)] = typeof(DefaultAttachmentConverter<>),
[typeof(IUser)] = typeof(DefaultUserConverter<>), [typeof(IUser)] = typeof(DefaultUserConverter<>),
[typeof(IMentionable)] = typeof(DefaultMentionableConverter<>), [typeof(IMentionable)] = typeof(DefaultMentionableConverter<>),
[typeof(IConvertible)] = typeof(DefaultValueConverter<>), [typeof(IConvertible)] = typeof(DefaultValueConverter<>),


+ 5
- 0
src/Discord.Net.Interactions/TypeConverters/DefaultEntityTypeConverter.cs View File

@@ -20,6 +20,11 @@ namespace Discord.Interactions
} }
} }


internal class DefaultAttachmentConverter<T> : DefaultEntityTypeConverter<T> where T : class, IAttachment
{
public override ApplicationCommandOptionType GetDiscordType() => ApplicationCommandOptionType.Attachment;
}

internal class DefaultRoleConverter<T> : DefaultEntityTypeConverter<T> where T : class, IRole internal class DefaultRoleConverter<T> : DefaultEntityTypeConverter<T> where T : class, IRole
{ {
public override ApplicationCommandOptionType GetDiscordType ( ) => ApplicationCommandOptionType.Role; public override ApplicationCommandOptionType GetDiscordType ( ) => ApplicationCommandOptionType.Role;


+ 2
- 0
src/Discord.Net.Rest/API/Common/ApplicationCommandInteractionDataResolved.cs View File

@@ -18,5 +18,7 @@ namespace Discord.API
public Optional<Dictionary<string, Role>> Roles { get; set; } public Optional<Dictionary<string, Role>> Roles { get; set; }
[JsonProperty("messages")] [JsonProperty("messages")]
public Optional<Dictionary<string, Message>> Messages { get; set; } public Optional<Dictionary<string, Message>> Messages { get; set; }
[JsonProperty("attachments")]
public Optional<Dictionary<string, Attachment>> Attachments { get; set; }
} }
} }

+ 13
- 0
src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestResolvableData.cs View File

@@ -19,6 +19,9 @@ namespace Discord.Rest
internal readonly Dictionary<ulong, RestMessage> Messages internal readonly Dictionary<ulong, RestMessage> Messages
= new Dictionary<ulong, RestMessage>(); = new Dictionary<ulong, RestMessage>();


internal readonly Dictionary<ulong, Attachment> Attachments
= new Dictionary<ulong, Attachment>();

internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model) internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model)
{ {
var resolved = model.Resolved.Value; var resolved = model.Resolved.Value;
@@ -91,6 +94,16 @@ namespace Discord.Rest
Messages.Add(message.Id, message); 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);
}
}
} }
} }
} }

+ 4
- 0
src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandDataOption.cs View File

@@ -43,6 +43,7 @@ namespace Discord.Rest
case ApplicationCommandOptionType.Role: case ApplicationCommandOptionType.Role:
case ApplicationCommandOptionType.Channel: case ApplicationCommandOptionType.Channel:
case ApplicationCommandOptionType.Mentionable: case ApplicationCommandOptionType.Mentionable:
case ApplicationCommandOptionType.Attachment:
if (ulong.TryParse($"{model.Value.Value}", out var valueId)) if (ulong.TryParse($"{model.Value.Value}", out var valueId))
{ {
switch (Type) switch (Type)
@@ -80,6 +81,9 @@ namespace Discord.Rest
} }
} }
break; break;
case ApplicationCommandOptionType.Attachment:
Value = data.ResolvableData.Attachments.FirstOrDefault(x => x.Key == valueId).Value;
break;
default: default:
Value = model.Value.Value; Value = model.Value.Value;
break; break;


+ 4
- 0
src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommandDataOption.cs View File

@@ -39,6 +39,7 @@ namespace Discord.WebSocket
case ApplicationCommandOptionType.Role: case ApplicationCommandOptionType.Role:
case ApplicationCommandOptionType.Channel: case ApplicationCommandOptionType.Channel:
case ApplicationCommandOptionType.Mentionable: case ApplicationCommandOptionType.Mentionable:
case ApplicationCommandOptionType.Attachment:
if (ulong.TryParse($"{model.Value.Value}", out var valueId)) if (ulong.TryParse($"{model.Value.Value}", out var valueId))
{ {
switch (Type) switch (Type)
@@ -76,6 +77,9 @@ namespace Discord.WebSocket
} }
} }
break; break;
case ApplicationCommandOptionType.Attachment:
Value = data.ResolvableData.Attachments.FirstOrDefault(x => x.Key == valueId).Value;
break;
default: default:
Value = model.Value.Value; Value = model.Value.Value;
break; break;


+ 13
- 0
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs View File

@@ -16,6 +16,9 @@ namespace Discord.WebSocket
internal readonly Dictionary<ulong, SocketMessage> Messages internal readonly Dictionary<ulong, SocketMessage> Messages
= new Dictionary<ulong, SocketMessage>(); = new Dictionary<ulong, SocketMessage>();


internal readonly Dictionary<ulong, Attachment> Attachments
= new Dictionary<ulong, Attachment>();

internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T model) internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T model)
{ {
var guild = guildId.HasValue ? discord.GetGuild(guildId.Value) : null; var guild = guildId.HasValue ? discord.GetGuild(guildId.Value) : null;
@@ -104,6 +107,16 @@ namespace Discord.WebSocket
Messages.Add(message.Id, message); 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);
}
}
} }
} }
} }

Loading…
Cancel
Save