finish ChannelUpdate responder

This commit is contained in:
sam 2024-08-20 18:18:17 +02:00
parent eb40872254
commit df8af75dd4
14 changed files with 155 additions and 63 deletions

View file

@ -1,4 +1,3 @@
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
@ -39,7 +38,6 @@ public class MessageUpdateResponder(
return Result.Success;
}
_logger.Debug("Guild is {GuildId}", msg.GuildID.Value);
var guildConfig = await db.GetGuildAsync(msg.GuildID.Value, ct);
if (await messageRepository.IsMessageIgnoredAsync(msg.ID.Value, ct))
@ -48,12 +46,12 @@ public class MessageUpdateResponder(
return Result.Success;
}
var logChannel = webhookExecutor.GetLogChannel(guildConfig, LogChannelType.MessageUpdate, msg.ChannelID,
msg.Author.ID.Value);
if (logChannel == null) return Result.Success;
try
{
var logChannel = webhookExecutor.GetLogChannel(guildConfig, LogChannelType.MessageUpdate, msg.ChannelID,
msg.Author.ID.Value);
if (logChannel == null) return Result.Success;
var oldMessage = await messageRepository.GetMessageAsync(msg.ID.Value, ct);
if (oldMessage == null)
{
@ -112,15 +110,26 @@ public class MessageUpdateResponder(
}
finally
{
if (!await messageRepository.UpdateMessageAsync(msg, ct) && msg.ApplicationID.Is(DiscordUtils.PkUserId))
// 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 :(
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)
{
_logger.Debug(
"Message {MessageId} wasn't stored yet and was proxied by PluralKit, fetching proxy information from its API",
msg.ID);
var pkMsg = await pluralkitApi.GetPluralKitMessageAsync(msg.ID.Value, ct);
if (pkMsg != null)
await messageRepository.SetProxiedMessageDataAsync(msg.ID.Value, pkMsg.Original, pkMsg.Sender,
pkMsg.System?.Id, pkMsg.Member?.Id);
if (!await messageRepository.UpdateMessageAsync(msg, ct) && msg.ApplicationID.Is(DiscordUtils.PkUserId))
{
_logger.Debug(
"Message {MessageId} wasn't stored yet and was proxied by PluralKit, fetching proxy information from its API",
msg.ID);
var pkMsg = await pluralkitApi.GetPluralKitMessageAsync(msg.ID.Value, ct);
if (pkMsg != null)
await messageRepository.SetProxiedMessageDataAsync(msg.ID.Value, pkMsg.Original, pkMsg.Sender,
pkMsg.System?.Id, pkMsg.Member?.Id);
}
}
}
}