- 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,12 +24,14 @@ namespace NadekoBot.Modules.Administration.Services
private readonly DbService _db;
private readonly ILogCommandService _logService;
private readonly IEmbedBuilderService _eb;
public AdministrationService(Bot bot, CommandHandler cmdHandler, DbService db,
ILogCommandService logService)
ILogCommandService logService, IEmbedBuilderService eb)
{
_db = db;
_logService = logService;
_eb = eb;
DeleteMessagesOnCommand = new ConcurrentHashSet<ulong>(bot.AllGuildConfigs
.Where(g => g.DeleteMessageOnCommand)
@@ -170,7 +172,7 @@ namespace NadekoBot.Modules.Administration.Services
rep.Replace(crembed);
await umsg.ModifyAsync(x =>
{
x.Embed = crembed.ToEmbed().Build();
x.Embed = crembed.ToEmbed(_eb).Build();
x.Content = crembed.PlainText?.SanitizeMentions() ?? "";
}).ConfigureAwait(false);
}

View File

@@ -68,13 +68,19 @@ namespace NadekoBot.Modules.Administration.Services
private readonly MuteService _mute;
private readonly ProtectionService _prot;
private readonly GuildTimezoneService _tz;
private readonly IEmbedBuilderService _eb;
private readonly IMemoryCache _memoryCache;
private readonly Timer _clearTimer;
private readonly ConcurrentHashSet<ulong> _ignoreMessageIds = new ConcurrentHashSet<ulong>();
public LogCommandService(DiscordSocketClient client, IBotStrings strings,
DbService db, MuteService mute, ProtectionService prot, GuildTimezoneService tz,
IMemoryCache memoryCache)
IMemoryCache memoryCache, IEmbedBuilderService eb)
{
_client = client;
_memoryCache = memoryCache;
_memoryCache = memoryCache;
_eb = eb;
_strings = strings;
_db = db;
_mute = mute;
@@ -111,7 +117,7 @@ namespace NadekoBot.Modules.Administration.Services
{
var title = GetText(key.Guild, "presence_updates");
var desc = string.Join(Environment.NewLine, msgs);
return key.SendConfirmAsync(title, desc.TrimTo(2048));
return key.SendConfirmAsync(_eb, title, desc.TrimTo(2048));
}
return Task.CompletedTask;
@@ -148,10 +154,6 @@ namespace NadekoBot.Modules.Administration.Services
#endif
}
private readonly Timer _clearTimer;
private readonly ConcurrentHashSet<ulong> _ignoreMessageIds = new ConcurrentHashSet<ulong>();
private readonly IMemoryCache _memoryCache;
public LogSetting GetGuildLogSettings(ulong guildId)
{
GuildLogSettings.TryGetValue(guildId, out LogSetting logSetting);
@@ -254,7 +256,7 @@ namespace NadekoBot.Modules.Administration.Services
await TryGetLogChannel(g, logSetting, LogType.UserUpdated).ConfigureAwait(false)) is null)
return;
var embed = new EmbedBuilder();
var embed = _eb.Create();
if (before.Username != after.Username)
{
@@ -441,7 +443,7 @@ namespace NadekoBot.Modules.Administration.Services
break;
}
var embed = new EmbedBuilder().WithAuthor(mutes)
var embed = _eb.Create().WithAuthor(mutes)
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter(CurrentTime(usr.Guild))
.WithOkColor();
@@ -486,7 +488,7 @@ namespace NadekoBot.Modules.Administration.Services
break;
}
var embed = new EmbedBuilder().WithAuthor(mutes)
var embed = _eb.Create().WithAuthor(mutes)
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter($"{CurrentTime(usr.Guild)}")
.WithOkColor();
@@ -541,7 +543,7 @@ namespace NadekoBot.Modules.Administration.Services
break;
}
var embed = new EmbedBuilder().WithAuthor($"🛡 Anti-{protection}")
var embed = _eb.Create().WithAuthor($"🛡 Anti-{protection}")
.WithTitle(GetText(logChannel.Guild, "users") + " " + punishment)
.WithDescription(string.Join("\n", users.Select(u => u.ToString())))
.WithFooter(CurrentTime(logChannel.Guild))
@@ -589,7 +591,7 @@ namespace NadekoBot.Modules.Administration.Services
(logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)
.ConfigureAwait(false)) != null)
{
var embed = new EmbedBuilder().WithOkColor()
var embed = _eb.Create().WithOkColor()
.WithFooter(CurrentTime(before.Guild))
.WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}");
if (before.Nickname != after.Nickname)
@@ -691,7 +693,7 @@ namespace NadekoBot.Modules.Administration.Services
.ConfigureAwait(false)) is null)
return;
var embed = new EmbedBuilder().WithOkColor()
var embed = _eb.Create().WithOkColor()
.WithFooter(CurrentTime(before.Guild));
var beforeTextChannel = cbefore as ITextChannel;
@@ -749,7 +751,7 @@ namespace NadekoBot.Modules.Administration.Services
else
title = GetText(logChannel.Guild, "text_chan_destroyed");
await logChannel.EmbedAsync(new EmbedBuilder()
await logChannel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
@@ -788,7 +790,7 @@ namespace NadekoBot.Modules.Administration.Services
else
title = GetText(logChannel.Guild, "text_chan_created");
await logChannel.EmbedAsync(new EmbedBuilder()
await logChannel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
@@ -920,7 +922,7 @@ namespace NadekoBot.Modules.Administration.Services
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserLeft)
.ConfigureAwait(false)) is null)
return;
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("❌ " + GetText(logChannel.Guild, "user_left"))
.WithDescription(usr.ToString())
@@ -955,7 +957,7 @@ namespace NadekoBot.Modules.Administration.Services
.ConfigureAwait(false)) is null)
return;
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("✅ " + GetText(logChannel.Guild, "user_joined"))
.WithDescription($"{usr.Mention} `{usr}`")
@@ -995,7 +997,7 @@ namespace NadekoBot.Modules.Administration.Services
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserUnbanned)
.ConfigureAwait(false)) is null)
return;
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("♻️ " + GetText(logChannel.Guild, "user_unbanned"))
.WithDescription(usr.ToString())
@@ -1030,7 +1032,7 @@ namespace NadekoBot.Modules.Administration.Services
await TryGetLogChannel(guild, logSetting, LogType.UserBanned).ConfigureAwait(false)) ==
null)
return;
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("🚫 " + GetText(logChannel.Guild, "user_banned"))
.WithDescription(usr.ToString())
@@ -1079,7 +1081,7 @@ namespace NadekoBot.Modules.Administration.Services
return;
var resolvedMessage = msg.Resolve(userHandling: TagHandling.FullName);
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("🗑 " + GetText(logChannel.Guild, "msg_del", ((ITextChannel) msg.Channel).Name))
.WithDescription(msg.Author.ToString())
@@ -1136,7 +1138,7 @@ namespace NadekoBot.Modules.Administration.Services
.ConfigureAwait(false)) is null || logChannel.Id == after.Channel.Id)
return;
var embed = new EmbedBuilder()
var embed = _eb.Create()
.WithOkColor()
.WithTitle("📝 " + GetText(logChannel.Guild, "msg_update", ((ITextChannel)after.Channel).Name))
.WithDescription(after.Author.ToString())

