Support sending rich embeds, add an Embed Builder
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#pragma warning disable CS1591
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Discord.API.Rest
|
||||
@@ -13,6 +14,8 @@ namespace Discord.API.Rest
|
||||
public Optional<string> Nonce { get; set; }
|
||||
[JsonProperty("tts")]
|
||||
public Optional<bool> IsTTS { get; set; }
|
||||
[JsonProperty("embed")]
|
||||
public Optional<Embed> Embed { get; set; }
|
||||
|
||||
public CreateMessageParams(string content)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Discord
|
||||
string Type { get; }
|
||||
string Title { get; }
|
||||
string Description { get; }
|
||||
uint? Color { get; }
|
||||
Color? Color { get; }
|
||||
EmbedAuthor? Author { get; }
|
||||
EmbedFooter? Footer { get; }
|
||||
EmbedProvider? Provider { get; }
|
||||
|
||||
132
src/Discord.Net.Core/Utils/EmbedBuilder.cs
Normal file
132
src/Discord.Net.Core/Utils/EmbedBuilder.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Embed = Discord.API.Embed;
|
||||
using Field = Discord.API.EmbedField;
|
||||
using Author = Discord.API.EmbedAuthor;
|
||||
using Footer = Discord.API.EmbedFooter;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public class EmbedBuilder
|
||||
{
|
||||
private Embed embed = new Embed();
|
||||
List<Field> fields = new List<Field>();
|
||||
|
||||
public EmbedBuilder()
|
||||
{
|
||||
embed.Type = "rich";
|
||||
}
|
||||
|
||||
public EmbedBuilder Title(string title)
|
||||
{
|
||||
embed.Title = title;
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Description(string description)
|
||||
{
|
||||
embed.Description = description;
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Url(string url)
|
||||
{
|
||||
embed.Url = url;
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Color(Color color)
|
||||
{
|
||||
embed.Color = color.RawValue;
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Field(Func<EmbedFieldBuilder, EmbedFieldBuilder> builder)
|
||||
{
|
||||
fields.Add(builder(new EmbedFieldBuilder()).Build());
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Author(Func<EmbedAuthorBuilder, EmbedAuthorBuilder> builder)
|
||||
{
|
||||
embed.Author = builder(new EmbedAuthorBuilder()).Build();
|
||||
return this;
|
||||
}
|
||||
public EmbedBuilder Footer(Func<EmbedFooterBuilder, EmbedFooterBuilder> builder)
|
||||
{
|
||||
embed.Footer = builder(new EmbedFooterBuilder()).Build();
|
||||
return this;
|
||||
}
|
||||
public Embed Build()
|
||||
{
|
||||
embed.Fields = fields.ToArray();
|
||||
return embed;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class EmbedFieldBuilder
|
||||
{
|
||||
private Field embedField = new Field();
|
||||
|
||||
public EmbedFieldBuilder Name(string name)
|
||||
{
|
||||
embedField.Name = name;
|
||||
return this;
|
||||
}
|
||||
public EmbedFieldBuilder Value(string value)
|
||||
{
|
||||
embedField.Value = value;
|
||||
return this;
|
||||
}
|
||||
public EmbedFieldBuilder Inline(bool inline)
|
||||
{
|
||||
embedField.Inline = inline;
|
||||
return this;
|
||||
}
|
||||
public Field Build()
|
||||
{
|
||||
return embedField;
|
||||
}
|
||||
}
|
||||
|
||||
public class EmbedAuthorBuilder
|
||||
{
|
||||
private Author author = new Author();
|
||||
|
||||
public EmbedAuthorBuilder Name(string name)
|
||||
{
|
||||
author.Name = name;
|
||||
return this;
|
||||
}
|
||||
public EmbedAuthorBuilder Url(string url)
|
||||
{
|
||||
author.Url = url;
|
||||
return this;
|
||||
}
|
||||
public EmbedAuthorBuilder IconUrl(string iconUrl)
|
||||
{
|
||||
author.IconUrl = iconUrl;
|
||||
return this;
|
||||
}
|
||||
public Author Build()
|
||||
{
|
||||
return author;
|
||||
}
|
||||
}
|
||||
|
||||
public class EmbedFooterBuilder
|
||||
{
|
||||
private Footer footer = new Footer();
|
||||
|
||||
public EmbedFooterBuilder Text(string text)
|
||||
{
|
||||
footer.Text = text;
|
||||
return this;
|
||||
}
|
||||
public EmbedFooterBuilder IconUrl(string iconUrl)
|
||||
{
|
||||
footer.IconUrl = iconUrl;
|
||||
return this;
|
||||
}
|
||||
public Footer Build()
|
||||
{
|
||||
return footer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,6 +116,13 @@ namespace Discord.Rest
|
||||
var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);
|
||||
return RestUserMessage.Create(client, guild, model);
|
||||
}
|
||||
public static async Task<RestUserMessage> SendRichMessageAsync(IChannel channel, BaseDiscordClient client,
|
||||
string text, bool isTTS, API.Embed embed, IGuild guild, RequestOptions options)
|
||||
{
|
||||
var args = new CreateMessageParams(text) { IsTTS = isTTS, Embed = embed };
|
||||
var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);
|
||||
return RestUserMessage.Create(client, guild, model);
|
||||
}
|
||||
|
||||
public static async Task<RestUserMessage> SendFileAsync(IChannel channel, BaseDiscordClient client,
|
||||
string filePath, string text, bool isTTS, IGuild guild, RequestOptions options)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Model = Discord.API.Embed;
|
||||
@@ -12,7 +13,7 @@ namespace Discord
|
||||
public string Url { get; }
|
||||
public string Title { get; }
|
||||
public string Type { get; }
|
||||
public uint? Color { get; }
|
||||
public Color? Color { get; }
|
||||
public EmbedAuthor? Author { get; }
|
||||
public EmbedFooter? Footer { get; }
|
||||
public EmbedProvider? Provider { get; }
|
||||
@@ -23,7 +24,7 @@ namespace Discord
|
||||
string title,
|
||||
string description,
|
||||
string url,
|
||||
uint? color,
|
||||
Color? color,
|
||||
EmbedAuthor? author,
|
||||
EmbedFooter? footer,
|
||||
EmbedProvider? provider,
|
||||
@@ -34,12 +35,17 @@ namespace Discord
|
||||
Title = title;
|
||||
Description = description;
|
||||
Url = url;
|
||||
Color = color;
|
||||
Author = author;
|
||||
Footer = footer;
|
||||
Provider = provider;
|
||||
Thumbnail = thumbnail;
|
||||
Fields = fields;
|
||||
}
|
||||
internal static Embed Create(Model model)
|
||||
{
|
||||
return new Embed(model.Type, model.Title, model.Description, model.Url, model.Color,
|
||||
return new Embed(model.Type, model.Title, model.Description, model.Url,
|
||||
model.Color.HasValue ? new Color(model.Color.Value) : (Color?)null,
|
||||
model.Author.IsSpecified ? EmbedAuthor.Create(model.Author.Value) : (EmbedAuthor?)null,
|
||||
model.Footer.IsSpecified ? EmbedFooter.Create(model.Footer.Value) : (EmbedFooter?)null,
|
||||
model.Provider.IsSpecified ? EmbedProvider.Create(model.Provider.Value) : (EmbedProvider?)null,
|
||||
|
||||
Reference in New Issue
Block a user