Skip to content

Commit add4e9d

Browse files
committed
MessageEvent: add regions to segment the code
Updates #267
1 parent af239c7 commit add4e9d

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

Events/MessageEvent.cs

+51-10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, DiscordMessag
116116
}
117117
public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMessage message, DiscordChannel channel, bool isAnEdit = false, bool limitFilters = false, bool wasAutoModBlock = false)
118118
{
119+
#region combine all message text
119120
// Get forwarded msg & embeds, if any, and combine with content to evaluate
120121
// Combined as a single long string
121122

@@ -160,9 +161,11 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
160161
msgContentWithEmbedData += $" {field.Name} {field.Value}";
161162
}
162163
}
164+
#endregion
163165

164166
try
165167
{
168+
#region return early checks
166169
if (message.Timestamp is not null && message.Timestamp.Value.Year < (DateTime.Now.Year - 2))
167170
return;
168171

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

172175
if (message.Author is null || message.Author.Id == client.CurrentUser.Id)
173176
return;
177+
#endregion
174178

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

186192
if (!limitFilters)
187193
{
194+
#region tracked user relaying
188195
if (Program.db.SetContains("trackedUsers", message.Author.Id))
189196
{
190197
// Check current channel against tracking channels
@@ -206,13 +213,17 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
206213
await RelayTrackedMessageAsync(client, message);
207214
}
208215
}
216+
#endregion
209217

218+
#region DM relaying
210219
if (!isAnEdit && channel.IsPrivate && Program.cfgjson.LogChannels.ContainsKey("dms"))
211220
{
212221
DirectMessageEvent.DirectMessageEventHandler(message.BaseMessage);
213222
return;
214223
}
224+
#endregion
215225

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

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

288301
await message.BaseMessage.CreateThreadAsync(giveawayTitle, DiscordAutoArchiveDuration.ThreeDays, "Automatically creating giveaway thread.");
289302
}
303+
#endregion
290304
}
291305

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

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

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

349+
#region retrieve member object
331350
DiscordMember member;
332351
try
333352
{
@@ -340,10 +359,12 @@ Program.cfgjson.GitListDirectory is not null
340359

341360
if (member == default)
342361
return;
362+
#endregion
343363

344-
// Skip messages from moderators beyond this point.
364+
#region content filters
345365
if ((await GetPermLevelAsync(member)) < ServerPermLevel.TrialModerator)
346366
{
367+
#region block messages in forum intro thread
347368
if (!limitFilters)
348369
{
349370
if ((channel.Id == Program.cfgjson.SupportForumIntroThreadId ||
@@ -357,7 +378,9 @@ Program.cfgjson.GitListDirectory is not null
357378
return;
358379
}
359380
}
381+
#endregion
360382

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

406+
407+
#region restricted word list filters
382408
bool match = false;
383409

384410
// Matching word list
@@ -431,7 +457,9 @@ Program.cfgjson.GitListDirectory is not null
431457
if (match)
432458
return;
433459

434-
// Unapproved invites
460+
#endregion
461+
462+
#region invite filter
435463
string checkedMessage = msgContentWithEmbedData.Replace('\\', '/');
436464

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

574-
// Mass emoji
602+
#endregion
603+
604+
#region mass emoji filter
575605
if (!Program.cfgjson.UnrestrictedEmojiChannels.Contains(channel.Id) && msgContentWithEmbedData.Length >= Program.cfgjson.MassEmojiThreshold)
576606
{
577607
char[] tempArray = msgContentWithEmbedData.Replace("🏻", "").Replace("🏼", "").Replace("🏽", "").Replace("🏾", "").Replace("🏿", "").ToCharArray();
@@ -635,7 +665,9 @@ Program.cfgjson.GitListDirectory is not null
635665
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, messageContentOverride: msgContentWithEmbedData, wasAutoModBlock: wasAutoModBlock);
636666
return;
637667
}
668+
#endregion
638669

670+
#region tech support relaying
639671
if (!limitFilters)
640672
{
641673
if (channel.Id == Program.cfgjson.TechSupportChannel &&
@@ -683,9 +715,10 @@ Program.cfgjson.GitListDirectory is not null
683715
_ = logOut.CreateReactionAsync(DiscordEmoji.FromName(client, ":CliptokAcknowledge:", true));
684716
}
685717
}
718+
#endregion
686719
}
687720

688-
// phishing API
721+
#region phishing API
689722
var urlMatches = url_rx.Matches(msgContentWithEmbedData);
690723
if (urlMatches.Count > 0 && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") is not null && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") != "useyourimagination")
691724
{
@@ -716,8 +749,9 @@ Program.cfgjson.GitListDirectory is not null
716749
}
717750
}
718751
}
752+
#endregion
719753

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

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

770-
// line limit
806+
#region line limit filter
771807
var lineCount = CountNewlines(msgContentWithEmbedData);
772808

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

840876
}
877+
#endregion
841878
}
879+
#endregion
842880

843881
if (!limitFilters)
844882
{
845-
// feedback hub forum
883+
#region feedback hub forum validation
846884
if ((await GetPermLevelAsync(member)) < ServerPermLevel.TrialModerator && !isAnEdit && channel.IsThread && channel.ParentId == Program.cfgjson.FeedbackHubForum && !Program.db.SetContains("processedFeedbackHubThreads", channel.Id))
847885
{
848886
var thread = (DiscordThreadChannel)channel;
@@ -884,7 +922,9 @@ await LogChannelHelper.LogMessageAsync("messages",
884922
}
885923
}
886924
}
925+
#endregion
887926

927+
#region passive list matching
888928
// Check the passive lists AFTER all other checks.
889929
if ((await GetPermLevelAsync(member)) >= ServerPermLevel.TrialModerator)
890930
return;
@@ -924,6 +964,7 @@ await InvestigationsHelpers.SendInfringingMessaageAsync(
924964
}
925965
}
926966
}
967+
#endregion
927968
}
928969
}
929970
catch (Exception e)

0 commit comments

Comments
 (0)