2022-07-08 19:03:15 +00:00
|
|
|
using Discord;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using RegexBot.Data;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace RegexBot.Modules.ModLogs;
|
|
|
|
// Contains handlers and all logic relating to logging message edits and deletions
|
2022-07-21 03:34:29 +00:00
|
|
|
internal partial class ModLogs {
|
2022-07-08 19:03:15 +00:00
|
|
|
const string PreviewCutoffNotify = "**Message too long to preview; showing first {0} characters.**\n\n";
|
|
|
|
const string NotCached = "Message not cached.";
|
2022-07-28 01:05:33 +00:00
|
|
|
const string MessageContentNull = "(blank)";
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
|
|
private async Task HandleDelete(Cacheable<IMessage, ulong> argMsg, Cacheable<IMessageChannel, ulong> argChannel) {
|
|
|
|
const int MaxPreviewLength = 750;
|
|
|
|
if (argChannel.Value is not SocketTextChannel channel) return;
|
2023-07-17 17:46:41 +00:00
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
var conf = GetGuildState<ModuleConfig>(channel.Guild.Id);
|
2022-08-24 03:39:44 +00:00
|
|
|
if ((conf?.LogMessageDeletions ?? false) == false) return;
|
2022-07-08 19:03:15 +00:00
|
|
|
var reportChannel = conf?.ReportingChannel?.FindChannelIn(channel.Guild, true);
|
|
|
|
if (reportChannel == null) return;
|
|
|
|
if (reportChannel.Id == channel.Id) {
|
2022-08-24 03:39:44 +00:00
|
|
|
Log(channel.Guild, "Message deleted in the reporting channel. Suppressing report.");
|
2022-07-08 19:03:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using var db = new BotDatabaseContext();
|
|
|
|
var cachedMsg = db.GuildMessageCache
|
|
|
|
.Include(gm => gm.Author)
|
|
|
|
.Where(gm => gm.MessageId == (long)argMsg.Id)
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
|
|
|
var reportEmbed = new EmbedBuilder()
|
2022-12-04 01:18:48 +00:00
|
|
|
.WithColor(Color.Red)
|
2022-07-08 19:03:15 +00:00
|
|
|
.WithTitle("Message deleted")
|
2022-07-28 21:10:07 +00:00
|
|
|
.WithCurrentTimestamp()
|
2022-12-03 04:06:51 +00:00
|
|
|
.WithFooter($"Message ID: {argMsg.Id}");
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
|
|
if (cachedMsg != null) {
|
2022-07-28 01:05:33 +00:00
|
|
|
if (cachedMsg.Content == null) {
|
|
|
|
reportEmbed.Description = MessageContentNull;
|
|
|
|
} else if (cachedMsg.Content.Length > MaxPreviewLength) {
|
2022-07-08 19:03:15 +00:00
|
|
|
reportEmbed.Description = string.Format(PreviewCutoffNotify, MaxPreviewLength) +
|
|
|
|
cachedMsg.Content[MaxPreviewLength..];
|
|
|
|
} else {
|
|
|
|
reportEmbed.Description = cachedMsg.Content;
|
|
|
|
}
|
|
|
|
if (cachedMsg.Author == null) {
|
|
|
|
reportEmbed.Author = new EmbedAuthorBuilder() {
|
|
|
|
Name = $"User ID {cachedMsg.AuthorId}",
|
|
|
|
IconUrl = GetDefaultAvatarUrl("0")
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
reportEmbed.Author = new EmbedAuthorBuilder() {
|
|
|
|
Name = $"{cachedMsg.Author.Username}#{cachedMsg.Author.Discriminator}",
|
|
|
|
IconUrl = cachedMsg.Author.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.Discriminator)
|
|
|
|
};
|
|
|
|
}
|
2022-08-24 03:39:44 +00:00
|
|
|
SetAttachmentsField(reportEmbed, cachedMsg.AttachmentNames);
|
2022-07-08 19:03:15 +00:00
|
|
|
} else {
|
|
|
|
reportEmbed.Description = NotCached;
|
|
|
|
}
|
|
|
|
|
2022-08-24 03:39:44 +00:00
|
|
|
var editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(argMsg.Id))}";
|
|
|
|
if (cachedMsg?.EditedAt != null) editLine += $"\nLast edit: {MakeTimestamp(cachedMsg.EditedAt.Value)}";
|
2022-12-04 01:18:48 +00:00
|
|
|
SetContextField(reportEmbed, (ulong?)cachedMsg?.AuthorId, channel, editLine);
|
2022-07-08 19:03:15 +00:00
|
|
|
|
2022-08-24 03:39:44 +00:00
|
|
|
await reportChannel.SendMessageAsync(embed: reportEmbed.Build());
|
2022-08-17 23:59:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
private async Task HandleUpdate(CachedGuildMessage? oldMsg, SocketMessage newMsg) {
|
|
|
|
const int MaxPreviewLength = 500;
|
|
|
|
var channel = (SocketTextChannel)newMsg.Channel;
|
|
|
|
var conf = GetGuildState<ModuleConfig>(channel.Guild.Id);
|
2022-08-24 03:39:44 +00:00
|
|
|
|
2023-07-17 17:46:41 +00:00
|
|
|
if (newMsg.Author.IsBot || newMsg.Author.IsWebhook) return;
|
2022-07-08 19:03:15 +00:00
|
|
|
var reportChannel = conf?.ReportingChannel?.FindChannelIn(channel.Guild, true);
|
|
|
|
if (reportChannel == null) return;
|
|
|
|
if (reportChannel.Id == channel.Id) {
|
2022-08-24 03:39:44 +00:00
|
|
|
Log(channel.Guild, "Message edited in the reporting channel. Suppressing report.");
|
2022-07-08 19:03:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var reportEmbed = new EmbedBuilder()
|
2022-12-04 01:18:48 +00:00
|
|
|
.WithColor(new Color(0xffff00)) // yellow
|
2022-07-08 19:03:15 +00:00
|
|
|
.WithTitle("Message edited")
|
2022-07-28 21:10:07 +00:00
|
|
|
.WithCurrentTimestamp()
|
2022-12-03 04:06:51 +00:00
|
|
|
.WithFooter($"Message ID: {newMsg.Id}");
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
|
|
reportEmbed.Author = new EmbedAuthorBuilder() {
|
|
|
|
Name = $"{newMsg.Author.Username}#{newMsg.Author.Discriminator}",
|
|
|
|
IconUrl = newMsg.Author.GetAvatarUrl() ?? newMsg.Author.GetDefaultAvatarUrl()
|
|
|
|
};
|
|
|
|
|
|
|
|
var oldField = new EmbedFieldBuilder() { Name = "Old" };
|
|
|
|
if (oldMsg != null) {
|
2022-07-28 01:05:33 +00:00
|
|
|
if (oldMsg.Content == null) {
|
|
|
|
oldField.Value = MessageContentNull;
|
|
|
|
} else if (oldMsg.Content.Length > MaxPreviewLength) {
|
2022-07-08 19:03:15 +00:00
|
|
|
oldField.Value = string.Format(PreviewCutoffNotify, MaxPreviewLength) +
|
|
|
|
oldMsg.Content[MaxPreviewLength..];
|
|
|
|
} else {
|
|
|
|
oldField.Value = oldMsg.Content;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
oldField.Value = NotCached;
|
|
|
|
}
|
|
|
|
reportEmbed.AddField(oldField);
|
|
|
|
|
|
|
|
// TODO shorten 'new' preview, add clickable? check if this would be good usability-wise
|
|
|
|
var newField = new EmbedFieldBuilder() { Name = "New" };
|
2022-07-28 01:05:33 +00:00
|
|
|
if (newMsg.Content == null) {
|
|
|
|
newField.Value = MessageContentNull;
|
|
|
|
} else if (newMsg.Content.Length > MaxPreviewLength) {
|
2022-07-08 19:03:15 +00:00
|
|
|
newField.Value = string.Format(PreviewCutoffNotify, MaxPreviewLength) +
|
|
|
|
newMsg.Content[MaxPreviewLength..];
|
|
|
|
} else {
|
|
|
|
newField.Value = newMsg.Content;
|
|
|
|
}
|
|
|
|
reportEmbed.AddField(newField);
|
2022-07-28 21:10:07 +00:00
|
|
|
|
2022-08-24 03:39:44 +00:00
|
|
|
SetAttachmentsField(reportEmbed, newMsg.Attachments.Select(a => a.Filename));
|
|
|
|
|
|
|
|
string editLine;
|
|
|
|
if ((oldMsg?.EditedAt) == null) editLine = $"Posted: {MakeTimestamp(SnowflakeUtils.FromSnowflake(newMsg.Id))}";
|
|
|
|
else editLine = $"Previous edit: {MakeTimestamp(oldMsg.EditedAt.Value)}";
|
2022-12-04 01:18:48 +00:00
|
|
|
SetContextField(reportEmbed, newMsg.Author.Id, channel, editLine);
|
2022-08-24 03:39:44 +00:00
|
|
|
|
|
|
|
await reportChannel.SendMessageAsync(embed: reportEmbed.Build());
|
|
|
|
}
|
|
|
|
|
2022-12-04 01:18:48 +00:00
|
|
|
private void SetContextField(EmbedBuilder e, ulong? userId, SocketTextChannel channel, string editLine) {
|
2022-08-24 03:39:44 +00:00
|
|
|
string userDisplay;
|
|
|
|
if (userId.HasValue) {
|
|
|
|
var q = Bot.EcQueryUser(userId.Value.ToString());
|
|
|
|
if (q != null) userDisplay = $"<@{q.UserId}> - {q.Username}#{q.Discriminator} `{q.UserId}`";
|
|
|
|
else userDisplay = $"Unknown user with ID `{userId}`";
|
|
|
|
} else {
|
|
|
|
userDisplay = "Unknown";
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
var contextStr = new StringBuilder();
|
2022-08-24 03:39:44 +00:00
|
|
|
contextStr.AppendLine($"User: {userDisplay}");
|
2022-07-08 19:03:15 +00:00
|
|
|
contextStr.AppendLine($"Channel: <#{channel.Id}> (#{channel.Name})");
|
2022-08-24 03:39:44 +00:00
|
|
|
contextStr.AppendLine(editLine);
|
|
|
|
|
|
|
|
e.AddField(new EmbedFieldBuilder() {
|
2022-07-08 19:03:15 +00:00
|
|
|
Name = "Context",
|
|
|
|
Value = contextStr.ToString()
|
2022-08-24 03:39:44 +00:00
|
|
|
});
|
2022-07-08 19:03:15 +00:00
|
|
|
}
|
2022-07-28 21:10:07 +00:00
|
|
|
|
2022-08-24 03:39:44 +00:00
|
|
|
private static void SetAttachmentsField(EmbedBuilder e, IEnumerable<string> attachments) {
|
2022-07-28 21:10:07 +00:00
|
|
|
if (attachments.Any()) {
|
|
|
|
var field = new EmbedFieldBuilder { Name = "Attachments" };
|
|
|
|
var attachNames = new StringBuilder();
|
|
|
|
foreach (var name in attachments) {
|
|
|
|
attachNames.AppendLine($"`{name}`");
|
|
|
|
}
|
|
|
|
field.Value = attachNames.ToString().TrimEnd();
|
2022-08-24 03:39:44 +00:00
|
|
|
e.AddField(field);
|
2022-07-28 21:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-08 19:03:15 +00:00
|
|
|
}
|