Skip to content

Commit

Permalink
Add debug logging to AutoMod event handler & msg handler
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Aug 21, 2024
1 parent e57145b commit 974918e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Events/AutoModEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static async Task AutoModerationRuleExecuted(DiscordClient client, AutoMo
{
// AutoMod blocked a message. Pass it to the message handler to run it through some filters anyway.

Program.discord.Logger.LogDebug("Got an AutoMod Message Block event in channel {channelId} by user {userId}", e.Rule.ChannelId, e.Rule.UserId);

var author = await client.GetUserAsync(e.Rule.UserId);
var channel = await client.GetChannelAsync(e.Rule.ChannelId!.Value);

Expand Down
91 changes: 81 additions & 10 deletions Events/MessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe

if (message.Author is null || message.Author.Id == client.CurrentUser.Id)
return;

if (wasAutoModBlock)
Program.discord.Logger.LogDebug("Processing AutoMod-blocked message in {channelId} by user {userId}", message.Channel.Id, message.Author.Id);
else
Program.discord.Logger.LogDebug("Processing message {messageId} in {channelId} by user {userId}", message.Id, message.Channel.Id, message.Author.Id);

if (!limitFilters)
{
Expand Down Expand Up @@ -282,8 +287,16 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe

if (message.MentionedUsers is not null && message.MentionedUsers.Count > Program.cfgjson.MassMentionBanThreshold)
{
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass-mention filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass-mention filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

_ = channel.Guild.BanMemberAsync(message.Author, TimeSpan.FromDays(7), $"Mentioned more than {Program.cfgjson.MassMentionBanThreshold} users in one message.");
string content = $"{Program.cfgjson.Emoji.Banned} {message.Author.Mention} was automatically banned for mentioning **{message.MentionedUsers.Count}** users.";
var chatMsg = await channel.SendMessageAsync(content);
Expand All @@ -306,6 +319,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(message.Content.ToLower(), listItem);
if (success)
{
if (wasAutoModBlock)
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered word list filter", message.Channel.Id, message.Author.Id);
else
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered word list filter", message.Id, message.Channel.Id, message.Author.Id);

string reason = listItem.Reason;
try
{
Expand Down Expand Up @@ -350,9 +368,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
checkedMessage.Contains("invite.gg/")
)
{
string reason = "Sent an unapproved invite";
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered unapproved invite filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered unapproved invite filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

string reason = "Sent an unapproved invite";
try
{
_ = InvestigationsHelpers.SendInfringingMessaageAsync("mod", message, reason, null, wasAutoModBlock: wasAutoModBlock);
Expand Down Expand Up @@ -507,9 +533,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
var matches = emoji_rx.Matches(input);
if (matches.Count > Program.cfgjson.MassEmojiThreshold)
{
string reason = "Mass emoji";
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass emoji filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass emoji filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

string reason = "Mass emoji";

if (GetPermLevel(member) == ServerPermLevel.Nothing && !Program.db.HashExists("emojiPardoned", message.Author.Id.ToString()))
{
Expand Down Expand Up @@ -599,8 +633,16 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
{
if (phishingMatch)
{
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered phishing message filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered phishing message filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

string reason = "Sending phishing URL(s)";
DiscordMessage msg = await message.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**");
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, message.Channel, " automatically ");
Expand All @@ -617,9 +659,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
// attempted to ping @everyone/@here
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")))
{
string reason = "Attempted to ping everyone/here";
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered everyone/here mention filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered everyone/here mention filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

string reason = "Attempted to ping everyone/here";
DiscordMessage msg = await message.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**");
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, message.Channel, " automatically ");
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, wasAutoModBlock: wasAutoModBlock);
Expand All @@ -629,6 +679,15 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
// Mass mentions
if (message.MentionedUsers is not null && message.MentionedUsers.Count >= Program.cfgjson.MassMentionThreshold && GetPermLevel(member) < ServerPermLevel.Tier3)
{
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered mass mention filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered mass mention filter", message.Id, message.Channel.Id, message.Author.Id);
}

string reason = "Mass mentions";
try
{
Expand All @@ -655,9 +714,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
&& (lineCount >= Program.cfgjson.IncreasedLineLimit
|| (lineCount >= Program.cfgjson.LineLimit && GetPermLevel(member) < (ServerPermLevel)Program.cfgjson.LineLimitTier)))
{
string reason = "Too many lines in a single message";
if (!wasAutoModBlock)
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("AutoMod-blocked message in {channelId} by user {userId} triggered line limit filter", message.Channel.Id, message.Author.Id);
}
else
{
Program.discord.Logger.LogDebug("Message {messageId} in {channelId} by user {userId} triggered line limit filter", message.Id, message.Channel.Id, message.Author.Id);
_ = message.DeleteAsync();
}

string reason = "Too many lines in a single message";

if (!Program.db.HashExists("linePardoned", message.Author.Id.ToString()))
{
Expand Down Expand Up @@ -826,6 +893,10 @@ await InvestigationsHelpers.SendInfringingMessaageAsync(
}
}

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

public static int CountNewlines(string input)
Expand Down

0 comments on commit 974918e

Please sign in to comment.