Skip to content

Commit

Permalink
MessageEvent: add regions to segment the code
Browse files Browse the repository at this point in the history
Updates #267
  • Loading branch information
Erisa committed Feb 20, 2025
1 parent af239c7 commit add4e9d
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions Events/MessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, DiscordMessag
}
public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMessage message, DiscordChannel channel, bool isAnEdit = false, bool limitFilters = false, bool wasAutoModBlock = false)
{
#region combine all message text
// Get forwarded msg & embeds, if any, and combine with content to evaluate
// Combined as a single long string

Expand Down Expand Up @@ -160,9 +161,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
msgContentWithEmbedData += $" {field.Name} {field.Value}";
}
}
#endregion

try
{
#region return early checks
if (message.Timestamp is not null && message.Timestamp.Value.Year < (DateTime.Now.Year - 2))
return;

Expand All @@ -171,7 +174,9 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe

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

#region debug logging
if (wasAutoModBlock)
{
Program.discord.Logger.LogDebug("Processing AutoMod-blocked message in {channelId} by user {userId}", channel.Id, message.Author.Id);
Expand All @@ -182,9 +187,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
{
Program.discord.Logger.LogDebug("Processing message {messageId} in {channelId} by user {userId}", message.Id, channel.Id, message.Author.Id);
}
#endregion

if (!limitFilters)
{
#region tracked user relaying
if (Program.db.SetContains("trackedUsers", message.Author.Id))
{
// Check current channel against tracking channels
Expand All @@ -206,13 +213,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
await RelayTrackedMessageAsync(client, message);
}
}
#endregion

#region DM relaying
if (!isAnEdit && channel.IsPrivate && Program.cfgjson.LogChannels.ContainsKey("dms"))
{
DirectMessageEvent.DirectMessageEventHandler(message.BaseMessage);
return;
}
#endregion

#region modmail thread handling
if (!isAnEdit && message.Author.Id == Program.cfgjson.ModmailUserId && message.Content == "@here" && message.Embeds[0].Footer.Text.Contains("User ID:"))
{
Program.discord.Logger.LogDebug(Program.CliptokEventID, "Processing modmail message {message} in {channel}", message.Id, channel);
Expand Down Expand Up @@ -274,7 +285,9 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
await LogChannelHelper.LogMessageAsync("mod", $"{Program.cfgjson.Emoji.Deleted} Note `{note.Value.NoteId}` was automatically deleted after modmail thread creation (belonging to {modmailMember.Mention})", embed);
}
}
#endregion

#region giveaways handling
// handle #giveaways
if (!isAnEdit && message.Author.Id == Program.cfgjson.GiveawayBot && channel.Id == Program.cfgjson.GiveawaysChannel && message.Content == Program.cfgjson.GiveawayTriggerMessage)
{
Expand All @@ -287,9 +300,10 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe

await message.BaseMessage.CreateThreadAsync(giveawayTitle, DiscordAutoArchiveDuration.ThreeDays, "Automatically creating giveaway thread.");
}
#endregion
}

// automatic listupdate for private lists
#region automatic listupdate for private lists
if (
Program.cfgjson.GitListDirectory is not null
&& Program.cfgjson.GitListDirectory != ""
Expand All @@ -316,18 +330,23 @@ Program.cfgjson.GitListDirectory is not null
await msg.ModifyAsync($"{Program.cfgjson.Emoji.Success} Successfully updated and reloaded private lists!\n```\n{result}\n```");
}
}
#endregion

// Skip DMs, external guilds, and messages from bots, beyond this point.
#region Skip DMs, external guilds, and messages from bots, beyond this point.
if (channel.IsPrivate || channel.Guild.Id != Program.cfgjson.ServerID || message.Author.IsBot)
return;
#endregion

#region mention relaying
if (!limitFilters && !Program.cfgjson.MentionTrackExcludedChannels.Contains(channel.Id) && (channel.ParentId is null || !Program.cfgjson.MentionTrackExcludedChannels.Contains((ulong)channel.ParentId)))
{
// track mentions
if (message.MentionedUsers.Any(x => x.Id == Program.discord.CurrentUser.Id))
await LogChannelHelper.LogMessageAsync("mentions", await DiscordHelpers.GenerateMessageRelay(message.BaseMessage, true, true, false));
}
#endregion

