From a707792fe4cc941bedea679dfe4f133f2b16db7f Mon Sep 17 00:00:00 2001 From: Noi Date: Sat, 3 Dec 2022 17:18:48 -0800 Subject: [PATCH] Add color codes to logging embeds --- Common/Utilities.cs | 30 ------------------------- Modules/ModLogs/ModLogs_Logging.cs | 35 ++++++++++++++++++++++++++++- Modules/ModLogs/ModLogs_Messages.cs | 8 ++++--- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/Common/Utilities.cs b/Common/Utilities.cs index ee2a4fc..4ffe0b9 100644 --- a/Common/Utilities.cs +++ b/Common/Utilities.cs @@ -66,36 +66,6 @@ public static class Utilities { return results; } - /// - /// Builds and returns an embed which displays this log entry. - /// - public static Embed BuildEmbed(this Data.ModLogEntry entry, RegexbotClient bot) { - var issuedDisplay = TryFromEntityNameString(entry.IssuedBy, bot); - string targetDisplay; - var targetq = bot.EcQueryUser(entry.UserId.ToString()); - if (targetq != null) targetDisplay = $"<@{targetq.UserId}> - {targetq.Username}#{targetq.Discriminator} `{targetq.UserId}`"; - else targetDisplay = $"User with ID `{entry.UserId}`"; - - var logEmbed = new EmbedBuilder() - .WithTitle(Enum.GetName(typeof(ModLogType), entry.LogType) + " logged:") - .WithTimestamp(entry.Timestamp) - .WithFooter($"Log #{entry.LogId}", bot.DiscordClient.CurrentUser.GetAvatarUrl()); // Escaping '#' not necessary here - if (entry.Message != null) { - logEmbed.Description = entry.Message; - } - - var contextStr = new StringBuilder(); - contextStr.AppendLine($"User: {targetDisplay}"); - contextStr.AppendLine($"Logged by: {issuedDisplay}"); - - logEmbed.AddField(new EmbedFieldBuilder() { - Name = "Context", - Value = contextStr.ToString() - }); - - return logEmbed.Build(); - } - /// /// Returns a representation of this entity that can be parsed by the constructor. /// diff --git a/Modules/ModLogs/ModLogs_Logging.cs b/Modules/ModLogs/ModLogs_Logging.cs index 147078a..cd9a2dd 100644 --- a/Modules/ModLogs/ModLogs_Logging.cs +++ b/Modules/ModLogs/ModLogs_Logging.cs @@ -1,5 +1,7 @@ +using Discord; using RegexBot.Common; using RegexBot.Data; +using System.Text; namespace RegexBot.Modules.ModLogs; // Contains all logic relating to reporting new database mod log entries @@ -12,6 +14,37 @@ internal partial class ModLogs { var reportChannel = conf?.ReportingChannel?.FindChannelIn(guild, true); if (reportChannel == null) return; - await reportChannel.SendMessageAsync(embed: entry.BuildEmbed(Bot)); + await reportChannel.SendMessageAsync(embed: BuildLogEmbed(entry)); + } + + /// + /// Builds and returns an embed which displays this log entry. + /// + private Embed BuildLogEmbed(ModLogEntry entry) { + var issuedDisplay = Utilities.TryFromEntityNameString(entry.IssuedBy, bot); + string targetDisplay; + var targetq = Bot.EcQueryUser(entry.UserId.ToString()); + if (targetq != null) targetDisplay = $"<@{targetq.UserId}> - {targetq.Username}#{targetq.Discriminator} `{targetq.UserId}`"; + else targetDisplay = $"User with ID `{entry.UserId}`"; + + var logEmbed = new EmbedBuilder() + .WithColor(Color.DarkGrey) + .WithTitle(Enum.GetName(typeof(ModLogType), entry.LogType) + " logged:") + .WithTimestamp(entry.Timestamp) + .WithFooter($"Log #{entry.LogId}", Bot.DiscordClient.CurrentUser.GetAvatarUrl()); // Escaping '#' not necessary here + if (entry.Message != null) { + logEmbed.Description = entry.Message; + } + + var contextStr = new StringBuilder(); + contextStr.AppendLine($"User: {targetDisplay}"); + contextStr.AppendLine($"Logged by: {issuedDisplay}"); + + logEmbed.AddField(new EmbedFieldBuilder() { + Name = "Context", + Value = contextStr.ToString() + }); + + return logEmbed.Build(); } } \ No newline at end of file diff --git a/Modules/ModLogs/ModLogs_Messages.cs b/Modules/ModLogs/ModLogs_Messages.cs index 70e9e1a..c6859e2 100644 --- a/Modules/ModLogs/ModLogs_Messages.cs +++ b/Modules/ModLogs/ModLogs_Messages.cs @@ -29,6 +29,7 @@ internal partial class ModLogs { .SingleOrDefault(); var reportEmbed = new EmbedBuilder() + .WithColor(Color.Red) .WithTitle("Message deleted") .WithCurrentTimestamp() .WithFooter($"Message ID: {argMsg.Id}"); @@ -60,7 +61,7 @@ internal partial class ModLogs { var editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(argMsg.Id))}"; if (cachedMsg?.EditedAt != null) editLine += $"\nLast edit: {MakeTimestamp(cachedMsg.EditedAt.Value)}"; - SetContextField(reportEmbed, (ulong?)cachedMsg?.AuthorId, channel, editLine, argMsg.Id); + SetContextField(reportEmbed, (ulong?)cachedMsg?.AuthorId, channel, editLine); await reportChannel.SendMessageAsync(embed: reportEmbed.Build()); } @@ -78,6 +79,7 @@ internal partial class ModLogs { } var reportEmbed = new EmbedBuilder() + .WithColor(new Color(0xffff00)) // yellow .WithTitle("Message edited") .WithCurrentTimestamp() .WithFooter($"Message ID: {newMsg.Id}"); @@ -119,12 +121,12 @@ internal partial class ModLogs { string editLine; if ((oldMsg?.EditedAt) == null) editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(newMsg.Id))}"; else editLine = $"Previous edit: {MakeTimestamp(oldMsg.EditedAt.Value)}"; - SetContextField(reportEmbed, newMsg.Author.Id, channel, editLine, newMsg.Id); + SetContextField(reportEmbed, newMsg.Author.Id, channel, editLine); await reportChannel.SendMessageAsync(embed: reportEmbed.Build()); } - private void SetContextField(EmbedBuilder e, ulong? userId, SocketTextChannel channel, string editLine, ulong msgId) { + private void SetContextField(EmbedBuilder e, ulong? userId, SocketTextChannel channel, string editLine) { string userDisplay; if (userId.HasValue) { var q = Bot.EcQueryUser(userId.Value.ToString());