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,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.Json.Serialization;
namespace Geekbot.Core.Interactions.Embed
{
/// <see href="https://discord.com/developers/docs/resources/channel#embed-object" />
public record Embed
public class Embed
{
/// <summary>
/// title of embed
@ -17,7 +18,7 @@ namespace Geekbot.Core.Interactions.Embed
/// type of embed (always "rich" for webhook embeds)
/// </summary>
[JsonPropertyName("type")]
public EmbedTypes Type { get; set; }
public EmbedTypes Type { get; set; } = EmbedTypes.Rich;
/// <summary>
/// description of embed
@ -41,7 +42,7 @@ namespace Geekbot.Core.Interactions.Embed
/// color code of the embed
/// </summary>
[JsonPropertyName("color")]
public int Color { get; set; }
public uint Color { get; set; }
/// <summary>
/// footer information
@ -83,6 +84,26 @@ namespace Geekbot.Core.Interactions.Embed
/// fields information
/// </summary>
[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);
}
protected InteractionResponse SimpleResponse(string message)
{
return new InteractionResponse()
protected InteractionResponse SimpleResponse(string message) => new InteractionResponse()
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new()
@ -40,7 +38,19 @@ namespace Geekbot.Core.Interactions
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 Task<InteractionResponse> Exec(Interaction interaction);