View File

@@ -39,12 +39,14 @@ namespace NadekoBot.Modules.Administration.Services
private readonly DiscordSocketClient _client;
private readonly DbService _db;
private readonly IEmbedBuilderService _eb;
public MuteService(DiscordSocketClient client, DbService db)
public MuteService(DiscordSocketClient client, DbService db, IEmbedBuilderService eb)
{
_client = client;
_db = db;
_eb = eb;
using (var uow = db.GetDbContext())
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
@@ -132,7 +134,7 @@ namespace NadekoBot.Modules.Administration.Services
if (string.IsNullOrWhiteSpace(reason))
return;
var _ = Task.Run(() => user.SendMessageAsync(embed: new EmbedBuilder()
var _ = Task.Run(() => user.SendMessageAsync(embed: _eb.Create()
.WithDescription($"You've been muted in {user.Guild} server")
.AddField("Mute Type", type.ToString())
.AddField("Moderator", mod.ToString())
@@ -145,7 +147,7 @@ namespace NadekoBot.Modules.Administration.Services
if (string.IsNullOrWhiteSpace(reason))
return;
var _ = Task.Run(() => user.SendMessageAsync(embed: new EmbedBuilder()
var _ = Task.Run(() => user.SendMessageAsync(embed: _eb.Create()
.WithDescription($"You've been unmuted in {user.Guild} server")
.AddField("Unmute Type", type.ToString())
.AddField("Moderator", mod.ToString())

View File

@@ -37,6 +37,7 @@ namespace NadekoBot.Modules.Administration.Services
private readonly IHttpClientFactory _httpFactory;
private readonly BotConfigService _bss;
private readonly IPubSub _pubSub;
private readonly IEmbedBuilderService _eb;
//keys
private readonly TypedKey<ActivityPubData> _activitySetKey;
@@ -52,7 +53,8 @@ namespace NadekoBot.Modules.Administration.Services
IDataCache cache,
IHttpClientFactory factory,
BotConfigService bss,
IPubSub pubSub)
IPubSub pubSub,
IEmbedBuilderService eb)
{
_cmdHandler = cmdHandler;
_db = db;
@@ -63,6 +65,7 @@ namespace NadekoBot.Modules.Administration.Services
_httpFactory = factory;
_bss = bss;
_pubSub = pubSub;
_eb = eb;
_activitySetKey = new("activity.set");
_imagesReloadKey = new("images.reload");
_guildLeaveKey = new("guild.leave");
@@ -256,7 +259,7 @@ namespace NadekoBot.Modules.Administration.Services
{
try
{
await ownerCh.SendConfirmAsync(title, toSend).ConfigureAwait(false);
await ownerCh.SendConfirmAsync(_eb, title, toSend).ConfigureAwait(false);
}
catch
{
@@ -271,7 +274,7 @@ namespace NadekoBot.Modules.Administration.Services
{
try
{
await firstOwnerChannel.SendConfirmAsync(title, toSend).ConfigureAwait(false);
await firstOwnerChannel.SendConfirmAsync(_eb, title, toSend).ConfigureAwait(false);
}
catch
{