Skip to content

Commit 974918e

Browse files
Add debug logging to AutoMod event handler & msg handler
1 parent e57145b commit 974918e

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

Events/AutoModEvents.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public static async Task AutoModerationRuleExecuted(DiscordClient client, AutoMo
88
{
99
// AutoMod blocked a message. Pass it to the message handler to run it through some filters anyway.
1010

11+
Program.discord.Logger.LogDebug("Got an AutoMod Message Block event in channel {channelId} by user {userId}", e.Rule.ChannelId, e.Rule.UserId);
12+
1113
var author = await client.GetUserAsync(e.Rule.UserId);
1214
var channel = await client.GetChannelAsync(e.Rule.ChannelId!.Value);
1315

Events/MessageEvent.cs

+81-10
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
137137

138138
if (message.Author is null || message.Author.Id == client.CurrentUser.Id)
139139
return;
140+
141+
if (wasAutoModBlock)
142+
Program.discord.Logger.LogDebug("Processing AutoMod-blocked message in {channelId} by user {userId}", message.Channel.Id, message.Author.Id);
143+
else
144+
Program.discord.Logger.LogDebug("Processing message {messageId} in {channelId} by user {userId}", message.Id, message.Channel.Id, message.Author.Id);
140145

141146
if (!limitFilters)
142147
{
@@ -282,8 +287,16 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
282287

283288
if (message.MentionedUsers is not null && message.MentionedUsers.Count > Program.cfgjson.MassMentionBanThreshold)
284289
{
285-
if (!wasAutoModBlock)
290+
if (wasAutoModBlock)
291+
{
292+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass-mention filter", message.Channel.Id, message.Author.Id);
293+
}
294+
else
295+
{
296+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass-mention filter", message.Id, message.Channel.Id, message.Author.Id);
286297
_ = message.DeleteAsync();
298+
}
299+
287300
_ = channel.Guild.BanMemberAsync(message.Author, TimeSpan.FromDays(7), $"Mentioned more than {Program.cfgjson.MassMentionBanThreshold} users in one message.");
288301
string content = $"{Program.cfgjson.Emoji.Banned} {message.Author.Mention} was automatically banned for mentioning **{message.MentionedUsers.Count}** users.";
289302
var chatMsg = await channel.SendMessageAsync(content);
@@ -306,6 +319,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
306319
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(message.Content.ToLower(), listItem);
307320
if (success)
308321
{
322+
if (wasAutoModBlock)
323+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered word list filter", message.Channel.Id, message.Author.Id);
324+
else
325+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered word list filter", message.Id, message.Channel.Id, message.Author.Id);
326+
309327
string reason = listItem.Reason;
310328
try
311329
{
@@ -350,9 +368,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
350368
checkedMessage.Contains("invite.gg/")
351369
)
352370
{
353-
string reason = "Sent an unapproved invite";
354-
if (!wasAutoModBlock)
371+
if (wasAutoModBlock)
372+
{
373+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered unapproved invite filter", message.Channel.Id, message.Author.Id);
374+
}
375+
else
376+
{
377+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered unapproved invite filter", message.Id, message.Channel.Id, message.Author.Id);
355378
_ = message.DeleteAsync();
379+
}
380+
381+
string reason = "Sent an unapproved invite";
356382
try
357383
{
358384
_ = InvestigationsHelpers.SendInfringingMessaageAsync("mod", message, reason, null, wasAutoModBlock: wasAutoModBlock);
@@ -507,9 +533,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
507533
var matches = emoji_rx.Matches(input);
508534
if (matches.Count > Program.cfgjson.MassEmojiThreshold)
509535
{
510-
string reason = "Mass emoji";
511-
if (!wasAutoModBlock)
536+
if (wasAutoModBlock)
537+
{
538+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass emoji filter", message.Channel.Id, message.Author.Id);
539+
}
540+
else
541+
{
542+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass emoji filter", message.Id, message.Channel.Id, message.Author.Id);
512543
_ = message.DeleteAsync();
544+
}
545+
546+
string reason = "Mass emoji";
513547

514548
if (GetPermLevel(member) == ServerPermLevel.Nothing && !Program.db.HashExists("emojiPardoned", message.Author.Id.ToString()))
515549
{
@@ -599,8 +633,16 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
599633
{
600634
if (phishingMatch)
601635
{
602-
if (!wasAutoModBlock)
636+
if (wasAutoModBlock)
637+
{
638+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered phishing message filter", message.Channel.Id, message.Author.Id);
639+
}
640+
else
641+
{
642+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered phishing message filter", message.Id, message.Channel.Id, message.Author.Id);
603643
_ = message.DeleteAsync();
644+
}
645+
604646
string reason = "Sending phishing URL(s)";
605647
DiscordMessage msg = await message.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**");
606648
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, message.Channel, " automatically ");
@@ -617,9 +659,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
617659
// attempted to ping @everyone/@here
618660
if (Program.cfgjson.EveryoneFilter && !member.Roles.Any(role => Program.cfgjson.EveryoneExcludedRoles.Contains(role.Id)) && !Program.cfgjson.EveryoneExcludedChannels.Contains(message.Channel.Id) && (message.Content.Contains("@everyone") || message.Content.Contains("@here")))
619661
{
620-
string reason = "Attempted to ping everyone/here";
621-
if (!wasAutoModBlock)
662+
if (wasAutoModBlock)
663+
{
664+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered everyone/here mention filter", message.Channel.Id, message.Author.Id);
665+
}
666+
else
667+
{
668+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered everyone/here mention filter", message.Id, message.Channel.Id, message.Author.Id);
622669
_ = message.DeleteAsync();
670+
}
671+
672+
string reason = "Attempted to ping everyone/here";
623673
DiscordMessage msg = await message.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**");
624674
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, message.Channel, " automatically ");
625675
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, wasAutoModBlock: wasAutoModBlock);
@@ -629,6 +679,15 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
629679
// Mass mentions
630680
if (message.MentionedUsers is not null && message.MentionedUsers.Count >= Program.cfgjson.MassMentionThreshold && GetPermLevel(member) < ServerPermLevel.Tier3)
631681
{
682+
if (wasAutoModBlock)
683+
{
684+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass mention filter", message.Channel.Id, message.Author.Id);
685+
}
686+
else
687+
{
688+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass mention filter", message.Id, message.Channel.Id, message.Author.Id);
689+
}
690+
632691
string reason = "Mass mentions";
633692
try
634693
{
@@ -655,9 +714,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
655714
&& (lineCount >= Program.cfgjson.IncreasedLineLimit
656715
|| (lineCount >= Program.cfgjson.LineLimit && GetPermLevel(member) < (ServerPermLevel)Program.cfgjson.LineLimitTier)))
657716
{
658-
string reason = "Too many lines in a single message";
659-
if (!wasAutoModBlock)
717+
if (wasAutoModBlock)
718+
{
719+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered line limit filter", message.Channel.Id, message.Author.Id);
720+
}
721+
else
722+
{
723+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered line limit filter", message.Id, message.Channel.Id, message.Author.Id);
660724
_ = message.DeleteAsync();
725+
}
726+
727+
string reason = "Too many lines in a single message";
661728

662729
if (!Program.db.HashExists("linePardoned", message.Author.Id.ToString()))
663730
{
@@ -826,6 +893,10 @@ await InvestigationsHelpers.SendInfringingMessaageAsync(
826893
}
827894
}
828895

896+
if (wasAutoModBlock)
897+
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered no filters!", message.Channel.Id, message.Author.Id);
898+
else
899+
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered no filters!", message.Id, message.Channel.Id, message.Author.Id);
829900
}
830901

831902
public static int CountNewlines(string input)

0 commit comments

Comments
 (0)