Add helper classes to simplify embed creation for interactions

This commit is contained in:
Daan Boerlage 2021-11-06 16:17:22 +01:00
parent c15a66255f
commit ea17ce2866
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 41 additions and 10 deletions

View file

@ -1,23 +1,24 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace Geekbot.Core.Interactions.Embed namespace Geekbot.Core.Interactions.Embed
{ {
/// <see href="https://discord.com/developers/docs/resources/channel#embed-object" /> /// <see href="https://discord.com/developers/docs/resources/channel#embed-object" />
public record Embed public class Embed
{ {
/// <summary> /// <summary>
/// title of embed /// title of embed
/// </summary> /// </summary>
[JsonPropertyName("title")] [JsonPropertyName("title")]
public string Title { get; set; } public string Title { get; set; }
/// <summary> /// <summary>
/// type of embed (always "rich" for webhook embeds) /// type of embed (always "rich" for webhook embeds)
/// </summary> /// </summary>
[JsonPropertyName("type")] [JsonPropertyName("type")]
public EmbedTypes Type { get; set; } public EmbedTypes Type { get; set; } = EmbedTypes.Rich;
/// <summary> /// <summary>
/// description of embed /// description of embed
@ -41,7 +42,7 @@ namespace Geekbot.Core.Interactions.Embed
/// color code of the embed /// color code of the embed
/// </summary> /// </summary>
[JsonPropertyName("color")] [JsonPropertyName("color")]
public int Color { get; set; } public uint Color { get; set; }
/// <summary> /// <summary>
/// footer information /// footer information
@ -78,11 +79,31 @@ namespace Geekbot.Core.Interactions.Embed
/// </summary> /// </summary>
[JsonPropertyName("author")] [JsonPropertyName("author")]
public EmbedAuthor Author { get; set; } public EmbedAuthor Author { get; set; }
/// <summary> /// <summary>
/// fields information /// fields information
/// </summary> /// </summary>
[JsonPropertyName("fields")] [JsonPropertyName("fields")]
public List<EmbedField> Fields { get; set; } public List<EmbedField> Fields { get; set; } = new List<EmbedField>();
public void AddField(string name, string value, bool inline = false)
{
Fields.Add(new EmbedField()
{
Name = name,
Value = value,
Inline = inline
});
}
public void AddInlineField(string name, string value)
{
AddField(name, value, true);
}
public void SetColor(Color c)
{
Color = (uint)(c.R << 16 | c.G << 8 | c.B);
}
} }
} }

View file

@ -30,9 +30,7 @@ namespace Geekbot.Core.Interactions
return SimpleResponse(Localization.Internal.SomethingWentWrong); return SimpleResponse(Localization.Internal.SomethingWentWrong);
} }
protected InteractionResponse SimpleResponse(string message) protected InteractionResponse SimpleResponse(string message) => new InteractionResponse()
{
return new InteractionResponse()
{ {
Type = InteractionResponseType.ChannelMessageWithSource, Type = InteractionResponseType.ChannelMessageWithSource,
Data = new() Data = new()
@ -40,7 +38,19 @@ namespace Geekbot.Core.Interactions
Content = message Content = message
} }
}; };
}
protected InteractionResponse SimpleResponse(Embed.Embed embed) => new InteractionResponse()
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new ()
{
Content = string.Empty,
Embeds = new ()
{
embed
}
}
};
public abstract Command GetCommandInfo(); public abstract Command GetCommandInfo();
public abstract Task<InteractionResponse> Exec(Interaction interaction); public abstract Task<InteractionResponse> Exec(Interaction interaction);