chore: format with csharpier

This commit is contained in:
sam 2024-10-09 17:35:11 +02:00
parent 2f516dcb73
commit 4f54077c68
59 changed files with 2000 additions and 942 deletions

View file

@ -21,7 +21,8 @@ public class MessageUpdateResponder(
UserCache userCache,
MessageRepository messageRepository,
WebhookExecutorService webhookExecutor,
PluralkitApiService pluralkitApi) : IResponder<IMessageUpdate>
PluralkitApiService pluralkitApi
) : IResponder<IMessageUpdate>
{
private readonly ILogger _logger = logger.ForContext<MessageUpdateResponder>();
@ -33,8 +34,10 @@ public class MessageUpdateResponder(
if (!msg.GuildID.IsDefined())
{
_logger.Debug("Received message create event for message {MessageId} despite it not being in a guild",
msg.ID);
_logger.Debug(
"Received message create event for message {MessageId} despite it not being in a guild",
msg.ID
);
return Result.Success;
}
@ -48,25 +51,39 @@ public class MessageUpdateResponder(
try
{
var logChannel = webhookExecutor.GetLogChannel(guildConfig, LogChannelType.MessageUpdate, msg.ChannelID,
msg.Author.ID.Value);
if (logChannel == null) return Result.Success;
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)
{
logger.Debug("Message {Id} was edited and should be logged but is not in the database", msg.ID);
logger.Debug(
"Message {Id} was edited and should be logged but is not in the database",
msg.ID
);
return Result.Success;
}
if (oldMessage.Content == msg.Content ||
(oldMessage.Content == "None" && string.IsNullOrEmpty(msg.Content))) return Result.Success;
if (
oldMessage.Content == msg.Content
|| (oldMessage.Content == "None" && string.IsNullOrEmpty(msg.Content))
)
return Result.Success;
var user = msg.Author;
if (msg.Author.ID != oldMessage.UserId)
{
var systemAccount = await userCache.GetUserAsync(DiscordSnowflake.New(oldMessage.UserId));
if (systemAccount != null) user = systemAccount;
var systemAccount = await userCache.GetUserAsync(
DiscordSnowflake.New(oldMessage.UserId)
);
if (systemAccount != null)
user = systemAccount;
}
var embedBuilder = new EmbedBuilder()
@ -78,19 +95,25 @@ public class MessageUpdateResponder(
.WithTimestamp(msg.ID.Timestamp);
var fields = ChunksUpTo(msg.Content, 1000)
.Select<string, IEmbedField>((s, i) =>
new EmbedField($"New content{(i != 0 ? " (cont.)" : "")}", s, false))
.Select<string, IEmbedField>(
(s, i) => new EmbedField($"New content{(i != 0 ? " (cont.)" : "")}", s, false)
)
.ToList();
embedBuilder.SetFields(fields);
string channelMention;
if (!channelCache.TryGet(msg.ChannelID, out var channel))
channelMention = $"<#{msg.ChannelID}>";
else if (channel.Type is ChannelType.AnnouncementThread or ChannelType.PrivateThread
or ChannelType.PublicThread)
else if (
channel.Type
is ChannelType.AnnouncementThread
or ChannelType.PrivateThread
or ChannelType.PublicThread
)
channelMention =
$"<#{channel.ParentID.Value}>\nID: {channel.ParentID.Value}\n\nThread: {channel.Name} (<#{channel.ID}>)";
else channelMention = $"<#{channel.ID}>\nID: {channel.ID}";
else
channelMention = $"<#{channel.ID}>\nID: {channel.ID}";
embedBuilder.AddField("Channel", channelMention, true);
embedBuilder.AddField("Sender", $"<@{user.ID}>\n{user.Tag()}\nID: {user.ID}", true);
@ -103,7 +126,10 @@ public class MessageUpdateResponder(
embedBuilder.AddField("Member ID", oldMessage.Member, true);
}
embedBuilder.AddField("Link", $"https://discord.com/channels/{msg.GuildID}/{msg.ChannelID}/{msg.ID}");
embedBuilder.AddField(
"Link",
$"https://discord.com/channels/{msg.GuildID}/{msg.ChannelID}/{msg.ID}"
);
webhookExecutor.QueueLog(logChannel.Value, embedBuilder.Build().GetOrThrow());
return Result.Success;
@ -113,39 +139,91 @@ public class MessageUpdateResponder(
// 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)
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
)
{
if (!await messageRepository.UpdateMessageAsync(msg, ct) && msg.ApplicationID.Is(DiscordUtils.PkUserId))
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);
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);
await messageRepository.SetProxiedMessageDataAsync(
msg.ID.Value,
pkMsg.Original,
pkMsg.Sender,
pkMsg.System?.Id,
pkMsg.Member?.Id
);
}
}
}
}
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);
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
);
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));
}
}
}