- Reworked embed builder

- Use IEmbedBuilderService to create embed builders
- Wrapped embed builder and using IEmbedBuilder
This commit is contained in:
Kwoth
2021-07-09 22:23:19 +02:00
parent 5b4daa9dd3
commit 5e4754fa40
103 changed files with 730 additions and 540 deletions

View File

@@ -24,6 +24,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using Color = Discord.Color;
namespace NadekoBot.Extensions
{
@@ -34,13 +35,15 @@ namespace NadekoBot.Extensions
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
=> Array.ConvertAll(arr, x => f(x));
public static Task<IUserMessage> EmbedAsync(this IMessageChannel channel, CREmbed crEmbed, bool sanitizeAll = false)
public static Task<IUserMessage> EmbedAsync(this IMessageChannel channel, CREmbed crEmbed, IEmbedBuilderService eb, bool sanitizeAll = false)
{
var plainText = sanitizeAll
? crEmbed.PlainText?.SanitizeAllMentions() ?? ""
: crEmbed.PlainText?.SanitizeMentions() ?? "";
return channel.SendMessageAsync(plainText, embed: crEmbed.IsEmbedValid ? crEmbed.ToEmbed().Build() : null);
return channel.SendMessageAsync(plainText, embed: crEmbed.IsEmbedValid
? crEmbed.ToEmbed(eb).Build()
: null);
}
public static List<ulong> GetGuildIds(this DiscordSocketClient client)
@@ -167,7 +170,7 @@ namespace NadekoBot.Extensions
public static string GetFullUsage(string commandName, string args, string prefix)
=> $"{prefix}{commandName} {string.Format(args, prefix)}";
public static EmbedBuilder AddPaginatedFooter(this EmbedBuilder embed, int curPage, int? lastPage)
public static IEmbedBuilder AddPaginatedFooter(this IEmbedBuilder embed, int curPage, int? lastPage)
{
if (lastPage != null)
return embed.WithFooter($"{curPage + 1} / {lastPage + 1}");
@@ -175,14 +178,17 @@ namespace NadekoBot.Extensions
return embed.WithFooter(curPage.ToString());
}
public static EmbedBuilder WithOkColor(this EmbedBuilder eb) =>
eb.WithColor(Bot.OkColor);
public static EmbedBuilder WithPendingColor(this EmbedBuilder eb) =>
eb.WithColor(Bot.PendingColor);
public static Color ToDiscordColor(this Rgba32 color)
=> new Color(color.R, color.G, color.B);
public static EmbedBuilder WithErrorColor(this EmbedBuilder eb) =>
eb.WithColor(Bot.ErrorColor);
public static IEmbedBuilder WithOkColor(this IEmbedBuilder eb) =>
eb.WithColor(EmbedColor.Ok);
public static IEmbedBuilder WithPendingColor(this IEmbedBuilder eb) =>
eb.WithColor(EmbedColor.Pending);
public static IEmbedBuilder WithErrorColor(this IEmbedBuilder eb) =>
eb.WithColor(EmbedColor.Error);
public static ReactionEventWrapper OnReaction(this IUserMessage msg, DiscordSocketClient client, Func<SocketReaction, Task> reactionAdded, Func<SocketReaction, Task> reactionRemoved = null)
{

View File

@@ -5,45 +5,67 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Services;
namespace NadekoBot.Extensions
{
public static class IMessageChannelExtensions
{
public static Task<IUserMessage> EmbedAsync(this IMessageChannel ch, EmbedBuilder embed, string msg = "")
public static Task<IUserMessage> EmbedAsync(this IMessageChannel ch, IEmbedBuilder embed, string msg = "")
=> ch.SendMessageAsync(msg, embed: embed.Build(),
options: new RequestOptions() { RetryMode = RetryMode.AlwaysRetry });
public static Task<IUserMessage> SendErrorAsync(this IMessageChannel ch, string title, string error, string url = null, string footer = null)
// this is a huge problem, because now i don't have
// access to embed builder service
// as this is an extension of the message channel
public static Task<IUserMessage> SendErrorAsync(this IMessageChannel ch, IEmbedBuilderService eb, string title, string error, string url = null, string footer = null)
{
var eb = new EmbedBuilder().WithErrorColor().WithDescription(error)
var embed = eb.Create()
.WithErrorColor()
.WithDescription(error)
.WithTitle(title);
if (url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute))
eb.WithUrl(url);
embed.WithUrl(url);
if (!string.IsNullOrWhiteSpace(footer))
eb.WithFooter(footer);
return ch.SendMessageAsync("", embed: eb.Build());
embed.WithFooter(footer);
return ch.SendMessageAsync("", embed: embed.Build());
}
public static Task<IUserMessage> SendErrorAsync(this IMessageChannel ch, string error)
=> ch.SendMessageAsync("", embed: new EmbedBuilder().WithErrorColor().WithDescription(error).Build());
public static Task<IUserMessage> SendErrorAsync(this IMessageChannel ch, IEmbedBuilderService eb, string error)
=> ch.SendMessageAsync("",
embed: eb.Create()
.WithErrorColor()
.WithDescription(error)
.Build());
public static Task<IUserMessage> SendPendingAsync(this IMessageChannel ch, string message)
=> ch.SendMessageAsync("", embed: new EmbedBuilder().WithPendingColor().WithDescription(message).Build());
public static Task<IUserMessage> SendPendingAsync(this IMessageChannel ch, IEmbedBuilderService eb, string message)
=> ch.SendMessageAsync("", embed: eb.Create()
.WithPendingColor()
.WithDescription(message)
.Build());
public static Task<IUserMessage> SendConfirmAsync(this IMessageChannel ch, string title, string text, string url = null, string footer = null)
public static Task<IUserMessage> SendConfirmAsync(this IMessageChannel ch, IEmbedBuilderService eb, string title, string text, string url = null, string footer = null)
{
var eb = new EmbedBuilder().WithOkColor().WithDescription(text)
var embed = eb.Create().WithOkColor().WithDescription(text)
.WithTitle(title);
if (url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute))
eb.WithUrl(url);
embed.WithUrl(url);
if (!string.IsNullOrWhiteSpace(footer))
eb.WithFooter(footer);
return ch.SendMessageAsync("", embed: eb.Build());
embed.WithFooter(footer);
return ch.SendMessageAsync("", embed: embed.Build());
}
public static Task<IUserMessage> SendConfirmAsync(this IMessageChannel ch, string text)
=> ch.SendMessageAsync("", embed: new EmbedBuilder().WithOkColor().WithDescription(text).Build());
public static Task<IUserMessage> SendConfirmAsync(this IMessageChannel ch, IEmbedBuilderService eb, string text)
=> ch.SendMessageAsync("", embed: eb.Create()
.WithOkColor()
.WithDescription(text)
.Build());
public static Task<IUserMessage> SendTableAsync<T>(this IMessageChannel ch, string seed, IEnumerable<T> items, Func<T, string> howToPrint, int columns = 3)
{
@@ -60,7 +82,7 @@ namespace NadekoBot.Extensions
private static readonly IEmote arrow_right = new Emoji("➡");
public static Task SendPaginatedConfirmAsync(this ICommandContext ctx,
int currentPage, Func<int, EmbedBuilder> pageFunc, int totalElements,
int currentPage, Func<int, IEmbedBuilder> pageFunc, int totalElements,
int itemsPerPage, bool addPaginatedFooter = true)
=> ctx.SendPaginatedConfirmAsync(currentPage,
(x) => Task.FromResult(pageFunc(x)), totalElements, itemsPerPage, addPaginatedFooter);
@@ -68,7 +90,7 @@ namespace NadekoBot.Extensions
/// danny kamisama
/// </summary>
public static async Task SendPaginatedConfirmAsync(this ICommandContext ctx, int currentPage,
Func<int, Task<EmbedBuilder>> pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true)
Func<int, Task<IEmbedBuilder>> pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true)
{
var embed = await pageFunc(currentPage).ConfigureAwait(false);

View File

@@ -4,33 +4,42 @@ using System;
using System.IO;
using System.Threading.Tasks;
using NadekoBot.Db.Models;
using NadekoBot.Services;
namespace NadekoBot.Extensions
{
public static class IUserExtensions
{
public static async Task<IUserMessage> SendConfirmAsync(this IUser user, string text)
=> await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync("", embed: new EmbedBuilder().WithOkColor().WithDescription(text).Build()).ConfigureAwait(false);
public static async Task<IUserMessage> SendConfirmAsync(this IUser user, IEmbedBuilderService eb, string text)
=> await (await user.GetOrCreateDMChannelAsync()).SendMessageAsync("", embed: eb.Create()
.WithOkColor()
.WithDescription(text)
.Build());
public static async Task<IUserMessage> SendConfirmAsync(this IUser user, string title, string text, string url = null)
public static async Task<IUserMessage> SendConfirmAsync(this IUser user, IEmbedBuilderService eb, string title, string text, string url = null)
{
var eb = new EmbedBuilder().WithOkColor().WithDescription(text).WithTitle(title);
var embed = eb.Create().WithOkColor().WithDescription(text).WithTitle(title);
if (url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute))
eb.WithUrl(url);
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync("", embed: eb.Build()).ConfigureAwait(false);
embed.WithUrl(url);
return await (await user.GetOrCreateDMChannelAsync()).SendMessageAsync("", embed: embed.Build());
}
public static async Task<IUserMessage> SendErrorAsync(this IUser user, string title, string error, string url = null)
public static async Task<IUserMessage> SendErrorAsync(this IUser user, IEmbedBuilderService eb, string title, string error, string url = null)
{
var eb = new EmbedBuilder().WithErrorColor().WithDescription(error).WithTitle(title);
var embed = eb.Create().WithErrorColor().WithDescription(error).WithTitle(title);
if (url != null && Uri.IsWellFormedUriString(url, UriKind.Absolute))
eb.WithUrl(url);
embed.WithUrl(url);
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync("", embed: eb.Build()).ConfigureAwait(false);
return await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync("", embed: embed.Build()).ConfigureAwait(false);
}
public static async Task<IUserMessage> SendErrorAsync(this IUser user, string error)
=> await (await user.GetOrCreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync("", embed: new EmbedBuilder().WithErrorColor().WithDescription(error).Build()).ConfigureAwait(false);
public static async Task<IUserMessage> SendErrorAsync(this IUser user, IEmbedBuilderService eb, string error)
=> await (await user.GetOrCreateDMChannelAsync())
.SendMessageAsync("", embed: eb.Create()
.WithErrorColor()
.WithDescription(error)
.Build());
public static async Task<IUserMessage> SendFileAsync(this IUser user, string filePath, string caption = null, string text = null, bool isTTS = false)
{