- 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

@@ -2,6 +2,7 @@ using Discord;
using NadekoBot.Extensions;
using Newtonsoft.Json;
using System;
using NadekoBot.Services;
namespace NadekoBot.Common
{
@@ -29,7 +30,7 @@ namespace NadekoBot.Common
(Footer != null && (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl))) ||
(Fields != null && Fields.Length > 0);
public EmbedBuilder ToEmbed()
public IEmbedBuilder ToEmbed(IEmbedBuilderService eb)
{
var embed = new EmbedBuilder();
@@ -67,7 +68,7 @@ namespace NadekoBot.Common
embed.AddField(f.Name, f.Value, f.Inline);
}
return embed;
return eb.Create(embed);
}
public static bool TryParse(string input, out CREmbed embed)

View File

@@ -0,0 +1,26 @@
using Discord;
// todo test guild colors
namespace NadekoBot
{
public interface IEmbedBuilder
{
IEmbedBuilder WithDescription(string desc);
IEmbedBuilder WithTitle(string title);
IEmbedBuilder AddField(string title, object value, bool isInline = false);
IEmbedBuilder WithFooter(string text, string iconUrl = null);
IEmbedBuilder WithAuthor(string name, string iconUrl = null, string url = null);
IEmbedBuilder WithColor(EmbedColor color);
Embed Build();
IEmbedBuilder WithUrl(string url);
IEmbedBuilder WithImageUrl(string url);
IEmbedBuilder WithThumbnailUrl(string url);
}
public enum EmbedColor
{
Ok,
Pending,
Error,
}
}

View File

@@ -14,6 +14,7 @@ namespace NadekoBot.Modules
public IBotStrings Strings { get; set; }
public CommandHandler CmdHandler { get; set; }
public ILocalization Localization { get; set; }
public IEmbedBuilderService _eb { get; set; }
public string Prefix => CmdHandler.GetPrefix(ctx.Guild);
@@ -34,36 +35,51 @@ namespace NadekoBot.Modules
protected string GetText(string key, params object[] args) =>
Strings.GetText(key, _cultureInfo, args);
public Task<IUserMessage> SendErrorAsync(string error)
=> ctx.Channel.SendErrorAsync(_eb, error);
public Task<IUserMessage> SendErrorAsync(string title, string error, string url = null, string footer = null)
=> ctx.Channel.SendErrorAsync(_eb, title, error, url, footer);
public Task<IUserMessage> SendConfirmAsync(string text)
=> ctx.Channel.SendConfirmAsync(_eb, text);
public Task<IUserMessage> SendConfirmAsync(string title, string text, string url = null, string footer = null)
=> ctx.Channel.SendConfirmAsync(_eb, title, text, url, footer);
public Task<IUserMessage> SendPendingAsync(string text)
=> ctx.Channel.SendPendingAsync(_eb, text);
public Task<IUserMessage> ErrorLocalizedAsync(string textKey, params object[] args)
{
var text = GetText(textKey, args);
return ctx.Channel.SendErrorAsync(text);
return SendErrorAsync(text);
}
public Task<IUserMessage> ReplyErrorLocalizedAsync(string textKey, params object[] args)
{
var text = GetText(textKey, args);
return ctx.Channel.SendErrorAsync(Format.Bold(ctx.User.ToString()) + " " + text);
return SendErrorAsync(Format.Bold(ctx.User.ToString()) + " " + text);
}
public Task<IUserMessage> ReplyPendingLocalizedAsync(string textKey, params object[] args)
{
var text = GetText(textKey, args);
return ctx.Channel.SendPendingAsync(Format.Bold(ctx.User.ToString()) + " " + text);
return SendPendingAsync(Format.Bold(ctx.User.ToString()) + " " + text);
}
public Task<IUserMessage> ConfirmLocalizedAsync(string textKey, params object[] args)
{
var text = GetText(textKey, args);
return ctx.Channel.SendConfirmAsync(text);
return SendConfirmAsync(text);
}
public Task<IUserMessage> ReplyConfirmLocalizedAsync(string textKey, params object[] args)
{
var text = GetText(textKey, args);
return ctx.Channel.SendConfirmAsync(Format.Bold(ctx.User.ToString()) + " " + text);
return SendConfirmAsync(Format.Bold(ctx.User.ToString()) + " " + text);
}
public async Task<bool> PromptUserConfirmAsync(EmbedBuilder embed)
public async Task<bool> PromptUserConfirmAsync(IEmbedBuilder embed)
{
embed
.WithPendingColor()