Add color codes to logging embeds

This commit is contained in:
Noi 2022-12-03 17:18:48 -08:00
parent 6bdf528c66
commit a707792fe4
3 changed files with 39 additions and 34 deletions

View file

@ -66,36 +66,6 @@ public static class Utilities {
return results; return results;
} }
/// <summary>
/// Builds and returns an embed which displays this log entry.
/// </summary>
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();
}
/// <summary> /// <summary>
/// Returns a representation of this entity that can be parsed by the <seealso cref="EntityName"/> constructor. /// Returns a representation of this entity that can be parsed by the <seealso cref="EntityName"/> constructor.
/// </summary> /// </summary>

View file

@ -1,5 +1,7 @@
using Discord;
using RegexBot.Common; using RegexBot.Common;
using RegexBot.Data; using RegexBot.Data;
using System.Text;
namespace RegexBot.Modules.ModLogs; namespace RegexBot.Modules.ModLogs;
// Contains all logic relating to reporting new database mod log entries // 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); var reportChannel = conf?.ReportingChannel?.FindChannelIn(guild, true);
if (reportChannel == null) return; if (reportChannel == null) return;
await reportChannel.SendMessageAsync(embed: entry.BuildEmbed(Bot)); await reportChannel.SendMessageAsync(embed: BuildLogEmbed(entry));
}
/// <summary>
/// Builds and returns an embed which displays this log entry.
/// </summary>
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();
} }
} }

View file

@ -29,6 +29,7 @@ internal partial class ModLogs {
.SingleOrDefault(); .SingleOrDefault();
var reportEmbed = new EmbedBuilder() var reportEmbed = new EmbedBuilder()
.WithColor(Color.Red)
.WithTitle("Message deleted") .WithTitle("Message deleted")
.WithCurrentTimestamp() .WithCurrentTimestamp()
.WithFooter($"Message ID: {argMsg.Id}"); .WithFooter($"Message ID: {argMsg.Id}");
@ -60,7 +61,7 @@ internal partial class ModLogs {
var editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(argMsg.Id))}"; var editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(argMsg.Id))}";
if (cachedMsg?.EditedAt != null) editLine += $"\nLast edit: {MakeTimestamp(cachedMsg.EditedAt.Value)}"; 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()); await reportChannel.SendMessageAsync(embed: reportEmbed.Build());
} }
@ -78,6 +79,7 @@ internal partial class ModLogs {
} }
var reportEmbed = new EmbedBuilder() var reportEmbed = new EmbedBuilder()
.WithColor(new Color(0xffff00)) // yellow
.WithTitle("Message edited") .WithTitle("Message edited")
.WithCurrentTimestamp() .WithCurrentTimestamp()
.WithFooter($"Message ID: {newMsg.Id}"); .WithFooter($"Message ID: {newMsg.Id}");
@ -119,12 +121,12 @@ internal partial class ModLogs {
string editLine; string editLine;
if ((oldMsg?.EditedAt) == null) editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(newMsg.Id))}"; if ((oldMsg?.EditedAt) == null) editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(newMsg.Id))}";
else editLine = $"Previous edit: {MakeTimestamp(oldMsg.EditedAt.Value)}"; 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()); 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; string userDisplay;
if (userId.HasValue) { if (userId.HasValue) {
var q = Bot.EcQueryUser(userId.Value.ToString()); var q = Bot.EcQueryUser(userId.Value.ToString());