Skip to content

Commit 4c44be4

Browse files
Add forwarded message support
1 parent 695d2c3 commit 4c44be4

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

Events/MessageEvent.cs

+54-8
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,52 @@ 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+
// Get forwarded msg & embeds, if any, and combine with content to evaluate
120+
// Combined as a single long string
121+
122+
string msgContentWithEmbedData = message.Content;
123+
var embeds = new List<DiscordEmbed>();
124+
embeds.AddRange(message.Embeds);
125+
126+
if (message.MessageSnapshots is not null)
127+
foreach (var snapshot in message.MessageSnapshots)
128+
{
129+
msgContentWithEmbedData += $" {snapshot.Message.Content}";
130+
embeds.AddRange(snapshot.Message.Embeds);
131+
}
132+
133+
foreach (var embed in embeds)
134+
{
135+
// Add any text from the embed into the content to be checked
136+
137+
if (embed.Author is not null)
138+
{
139+
if (embed.Author.Name is not null)
140+
msgContentWithEmbedData += $" {embed.Author.Name}";
141+
142+
if (embed.Author.Url is not null)
143+
msgContentWithEmbedData += $" {embed.Author.Url}";
144+
}
145+
146+
if (embed.Title is not null)
147+
msgContentWithEmbedData += $" {embed.Title}";
148+
149+
if (embed.Url is not null)
150+
msgContentWithEmbedData += $" {embed.Url}";
151+
152+
if (embed.Description is not null)
153+
msgContentWithEmbedData += $" {embed.Description}";
154+
155+
if (embed.Footer is not null && embed.Footer.Text is not null)
156+
msgContentWithEmbedData += $" {embed.Footer.Text}";
157+
158+
if (embed.Fields is not null)
159+
foreach (var field in embed.Fields)
160+
{
161+
msgContentWithEmbedData += $" {field.Name} {field.Value}";
162+
}
163+
}
164+
119165
try
120166
{
121167
if (message.Timestamp is not null && message.Timestamp.Value.Year < (DateTime.Now.Year - 2))
@@ -317,7 +363,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
317363
}
318364
else
319365
{
320-
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(message.Content.ToLower(), listItem);
366+
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(msgContentWithEmbedData.ToLower(), listItem);
321367
if (success)
322368
{
323369
if (wasAutoModBlock)
@@ -359,7 +405,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
359405
return;
360406

361407
// Unapproved invites
362-
string checkedMessage = message.Content.Replace('\\', '/');
408+
string checkedMessage = msgContentWithEmbedData.Replace('\\', '/');
363409

364410
if ((await GetPermLevelAsync(member)) < (ServerPermLevel)Program.cfgjson.InviteTierRequirement && checkedMessage.Contains("dsc.gg/") ||
365411
checkedMessage.Contains("invite.gg/")
@@ -499,9 +545,9 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
499545
return;
500546

501547
// Mass emoji
502-
if (!Program.cfgjson.UnrestrictedEmojiChannels.Contains(channel.Id) && message.Content.Length >= Program.cfgjson.MassEmojiThreshold)
548+
if (!Program.cfgjson.UnrestrictedEmojiChannels.Contains(channel.Id) && msgContentWithEmbedData.Length >= Program.cfgjson.MassEmojiThreshold)
503549
{
504-
char[] tempArray = message.Content.Replace("🏻", "").Replace("🏼", "").Replace("🏽", "").Replace("🏾", "").Replace("🏿", "").ToCharArray();
550+
char[] tempArray = msgContentWithEmbedData.Replace("🏻", "").Replace("🏼", "").Replace("🏽", "").Replace("🏾", "").Replace("🏿", "").ToCharArray();
505551
int pos = 0;
506552
foreach (char c in tempArray)
507553
{
@@ -613,10 +659,10 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
613659
}
614660

615661
// phishing API
616-
var urlMatches = url_rx.Matches(message.Content);
662+
var urlMatches = url_rx.Matches(msgContentWithEmbedData);
617663
if (urlMatches.Count > 0 && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") is not null && Environment.GetEnvironmentVariable("CLIPTOK_ANTIPHISHING_ENDPOINT") != "useyourimagination")
618664
{
619-
var (phishingMatch, httpStatus, responseText, phishingResponse) = await APIs.PhishingAPI.PhishingAPICheckAsync(message.Content);
665+
var (phishingMatch, httpStatus, responseText, phishingResponse) = await APIs.PhishingAPI.PhishingAPICheckAsync(msgContentWithEmbedData);
620666

621667
if (httpStatus == HttpStatusCode.OK)
622668
{
@@ -645,7 +691,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
645691
}
646692

647693
// attempted to ping @everyone/@here
648-
var msgContent = message.Content;
694+
var msgContent = msgContentWithEmbedData;
649695
foreach (var letter in Checks.ListChecks.lookalikeAlphabetMap)
650696
msgContent = msgContent.Replace(letter.Key, letter.Value);
651697
if (Program.cfgjson.EveryoneFilter && !member.Roles.Any(role => Program.cfgjson.EveryoneExcludedRoles.Contains(role.Id)) && !Program.cfgjson.EveryoneExcludedChannels.Contains(channel.Id) && (msgContent.Contains("@everyone") || msgContent.Contains("@here")))
@@ -824,7 +870,7 @@ await LogChannelHelper.LogMessageAsync("messages",
824870
}
825871
else
826872
{
827-
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(message.Content.ToLower(), listItem);
873+
(bool success, string flaggedWord) = Checks.ListChecks.CheckForNaughtyWords(msgContentWithEmbedData.ToLower(), listItem);
828874
if (success)
829875
{
830876
DiscordChannel logChannel = default;

Events/MockDiscordMessage.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ public MockDiscordMessage(DiscordMessage baseMessage)
2020
JumpLink = baseMessage.JumpLink;
2121
MentionedUsers = baseMessage.MentionedUsers;
2222
MentionedUsersCount = baseMessage.MentionedUsers.Count;
23+
MessageSnapshots = baseMessage.MessageSnapshots;
2324
Reactions = baseMessage.Reactions;
2425
ReferencedMessage = baseMessage.ReferencedMessage;
2526
Stickers = baseMessage.Stickers;
2627
Timestamp = baseMessage.Timestamp;
2728
}
2829

29-
public MockDiscordMessage(IReadOnlyList<DiscordAttachment> attachments = default, DiscordUser author = default, DiscordChannel channel = default, ulong channelId = default, string content = default, IReadOnlyList<DiscordEmbed> embeds = default, ulong id = default, Uri jumpLink = default, IReadOnlyList<DiscordUser> mentionedUsers = default, int mentionedUsersCount = default, IReadOnlyList<DiscordReaction> reactions = default, DiscordMessage referencedMessage = default, IReadOnlyList<DiscordMessageSticker> stickers = default, DateTimeOffset? timestamp = default)
30+
public MockDiscordMessage(IReadOnlyList<DiscordAttachment> attachments = default, DiscordUser author = default, DiscordChannel channel = default, ulong channelId = default, string content = default, IReadOnlyList<DiscordEmbed> embeds = default, ulong id = default, Uri jumpLink = default, IReadOnlyList<DiscordUser> mentionedUsers = default, int mentionedUsersCount = default, IReadOnlyList<DiscordMessageSnapshot> messageSnapshots = default, IReadOnlyList<DiscordReaction> reactions = default, DiscordMessage referencedMessage = default, IReadOnlyList<DiscordMessageSticker> stickers = default, DateTimeOffset? timestamp = default)
3031
{
3132
Attachments = attachments;
3233
Author = author;
@@ -38,6 +39,7 @@ public MockDiscordMessage(IReadOnlyList<DiscordAttachment> attachments = default
3839
JumpLink = jumpLink;
3940
MentionedUsers = mentionedUsers;
4041
MentionedUsersCount = mentionedUsersCount;
42+
MessageSnapshots = messageSnapshots;
4143
Reactions = reactions;
4244
ReferencedMessage = referencedMessage;
4345
Stickers = stickers;
@@ -55,6 +57,7 @@ public MockDiscordMessage(IReadOnlyList<DiscordAttachment> attachments = default
5557
public Uri JumpLink { get; set; }
5658
public IReadOnlyList<DiscordUser> MentionedUsers { get; }
5759
public int MentionedUsersCount { get; }
60+
public IReadOnlyList<DiscordMessageSnapshot> MessageSnapshots { get; }
5861
public IReadOnlyList<DiscordReaction> Reactions { get; set; }
5962
public DiscordMessage ReferencedMessage { get; set; }
6063
public IReadOnlyList<DiscordMessageSticker> Stickers { get; set; }

0 commit comments

Comments
 (0)