2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-16 00:51:19 +02:00
|
|
|
using Catalogger.Backend.Database;
|
|
|
|
|
using Catalogger.Backend.Database.Queries;
|
|
|
|
|
using Catalogger.Backend.Extensions;
|
|
|
|
|
using Catalogger.Backend.Services;
|
|
|
|
|
using Remora.Discord.API;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
using Remora.Discord.API.Gateway.Events;
|
|
|
|
|
using Remora.Discord.API.Objects;
|
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
2024-08-20 20:25:52 +02:00
|
|
|
namespace Catalogger.Backend.Bot.Responders.Messages;
|
2024-08-16 00:51:19 +02:00
|
|
|
|
|
|
|
|
public class MessageUpdateResponder(
|
|
|
|
|
ILogger logger,
|
|
|
|
|
DatabaseContext db,
|
2024-08-19 16:12:28 +02:00
|
|
|
ChannelCache channelCache,
|
|
|
|
|
UserCache userCache,
|
2024-08-16 00:51:19 +02:00
|
|
|
MessageRepository messageRepository,
|
2024-08-16 17:03:26 +02:00
|
|
|
WebhookExecutorService webhookExecutor,
|
2024-10-09 17:35:11 +02:00
|
|
|
PluralkitApiService pluralkitApi
|
|
|
|
|
) : IResponder<IMessageUpdate>
|
2024-08-16 00:51:19 +02:00
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<MessageUpdateResponder>();
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RespondAsync(IMessageUpdate evt, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
// Discord only *very* recently changed message update events to have all fields,
|
|
|
|
|
// so we convert the event to a MessageCreate to avoid having to unwrap every single field
|
|
|
|
|
var msg = ConvertToMessageCreate(evt);
|
|
|
|
|
|
|
|
|
|
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-16 00:51:19 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var guildConfig = await db.GetGuildAsync(msg.GuildID.Value, ct);
|
|
|
|
|
|
|
|
|
|
if (await messageRepository.IsMessageIgnoredAsync(msg.ID.Value, ct))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Message {MessageId} should be ignored", msg.ID);
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
var logChannel = webhookExecutor.GetLogChannel(
|
|
|
|
|
guildConfig,
|
|
|
|
|
LogChannelType.MessageUpdate,
|
|
|
|
|
msg.ChannelID,
|
|
|
|
|
msg.Author.ID.Value
|
|
|
|
|
);
|
|
|
|
|
if (logChannel == null)
|
|
|
|
|
return Result.Success;
|
2024-08-20 18:18:17 +02:00
|
|
|
|
2024-08-16 00:51:19 +02:00
|
|
|
var oldMessage = await messageRepository.GetMessageAsync(msg.ID.Value, ct);
|
|
|
|
|
if (oldMessage == null)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
logger.Debug(
|
|
|
|
|
"Message {Id} was edited and should be logged but is not in the database",
|
|
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-16 00:51:19 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
if (
|
|
|
|
|
oldMessage.Content == msg.Content
|
|
|
|
|
|| (oldMessage.Content == "None" && string.IsNullOrEmpty(msg.Content))
|
|
|
|
|
)
|
|
|
|
|
return Result.Success;
|
2024-08-16 00:51:19 +02:00
|
|
|
|
|
|
|
|
var user = msg.Author;
|
|
|
|
|
if (msg.Author.ID != oldMessage.UserId)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
var systemAccount = await userCache.GetUserAsync(
|
|
|
|
|
DiscordSnowflake.New(oldMessage.UserId)
|
|
|
|
|
);
|
|
|
|
|
if (systemAccount != null)
|
|
|
|
|
user = systemAccount;
|
2024-08-16 00:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var embedBuilder = new EmbedBuilder()
|
|
|
|
|
.WithAuthor(user.Tag(), null, user.AvatarUrl())
|
|
|
|
|
.WithTitle("Message edited")
|
|
|
|
|
.WithDescription(oldMessage.Content)
|
|
|
|
|
.WithColour(DiscordUtils.Purple)
|
|
|
|
|
.WithFooter($"ID: {msg.ID}")
|
|
|
|
|
.WithTimestamp(msg.ID.Timestamp);
|
|
|
|
|
|
2024-08-16 17:03:26 +02:00
|
|
|
var fields = ChunksUpTo(msg.Content, 1000)
|
2024-10-09 17:35:11 +02:00
|
|
|
.Select<string, IEmbedField>(
|
|
|
|
|
(s, i) => new EmbedField($"New content{(i != 0 ? " (cont.)" : "")}", s, false)
|
|
|
|
|
)
|
2024-08-16 00:51:19 +02:00
|
|
|
.ToList();
|
|
|
|
|
embedBuilder.SetFields(fields);
|
|
|
|
|
|
|
|
|
|
string channelMention;
|
|
|
|
|
if (!channelCache.TryGet(msg.ChannelID, out var channel))
|
|
|
|
|
channelMention = $"<#{msg.ChannelID}>";
|
2024-10-09 17:35:11 +02:00
|
|
|
else if (
|
|
|
|
|
channel.Type
|
|
|
|
|
is ChannelType.AnnouncementThread
|
|
|
|
|
or ChannelType.PrivateThread
|
|
|
|
|
or ChannelType.PublicThread
|
|
|
|
|
)
|
2024-08-16 00:51:19 +02:00
|
|
|
channelMention =
|
|
|
|
|
$"<#{channel.ParentID.Value}>\nID: {channel.ParentID.Value}\n\nThread: {channel.Name} (<#{channel.ID}>)";
|
2024-10-09 17:35:11 +02:00
|
|
|
else
|
|
|
|
|
channelMention = $"<#{channel.ID}>\nID: {channel.ID}";
|
2024-08-16 00:51:19 +02:00
|
|
|
|
|
|
|
|
embedBuilder.AddField("Channel", channelMention, true);
|
|
|
|
|
embedBuilder.AddField("Sender", $"<@{user.ID}>\n{user.Tag()}\nID: {user.ID}", true);
|
|
|
|
|
|
|
|
|
|
if (oldMessage is { System: not null, Member: not null })
|
|
|
|
|
{
|
|
|
|
|
embedBuilder.WithTitle($"Message by {msg.Author.Username} edited");
|
|
|
|
|
embedBuilder.AddField("\u200b", "**PluralKit information**", false);
|
|
|
|
|
embedBuilder.AddField("System ID", oldMessage.System, true);
|
|
|
|
|
embedBuilder.AddField("Member ID", oldMessage.Member, true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
embedBuilder.AddField(
|
|
|
|
|
"Link",
|
|
|
|
|
$"https://discord.com/channels/{msg.GuildID}/{msg.ChannelID}/{msg.ID}"
|
|
|
|
|
);
|
2024-08-16 00:51:19 +02:00
|
|
|
|
2024-09-02 15:59:16 +02:00
|
|
|
webhookExecutor.QueueLog(logChannel.Value, embedBuilder.Build().GetOrThrow());
|
2024-08-16 00:51:19 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2024-08-20 18:18:17 +02:00
|
|
|
// Messages should be *saved* if any of the message events are enabled for this channel, but should only
|
|
|
|
|
// be *logged* if the MessageUpdate event is enabled, so we check if we should save here.
|
|
|
|
|
// You also can't return early in `finally` blocks, so this has to be nested :(
|
2024-10-09 17:35:11 +02:00
|
|
|
if (
|
|
|
|
|
webhookExecutor.GetLogChannel(
|
|
|
|
|
guildConfig,
|
|
|
|
|
LogChannelType.MessageUpdate,
|
|
|
|
|
msg.ChannelID,
|
|
|
|
|
msg.Author.ID.Value
|
|
|
|
|
) != null
|
|
|
|
|
|| webhookExecutor.GetLogChannel(
|
|
|
|
|
guildConfig,
|
|
|
|
|
LogChannelType.MessageDelete,
|
|
|
|
|
msg.ChannelID,
|
|
|
|
|
msg.Author.ID.Value
|
|
|
|
|
) != null
|
|
|
|
|
|| webhookExecutor.GetLogChannel(
|
|
|
|
|
guildConfig,
|
|
|
|
|
LogChannelType.MessageDeleteBulk,
|
|
|
|
|
msg.ChannelID,
|
|
|
|
|
msg.Author.ID.Value
|
|
|
|
|
) != null
|
|
|
|
|
)
|
2024-08-16 17:03:26 +02:00
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
if (
|
|
|
|
|
!await messageRepository.UpdateMessageAsync(msg, ct)
|
|
|
|
|
&& msg.ApplicationID.Is(DiscordUtils.PkUserId)
|
|
|
|
|
)
|
2024-08-20 18:18:17 +02:00
|
|
|
{
|
|
|
|
|
_logger.Debug(
|
|
|
|
|
"Message {MessageId} wasn't stored yet and was proxied by PluralKit, fetching proxy information from its API",
|
2024-10-09 17:35:11 +02:00
|
|
|
msg.ID
|
|
|
|
|
);
|
2024-08-20 18:18:17 +02:00
|
|
|
var pkMsg = await pluralkitApi.GetPluralKitMessageAsync(msg.ID.Value, ct);
|
|
|
|
|
if (pkMsg != null)
|
2024-10-09 17:35:11 +02:00
|
|
|
await messageRepository.SetProxiedMessageDataAsync(
|
|
|
|
|
msg.ID.Value,
|
|
|
|
|
pkMsg.Original,
|
|
|
|
|
pkMsg.Sender,
|
|
|
|
|
pkMsg.System?.Id,
|
|
|
|
|
pkMsg.Member?.Id
|
|
|
|
|
);
|
2024-08-20 18:18:17 +02:00
|
|
|
}
|
2024-08-16 17:03:26 +02:00
|
|
|
}
|
2024-08-16 00:51:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
private static MessageCreate ConvertToMessageCreate(IMessageUpdate evt) =>
|
|
|
|
|
new(
|
|
|
|
|
evt.GuildID,
|
|
|
|
|
evt.Member,
|
|
|
|
|
evt.Mentions.GetOrThrow(),
|
|
|
|
|
evt.ID.GetOrThrow(),
|
|
|
|
|
evt.ChannelID.GetOrThrow(),
|
|
|
|
|
evt.Author.GetOrThrow(),
|
|
|
|
|
evt.Content.GetOrThrow(),
|
|
|
|
|
evt.Timestamp.GetOrThrow(),
|
|
|
|
|
evt.EditedTimestamp.GetOrThrow(),
|
|
|
|
|
IsTTS: false,
|
|
|
|
|
evt.MentionsEveryone.GetOrThrow(),
|
|
|
|
|
evt.MentionedRoles.GetOrThrow(),
|
|
|
|
|
evt.MentionedChannels,
|
|
|
|
|
evt.Attachments.GetOrThrow(),
|
|
|
|
|
evt.Embeds.GetOrThrow(),
|
|
|
|
|
evt.Reactions,
|
|
|
|
|
evt.Nonce,
|
|
|
|
|
evt.IsPinned.GetOrThrow(),
|
|
|
|
|
evt.WebhookID,
|
|
|
|
|
evt.Type.GetOrThrow(),
|
|
|
|
|
evt.Activity,
|
|
|
|
|
evt.Application,
|
|
|
|
|
evt.ApplicationID,
|
|
|
|
|
evt.MessageReference,
|
|
|
|
|
evt.Flags,
|
|
|
|
|
evt.ReferencedMessage,
|
|
|
|
|
evt.Interaction,
|
|
|
|
|
evt.Thread,
|
|
|
|
|
evt.Components,
|
|
|
|
|
evt.StickerItems,
|
|
|
|
|
evt.Position,
|
|
|
|
|
evt.Resolved,
|
|
|
|
|
evt.InteractionMetadata,
|
|
|
|
|
evt.Poll
|
|
|
|
|
);
|
2024-08-16 17:03:26 +02:00
|
|
|
|
|
|
|
|
private static IEnumerable<string> ChunksUpTo(string str, int maxChunkSize)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < str.Length; i += maxChunkSize)
|
|
|
|
|
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i));
|
|
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|