2024-10-14 14:56:40 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
using System.Text.RegularExpressions;
|
2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Database;
|
|
|
|
|
using Catalogger.Backend.Database.Models;
|
|
|
|
|
using Catalogger.Backend.Database.Queries;
|
|
|
|
|
using Catalogger.Backend.Extensions;
|
|
|
|
|
using Catalogger.Backend.Services;
|
|
|
|
|
using Humanizer;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
2024-08-20 20:25:52 +02:00
|
|
|
namespace Catalogger.Backend.Bot.Responders.Messages;
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
public class MessageCreateResponder(
|
|
|
|
|
ILogger logger,
|
2024-08-13 16:48:54 +02:00
|
|
|
Config config,
|
2024-08-13 13:08:50 +02:00
|
|
|
DatabaseContext db,
|
|
|
|
|
MessageRepository messageRepository,
|
2024-08-19 16:12:28 +02:00
|
|
|
UserCache userCache,
|
2024-10-09 17:35:11 +02:00
|
|
|
PkMessageHandler pkMessageHandler
|
|
|
|
|
) : IResponder<IMessageCreate>
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<MessageCreateResponder>();
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RespondAsync(IMessageCreate msg, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
userCache.UpdateUser(msg.Author);
|
2024-08-20 20:19:24 +02:00
|
|
|
CataloggerMetrics.MessagesReceived.Inc();
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
if (!msg.GuildID.IsDefined())
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Received message create event for message {MessageId} despite it not being in a guild",
|
|
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 16:04:01 +02:00
|
|
|
var guild = await db.GetGuildAsync(msg.GuildID, false, ct);
|
2024-08-13 13:08:50 +02:00
|
|
|
// The guild needs to have enabled at least one of the message logging events,
|
|
|
|
|
// and the channel must not be ignored, to store the message.
|
|
|
|
|
if (guild.IsMessageIgnored(msg.ChannelID, msg.Author.ID))
|
|
|
|
|
{
|
|
|
|
|
db.IgnoredMessages.Add(new IgnoredMessage(msg.ID.ToUlong()));
|
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 17:03:26 +02:00
|
|
|
if (msg.Author.ID == DiscordUtils.PkUserId)
|
2024-08-13 13:08:50 +02:00
|
|
|
_ = pkMessageHandler.HandlePkMessageAsync(msg);
|
2024-08-16 17:03:26 +02:00
|
|
|
if (msg.ApplicationID.Is(DiscordUtils.PkUserId))
|
2024-08-13 13:08:50 +02:00
|
|
|
_ = pkMessageHandler.HandleProxiedMessageAsync(msg.ID.Value);
|
2024-08-16 17:03:26 +02:00
|
|
|
else if (msg.ApplicationID.HasValue && msg.ApplicationID.Is(config.Discord.ApplicationId))
|
2024-08-13 16:48:54 +02:00
|
|
|
{
|
|
|
|
|
db.IgnoredMessages.Add(new IgnoredMessage(msg.ID.Value));
|
|
|
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
await messageRepository.SaveMessageAsync(msg, ct);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class PkMessageHandler(ILogger logger, IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<PkMessageHandler>();
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
[GeneratedRegex(
|
2024-10-09 17:35:11 +02:00
|
|
|
@"^System ID: (\w{5,6}) \| Member ID: (\w{5,6}) \| Sender: .+ \((\d+)\) \| Message ID: (\d+) \| Original Message ID: (\d+)$"
|
|
|
|
|
)]
|
2024-08-13 13:08:50 +02:00
|
|
|
private static partial Regex FooterRegex();
|
|
|
|
|
|
|
|
|
|
[GeneratedRegex(@"^https:\/\/discord.com\/channels\/\d+\/(\d+)\/\d+$")]
|
|
|
|
|
private static partial Regex LinkRegex();
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public async Task HandlePkMessageAsync(IMessageCreate msg)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Received PluralKit message");
|
|
|
|
|
|
|
|
|
|
await Task.Delay(500.Milliseconds());
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
_logger.Debug("Starting handling PluralKit message");
|
|
|
|
|
|
|
|
|
|
// Check if the content matches a Discord link--if not, it's not a log message (we already check if this is a PluralKit message earlier)
|
|
|
|
|
if (!LinkRegex().IsMatch(msg.Content))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("PluralKit message is not a log message because content is not a link");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The first (only, I think always?) embed's footer must match the expected format
|
|
|
|
|
var firstEmbed = msg.Embeds.FirstOrDefault();
|
2024-10-09 17:35:11 +02:00
|
|
|
if (
|
|
|
|
|
firstEmbed == null
|
|
|
|
|
|| !firstEmbed.Footer.TryGet(out var footer)
|
|
|
|
|
|| !FooterRegex().IsMatch(footer.Text)
|
|
|
|
|
)
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
|
|
|
|
_logger.Debug(
|
2024-10-09 17:35:11 +02:00
|
|
|
"PK message is not a log message because there is no first embed or its footer doesn't match the regex"
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var match = FooterRegex().Match(footer.Text);
|
|
|
|
|
|
|
|
|
|
if (!ulong.TryParse(match.Groups[3].Value, out var authorId))
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Author ID in PluralKit log {LogMessageId} was not a valid snowflake",
|
|
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ulong.TryParse(match.Groups[4].Value, out var msgId))
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Message ID in PluralKit log {LogMessageId} was not a valid snowflake",
|
|
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ulong.TryParse(match.Groups[5].Value, out var originalId))
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Original ID in PluralKit log {LogMessageId} was not a valid snowflake",
|
|
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await using var scope = services.CreateAsyncScope();
|
|
|
|
|
await using var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
|
|
|
|
var messageRepository = scope.ServiceProvider.GetRequiredService<MessageRepository>();
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
await messageRepository.SetProxiedMessageDataAsync(
|
|
|
|
|
msgId,
|
|
|
|
|
originalId,
|
|
|
|
|
authorId,
|
|
|
|
|
systemId: match.Groups[1].Value,
|
|
|
|
|
memberId: match.Groups[2].Value
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
db.IgnoredMessages.Add(new IgnoredMessage(originalId));
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public async Task HandleProxiedMessageAsync(ulong msgId)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(3.Seconds());
|
2024-08-13 16:48:54 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
await using var scope = services.CreateAsyncScope();
|
|
|
|
|
await using var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
|
|
|
|
var messageRepository = scope.ServiceProvider.GetRequiredService<MessageRepository>();
|
|
|
|
|
var pluralkitApi = scope.ServiceProvider.GetRequiredService<PluralkitApiService>();
|
|
|
|
|
|
|
|
|
|
var (isStored, hasProxyInfo) = await messageRepository.HasProxyInfoAsync(msgId);
|
|
|
|
|
if (!isStored)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Message with ID {MessageId} is not stored in the database", msgId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
if (hasProxyInfo)
|
|
|
|
|
return;
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
var pkMessage = await pluralkitApi.GetPluralKitMessageAsync(msgId);
|
|
|
|
|
if (pkMessage == null)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Message with ID {MessageId} was proxied by PluralKit, but API returned 404",
|
|
|
|
|
msgId
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
await messageRepository.SetProxiedMessageDataAsync(
|
|
|
|
|
msgId,
|
|
|
|
|
pkMessage.Original,
|
|
|
|
|
pkMessage.Sender,
|
|
|
|
|
pkMessage.System?.Id,
|
|
|
|
|
pkMessage.Member?.Id
|
|
|
|
|
);
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
db.IgnoredMessages.Add(new IgnoredMessage(pkMessage.Original));
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|