#region retrieve member object
DiscordMember member;
try
{
Expand All @@ -340,10 +359,12 @@ Program.cfgjson.GitListDirectory is not null

if (member == default)
return;
#endregion

// Skip messages from moderators beyond this point.
#region content filters
if ((await GetPermLevelAsync(member)) < ServerPermLevel.TrialModerator)
{
#region block messages in forum intro thread
if (!limitFilters)
{
if ((channel.Id == Program.cfgjson.SupportForumIntroThreadId ||
Expand All @@ -357,7 +378,9 @@ Program.cfgjson.GitListDirectory is not null
return;
}
}
#endregion

#region mass mentions ban filter
if ((message.MentionedUsers is not null && message.MentionedUsers.Count > Program.cfgjson.MassMentionBanThreshold) || (message.MentionedUsersCount > Program.cfgjson.MassMentionBanThreshold))
{
if (wasAutoModBlock)
Expand All @@ -378,7 +401,10 @@ Program.cfgjson.GitListDirectory is not null
_ = InvestigationsHelpers.SendInfringingMessaageAsync("mod", message, "Mass mentions (Ban threshold)", DiscordHelpers.MessageLink(chatMsg), content: content, messageContentOverride: msgContentWithEmbedData, wasAutoModBlock: wasAutoModBlock);
return;
}
#endregion


#region restricted word list filters
bool match = false;

// Matching word list
Expand Down Expand Up @@ -431,7 +457,9 @@ Program.cfgjson.GitListDirectory is not null
if (match)
return;

// Unapproved invites
#endregion

#region invite filter
string checkedMessage = msgContentWithEmbedData.Replace('\\', '/');

if ((await GetPermLevelAsync(member)) < (ServerPermLevel)Program.cfgjson.InviteTierRequirement && checkedMessage.Contains("dsc.gg/") ||
Expand Down Expand Up @@ -571,7 +599,9 @@ Program.cfgjson.GitListDirectory is not null
if (match)
return;

// Mass emoji
#endregion

#region mass emoji filter
if (!Program.cfgjson.UnrestrictedEmojiChannels.Contains(channel.Id) && msgContentWithEmbedData.Length >= Program.cfgjson.MassEmojiThreshold)
{
char[] tempArray = msgContentWithEmbedData.Replace("🏻", "").Replace("🏼", "").Replace("🏽", "").Replace("🏾", "").Replace("🏿", "").ToCharArray();
Expand Down Expand Up @@ -635,7 +665,9 @@ Program.cfgjson.GitListDirectory is not null
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, messageContentOverride: msgContentWithEmbedData, wasAutoModBlock: wasAutoModBlock);
return;
}
#endregion

#region tech support relaying
if (!limitFilters)
{
if (channel.Id == Program.cfgjson.TechSupportChannel &&
Expand Down Expand Up @@ -683,9 +715,10 @@ Program.cfgjson.GitListDirectory is not null
_ = logOut.CreateReactionAsync(DiscordEmoji.FromName(client, ":CliptokAcknowledge:", true));
}
}
#endregion
}

// phishing API
#region phishing API
var urlMatches = url_rx.Matches(msgContentWithEmbedData);
if (urlMatches.Count > 0 && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") is not null && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") != "useyourimagination")
{
Expand Down Expand Up @@ -716,8 +749,9 @@ Program.cfgjson.GitListDirectory is not null
}
}
}
#endregion

// attempted to ping @everyone/@here
#region everyone/here ping filter
var msgContent = msgContentWithEmbedData;
foreach (var letter in Checks.ListChecks.lookalikeAlphabetMap)
msgContent = msgContent.Replace(letter.Key, letter.Value);
Expand All @@ -738,8 +772,9 @@ Program.cfgjson.GitListDirectory is not null
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, messageContentOverride: msgContentWithEmbedData, wasAutoModBlock: wasAutoModBlock);
return;
}
#endregion

// Mass mentions
#region mass mentions warn filter
if (((message.MentionedUsers is not null && message.MentionedUsers.Count >= Program.cfgjson.MassMentionThreshold) || (message.MentionedUsersCount >= Program.cfgjson.MassMentionThreshold)) && (await GetPermLevelAsync(member)) < ServerPermLevel.Tier3)
{
if (wasAutoModBlock)
Expand All @@ -766,8 +801,9 @@ Program.cfgjson.GitListDirectory is not null
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, messageContentOverride: msgContentWithEmbedData, wasAutoModBlock: wasAutoModBlock);
return;
}
#endregion

// line limit
#region line limit filter
var lineCount = CountNewlines(msgContentWithEmbedData);

if (!Program.cfgjson.LineLimitExcludedChannels.Contains(channel.Id)
Expand Down Expand Up @@ -838,11 +874,13 @@ Program.cfgjson.GitListDirectory is not null
}

}
#endregion
}
#endregion

if (!limitFilters)
{
// feedback hub forum
#region feedback hub forum validation
if ((await GetPermLevelAsync(member)) < ServerPermLevel.TrialModerator && !isAnEdit && channel.IsThread && channel.ParentId == Program.cfgjson.FeedbackHubForum && !Program.db.SetContains("processedFeedbackHubThreads", channel.Id))
{
var thread = (DiscordThreadChannel)channel;
Expand Down Expand Up @@ -884,7 +922,9 @@ await LogChannelHelper.LogMessageAsync("messages",
}
}
}
#endregion

#region passive list matching
// Check the passive lists AFTER all other checks.
if ((await GetPermLevelAsync(member)) >= ServerPermLevel.TrialModerator)
return;
Expand Down Expand Up @@ -924,6 +964,7 @@ await InvestigationsHelpers.SendInfringingMessaageAsync(
}
}
}
#endregion
}
}
catch (Exception e)
Expand Down

0 comments on commit add4e9d

Please sign in to comment.