2020-02-14 23:12:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
using Myriad.Cache;
|
|
|
|
|
using Myriad.Extensions;
|
|
|
|
|
using Myriad.Rest;
|
|
|
|
|
using Myriad.Rest.Exceptions;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
using Myriad.Types;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
using NodaTime.Extensions;
|
|
|
|
|
using NodaTime.Text;
|
|
|
|
|
|
2020-02-14 23:12:03 +00:00
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
using Serilog;
|
|
|
|
|
|
2020-02-14 23:12:03 +00:00
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
|
{
|
|
|
|
|
public class LoggerCleanService
|
|
|
|
|
{
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static readonly Regex _basicRegex = new("(\\d{17,19})");
|
|
|
|
|
private static readonly Regex _dynoRegex = new("Message ID: (\\d{17,19})");
|
|
|
|
|
private static readonly Regex _carlRegex = new("ID: (\\d{17,19})");
|
|
|
|
|
private static readonly Regex _circleRegex = new("\\(`(\\d{17,19})`\\)");
|
|
|
|
|
private static readonly Regex _loggerARegex = new("Message = (\\d{17,19})");
|
|
|
|
|
private static readonly Regex _loggerBRegex = new("MessageID:(\\d{17,19})");
|
|
|
|
|
private static readonly Regex _auttajaRegex = new("Message (\\d{17,19}) deleted");
|
|
|
|
|
private static readonly Regex _mantaroRegex = new("Message \\(?ID:? (\\d{17,19})\\)? created by .* in channel .* was deleted\\.");
|
|
|
|
|
private static readonly Regex _pancakeRegex = new("Message from <@(\\d{17,19})> deleted in");
|
|
|
|
|
private static readonly Regex _unbelievaboatRegex = new("Message ID: (\\d{17,19})");
|
|
|
|
|
private static readonly Regex _vanessaRegex = new("Message sent by <@!?(\\d{17,19})> deleted in");
|
|
|
|
|
private static readonly Regex _salRegex = new("\\(ID: (\\d{17,19})\\)");
|
|
|
|
|
private static readonly Regex _GearBotRegex = new("\\(``(\\d{17,19})``\\) in <#\\d{17,19}> has been removed.");
|
|
|
|
|
private static readonly Regex _GiselleRegex = new("\\*\\*Message ID\\*\\*: `(\\d{17,19})`");
|
2021-07-24 16:02:06 +00:00
|
|
|
|
private static readonly Regex _VortexRegex = new("`\\[(\\d\\d:\\d\\d:\\d\\d)\\]` .* \\(ID:(\\d{17,19})\\).* <#\\d{17,19}>:");
|
2020-02-15 00:41:26 +00:00
|
|
|
|
|
2020-02-14 23:12:03 +00:00
|
|
|
|
private static readonly Dictionary<ulong, LoggerBot> _bots = new[]
|
|
|
|
|
{
|
|
|
|
|
new LoggerBot("Carl-bot", 23514896210395136, fuzzyExtractFunc: ExtractCarlBot, webhookName: "Carl-bot Logging"),
|
|
|
|
|
new LoggerBot("Circle", 497196352866877441, fuzzyExtractFunc: ExtractCircle),
|
2020-02-15 00:32:18 +00:00
|
|
|
|
new LoggerBot("Pancake", 239631525350604801, fuzzyExtractFunc: ExtractPancake),
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
|
|
|
|
// There are two "Logger"s. They seem to be entirely unrelated. Don't ask.
|
|
|
|
|
new LoggerBot("Logger#6088", 298822483060981760 , ExtractLoggerA, webhookName: "Logger"),
|
|
|
|
|
new LoggerBot("Logger#6278", 327424261180620801, ExtractLoggerB),
|
|
|
|
|
|
|
|
|
|
new LoggerBot("Dyno", 155149108183695360, ExtractDyno, webhookName: "Dyno"),
|
2020-06-19 17:00:04 +00:00
|
|
|
|
new LoggerBot("Auttaja", 242730576195354624, ExtractAuttaja, webhookName: "Auttaja"),
|
2020-02-14 23:12:03 +00:00
|
|
|
|
new LoggerBot("GenericBot", 295329346590343168, ExtractGenericBot),
|
|
|
|
|
new LoggerBot("blargbot", 134133271750639616, ExtractBlargBot),
|
|
|
|
|
new LoggerBot("Mantaro", 213466096718708737, ExtractMantaro),
|
2020-02-15 00:41:26 +00:00
|
|
|
|
new LoggerBot("UnbelievaBoat", 292953664492929025, ExtractUnbelievaBoat, webhookName: "UnbelievaBoat"),
|
2020-03-04 17:32:14 +00:00
|
|
|
|
new LoggerBot("Vanessa", 310261055060443136, fuzzyExtractFunc: ExtractVanessa),
|
2020-06-18 00:46:03 +00:00
|
|
|
|
new LoggerBot("SafetyAtLast", 401549924199694338, fuzzyExtractFunc: ExtractSAL),
|
2020-11-24 05:02:36 +00:00
|
|
|
|
new LoggerBot("GearBot", 349977940198555660, fuzzyExtractFunc: ExtractGearBot),
|
2021-07-24 16:02:06 +00:00
|
|
|
|
new LoggerBot("GiselleBot", 356831787445387285, ExtractGiselleBot),
|
|
|
|
|
new LoggerBot("Vortex", 240254129333731328, fuzzyExtractFunc: ExtractVortex),
|
2020-02-14 23:12:03 +00:00
|
|
|
|
}.ToDictionary(b => b.Id);
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, LoggerBot> _botsByWebhookName = _bots.Values
|
|
|
|
|
.Where(b => b.WebhookName != null)
|
|
|
|
|
.ToDictionary(b => b.WebhookName);
|
|
|
|
|
|
2020-08-29 11:46:27 +00:00
|
|
|
|
private readonly IDatabase _db;
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private readonly DiscordApiClient _client;
|
|
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
|
private readonly Bot _bot; // todo: get rid of this nasty
|
|
|
|
|
private readonly ILogger _logger;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
public LoggerCleanService(IDatabase db, DiscordApiClient client, IDiscordCache cache, Bot bot, ILogger logger)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
_client = client;
|
2021-01-30 00:07:43 +00:00
|
|
|
|
_cache = cache;
|
|
|
|
|
_bot = bot;
|
|
|
|
|
_logger = logger.ForContext<LoggerCleanService>();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICollection<LoggerBot> Bots => _bots.Values;
|
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
|
public async ValueTask HandleLoggerBotCleanup(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
2021-01-30 00:07:43 +00:00
|
|
|
|
var channel = _cache.GetChannel(msg.ChannelId);
|
|
|
|
|
|
|
|
|
|
if (channel.Type != Channel.ChannelType.GuildText) return;
|
|
|
|
|
if (!_bot.PermissionsIn(channel.Id).HasFlag(PermissionSet.ManageMessages)) return;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
|
|
|
|
// If this message is from a *webhook*, check if the name matches one of the bots we know
|
|
|
|
|
// TODO: do we need to do a deeper webhook origin check, or would that be too hard on the rate limit?
|
|
|
|
|
// If it's from a *bot*, check the bot ID to see if we know it.
|
|
|
|
|
LoggerBot bot = null;
|
2021-01-30 00:07:43 +00:00
|
|
|
|
if (msg.WebhookId != null) _botsByWebhookName.TryGetValue(msg.Author.Username, out bot);
|
|
|
|
|
else if (msg.Author.Bot) _bots.TryGetValue(msg.Author.Id, out bot);
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
|
|
|
|
// If we didn't find anything before, or what we found is an unsupported bot, bail
|
|
|
|
|
if (bot == null) return;
|
|
|
|
|
|
2020-05-01 17:41:15 +00:00
|
|
|
|
try
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
2020-05-01 17:41:15 +00:00
|
|
|
|
// We try two ways of extracting the actual message, depending on the bots
|
|
|
|
|
if (bot.FuzzyExtractFunc != null)
|
|
|
|
|
{
|
|
|
|
|
// Some bots (Carl, Circle, etc) only give us a user ID and a rough timestamp, so we try our best to
|
|
|
|
|
// "cross-reference" those with the message DB. We know the deletion event happens *after* the message
|
|
|
|
|
// was sent, so we're checking for any messages sent in the same guild within 3 seconds before the
|
|
|
|
|
// delete event timestamp, which is... good enough, I think? Potential for false positives and negatives
|
|
|
|
|
// either way but shouldn't be too much, given it's constrained by user ID and guild.
|
|
|
|
|
var fuzzy = bot.FuzzyExtractFunc(msg);
|
|
|
|
|
if (fuzzy == null) return;
|
2021-01-30 00:07:43 +00:00
|
|
|
|
|
|
|
|
|
_logger.Debug("Fuzzy logclean for {BotName} on {MessageId}: {@FuzzyExtractResult}",
|
|
|
|
|
bot.Name, msg.Id, fuzzy);
|
|
|
|
|
|
2020-09-09 20:20:52 +00:00
|
|
|
|
var mid = await _db.Execute(conn =>
|
|
|
|
|
conn.QuerySingleOrDefaultAsync<ulong?>(
|
|
|
|
|
"select mid from messages where sender = @User and mid > @ApproxID and guild = @Guild limit 1",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
fuzzy.Value.User,
|
2021-01-30 00:07:43 +00:00
|
|
|
|
Guild = msg.GuildId,
|
2020-09-09 20:20:52 +00:00
|
|
|
|
ApproxId = DiscordUtils.InstantToSnowflake(
|
2021-01-30 00:07:43 +00:00
|
|
|
|
fuzzy.Value.ApproxTimestamp - Duration.FromSeconds(3))
|
2020-09-09 20:20:52 +00:00
|
|
|
|
}));
|
2021-01-30 00:07:43 +00:00
|
|
|
|
|
|
|
|
|
// If we didn't find a corresponding message, bail
|
|
|
|
|
if (mid == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-05-01 17:41:15 +00:00
|
|
|
|
// Otherwise, we can *reasonably assume* that this is a logged deletion, so delete the log message.
|
2021-01-30 00:07:43 +00:00
|
|
|
|
await _client.DeleteMessage(msg.ChannelId, msg.Id);
|
2020-05-01 17:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
else if (bot.ExtractFunc != null)
|
|
|
|
|
{
|
|
|
|
|
// Other bots give us the message ID itself, and we can just extract that from the database directly.
|
|
|
|
|
var extractedId = bot.ExtractFunc(msg);
|
|
|
|
|
if (extractedId == null) return; // If we didn't find anything, bail.
|
2021-01-30 00:07:43 +00:00
|
|
|
|
|
|
|
|
|
_logger.Debug("Pure logclean for {BotName} on {MessageId}: {@FuzzyExtractResult}",
|
|
|
|
|
bot.Name, msg.Id, extractedId);
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
2020-09-09 20:20:52 +00:00
|
|
|
|
var mid = await _db.Execute(conn => conn.QuerySingleOrDefaultAsync<ulong?>(
|
|
|
|
|
"select mid from messages where original_mid = @Mid", new {Mid = extractedId.Value}));
|
2020-05-01 17:41:15 +00:00
|
|
|
|
if (mid == null) return;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
2020-05-01 17:41:15 +00:00
|
|
|
|
// If we've gotten this far, we found a logged deletion of a trigger message. Just yeet it!
|
2021-01-30 00:07:43 +00:00
|
|
|
|
await _client.DeleteMessage(msg.ChannelId, msg.Id);
|
2020-05-01 17:41:15 +00:00
|
|
|
|
} // else should not happen, but idk, it might
|
|
|
|
|
}
|
|
|
|
|
catch (NotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// Sort of a temporary measure: getting an error in Sentry about a NotFoundException from D#+ here
|
|
|
|
|
// The only thing I can think of that'd cause this are the DeleteAsync() calls which 404 when
|
|
|
|
|
// the message doesn't exist anyway - so should be safe to just ignore it, right?
|
|
|
|
|
}
|
2020-02-14 23:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractAuttaja(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Auttaja has an optional "compact mode" that logs without embeds
|
|
|
|
|
// That one puts the ID in the message content, non-compact puts it in the embed description.
|
|
|
|
|
// Regex also checks that this is a deletion.
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var stringWithId = msg.Embeds?.FirstOrDefault()?.Description ?? msg.Content;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (stringWithId == null) return null;
|
|
|
|
|
|
|
|
|
|
var match = _auttajaRegex.Match(stringWithId);
|
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractDyno(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Embed *description* contains "Message sent by [mention] deleted in [channel]", contains message ID in footer per regex
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed?.Footer == null || !(embed.Description?.Contains("deleted in") ?? false)) return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
var match = _dynoRegex.Match(embed.Footer.Text ?? "");
|
2020-02-14 23:12:03 +00:00
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractLoggerA(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// This is for Logger#6088 (298822483060981760), distinct from Logger#6278 (327424261180620801).
|
|
|
|
|
// Embed contains title "Message deleted in [channel]", and an ID field containing both message and user ID (see regex).
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed == null) return null;
|
|
|
|
|
if (!embed.Description.StartsWith("Message deleted in")) return null;
|
|
|
|
|
|
|
|
|
|
var idField = embed.Fields.FirstOrDefault(f => f.Name == "ID");
|
|
|
|
|
if (idField.Value == null) return null; // "OrDefault" = all-null object
|
|
|
|
|
var match = _loggerARegex.Match(idField.Value);
|
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractLoggerB(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// This is for Logger#6278 (327424261180620801), distinct from Logger#6088 (298822483060981760).
|
|
|
|
|
// Embed title ends with "A Message Was Deleted!", footer contains message ID as per regex.
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed?.Footer == null || !(embed.Title?.EndsWith("A Message Was Deleted!") ?? false)) return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
var match = _loggerBRegex.Match(embed.Footer.Text ?? "");
|
2020-02-14 23:12:03 +00:00
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractGenericBot(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Embed, title is "Message Deleted", ID plain in footer.
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed?.Footer == null || !(embed.Title?.Contains("Message Deleted") ?? false)) return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
var match = _basicRegex.Match(embed.Footer.Text ?? "");
|
2020-02-14 23:12:03 +00:00
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractBlargBot(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Embed, title ends with "Message Deleted", contains ID plain in a field.
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed == null || !(embed.Title?.EndsWith("Message Deleted") ?? false)) return null;
|
|
|
|
|
var field = embed.Fields.FirstOrDefault(f => f.Name == "Message ID");
|
|
|
|
|
var match = _basicRegex.Match(field.Value ?? "");
|
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractMantaro(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Plain message, "Message (ID: [id]) created by [user] (ID: [id]) in channel [channel] was deleted.
|
|
|
|
|
if (!(msg.Content?.Contains("was deleted.") ?? false)) return null;
|
|
|
|
|
var match = _mantaroRegex.Match(msg.Content);
|
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractCarlBot(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Embed, title is "Message deleted in [channel], **user** ID in the footer, timestamp as, well, timestamp in embed.
|
|
|
|
|
// This is the *deletion* timestamp, which we can assume is a couple seconds at most after the message was originally sent
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-14 23:12:03 +00:00
|
|
|
|
if (embed?.Footer == null || embed.Timestamp == null || !(embed.Title?.StartsWith("Message deleted in") ?? false)) return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
var match = _carlRegex.Match(embed.Footer.Text ?? "");
|
2020-02-14 23:12:03 +00:00
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = OffsetDateTimePattern.Rfc3339.Parse(embed.Timestamp).GetValueOrThrow().ToInstant()
|
|
|
|
|
}
|
2020-02-14 23:12:03 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractCircle(Message msg)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Like Auttaja, Circle has both embed and compact modes, but the regex works for both.
|
|
|
|
|
// Compact: "Message from [user] ([id]) deleted in [channel]", no timestamp (use message time)
|
|
|
|
|
// Embed: Message Author field: "[user] ([id])", then an embed timestamp
|
|
|
|
|
string stringWithId = msg.Content;
|
2021-04-13 02:07:03 +00:00
|
|
|
|
if (msg.Embeds?.Length > 0)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.First();
|
2020-04-17 21:10:01 +00:00
|
|
|
|
if (embed.Author?.Name == null || !embed.Author.Name.StartsWith("Message Deleted in")) return null;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
var field = embed.Fields.FirstOrDefault(f => f.Name == "Message Author");
|
|
|
|
|
if (field.Value == null) return null;
|
|
|
|
|
stringWithId = field.Value;
|
|
|
|
|
}
|
|
|
|
|
if (stringWithId == null) return null;
|
|
|
|
|
|
|
|
|
|
var match = _circleRegex.Match(stringWithId);
|
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult {
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
2020-02-15 00:32:18 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractPancake(Message msg)
|
2020-02-15 00:32:18 +00:00
|
|
|
|
{
|
2020-02-15 00:41:26 +00:00
|
|
|
|
// Embed, author is "Message Deleted", description includes a mention, timestamp is *message send time* (but no ID)
|
2020-02-15 00:32:18 +00:00
|
|
|
|
// so we use the message timestamp to get somewhere *after* the message was proxied
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-15 00:32:18 +00:00
|
|
|
|
if (embed?.Description == null || embed.Author?.Name != "Message Deleted") return null;
|
|
|
|
|
var match = _pancakeRegex.Match(embed.Description);
|
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
2020-02-14 23:12:03 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
2020-02-15 00:41:26 +00:00
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractUnbelievaBoat(Message msg)
|
2020-02-15 00:41:26 +00:00
|
|
|
|
{
|
|
|
|
|
// Embed author is "Message Deleted", footer contains message ID per regex
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-02-15 00:41:26 +00:00
|
|
|
|
if (embed?.Footer == null || embed.Author?.Name != "Message Deleted") return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
var match = _unbelievaboatRegex.Match(embed.Footer.Text ?? "");
|
2020-02-15 00:41:26 +00:00
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
2020-03-04 17:32:14 +00:00
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractVanessa(Message msg)
|
2020-03-04 17:32:14 +00:00
|
|
|
|
{
|
|
|
|
|
// Title is "Message Deleted", embed description contains mention
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-03-04 17:32:14 +00:00
|
|
|
|
if (embed?.Title == null || embed.Title != "Message Deleted" || embed.Description == null) return null;
|
|
|
|
|
var match = _vanessaRegex.Match(embed.Description);
|
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
2020-05-02 18:42:42 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractSAL(Message msg)
|
2020-05-02 18:42:42 +00:00
|
|
|
|
{
|
|
|
|
|
// Title is "Message Deleted!", field "Message Author" contains ID
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-05-02 18:42:42 +00:00
|
|
|
|
if (embed?.Title == null || embed.Title != "Message Deleted!") return null;
|
|
|
|
|
var authorField = embed.Fields.FirstOrDefault(f => f.Name == "Message Author");
|
|
|
|
|
if (authorField == null) return null;
|
|
|
|
|
var match = _salRegex.Match(authorField.Value);
|
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
2020-06-18 00:46:03 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractGearBot(Message msg)
|
2020-06-18 00:46:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Simple text based message log.
|
|
|
|
|
// No message ID, but we have timestamp and author ID.
|
|
|
|
|
// Not using timestamp here though (seems to be same as message timestamp), might be worth implementing in the future.
|
|
|
|
|
var match = _GearBotRegex.Match(msg.Content);
|
|
|
|
|
return match.Success
|
2021-01-30 00:07:43 +00:00
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[1].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
2020-03-04 17:32:14 +00:00
|
|
|
|
: (FuzzyExtractResult?) null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
private static ulong? ExtractGiselleBot(Message msg)
|
2020-11-24 05:02:36 +00:00
|
|
|
|
{
|
2021-04-13 02:07:03 +00:00
|
|
|
|
var embed = msg.Embeds?.FirstOrDefault();
|
2020-11-24 05:02:36 +00:00
|
|
|
|
if (embed?.Title == null || embed.Title != "🗑 Message Deleted") return null;
|
|
|
|
|
var match = _GiselleRegex.Match(embed?.Description);
|
|
|
|
|
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
|
|
|
|
}
|
2020-02-14 23:12:03 +00:00
|
|
|
|
|
2021-07-24 16:02:06 +00:00
|
|
|
|
private static FuzzyExtractResult? ExtractVortex(Message msg)
|
|
|
|
|
{
|
|
|
|
|
// timestamp is HH:MM:SS
|
|
|
|
|
// however, that can be set to the user's timezone, so we just use the message timestamp
|
|
|
|
|
var match = _VortexRegex.Match(msg.Content);
|
|
|
|
|
return match.Success
|
|
|
|
|
? new FuzzyExtractResult
|
|
|
|
|
{
|
|
|
|
|
User = ulong.Parse(match.Groups[2].Value),
|
|
|
|
|
ApproxTimestamp = msg.Timestamp().ToInstant()
|
|
|
|
|
}
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 23:12:03 +00:00
|
|
|
|
public class LoggerBot
|
|
|
|
|
{
|
|
|
|
|
public string Name;
|
|
|
|
|
public ulong Id;
|
2021-01-30 00:07:43 +00:00
|
|
|
|
public Func<Message, ulong?> ExtractFunc;
|
|
|
|
|
public Func<Message, FuzzyExtractResult?> FuzzyExtractFunc;
|
2020-02-14 23:12:03 +00:00
|
|
|
|
public string WebhookName;
|
|
|
|
|
|
2021-01-30 00:07:43 +00:00
|
|
|
|
public LoggerBot(string name, ulong id, Func<Message, ulong?> extractFunc = null, Func<Message, FuzzyExtractResult?> fuzzyExtractFunc = null, string webhookName = null)
|
2020-02-14 23:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Id = id;
|
|
|
|
|
FuzzyExtractFunc = fuzzyExtractFunc;
|
|
|
|
|
ExtractFunc = extractFunc;
|
|
|
|
|
WebhookName = webhookName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct FuzzyExtractResult
|
|
|
|
|
{
|
2021-01-30 00:07:43 +00:00
|
|
|
|
public ulong User { get; set; }
|
|
|
|
|
public Instant ApproxTimestamp { get; set; }
|
2020-02-14 23:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|