Skip to content

Commit 17238b7

Browse files
committed
feat(on_message): audit messages
- Provide advise for certain message content - When messages contain links to X, advise to use xcancel for better previews
1 parent 9b31adc commit 17238b7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/events/on_message/_advise.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ChannelType, Message } from 'discord.js';
2+
import { has_link } from './_common.js';
3+
import urlRegex from 'url-regex';
4+
5+
const replacementMap = [
6+
{ test: 'https://x.com', replace: 'https://xcancel.com' },
7+
];
8+
9+
export default async function mutate_content(message: Message) {
10+
if (
11+
message.channel.type != ChannelType.GuildText ||
12+
(!has_link(message) && !message.content.includes('x.com'))
13+
)
14+
return;
15+
16+
// Get links
17+
const caughtLinks: string[] | undefined = message.content
18+
.match(urlRegex())
19+
?.filter(
20+
(link) =>
21+
replacementMap.findIndex((item) =>
22+
link.startsWith(item.test),
23+
) !== -1,
24+
);
25+
if (!caughtLinks) return;
26+
const hasXLinks: boolean = caughtLinks.some((item) =>
27+
item.startsWith('https://x.com'),
28+
);
29+
if (!hasXLinks) return;
30+
31+
const updatedPhrase: string =
32+
caughtLinks.length > 1
33+
? 'Here are the updated links:'
34+
: 'Here is the updated link:';
35+
const updatedLinks = caughtLinks
36+
.map((link) => {
37+
const replaceIdx = replacementMap.findIndex((item) =>
38+
link.startsWith(item.test),
39+
);
40+
return link.replace(
41+
replacementMap[replaceIdx].test,
42+
replacementMap[replaceIdx].replace,
43+
);
44+
})
45+
.join('\n');
46+
47+
const updatedMessage: string = (function () {
48+
let newContent = message.content;
49+
replacementMap.forEach(
50+
(item) =>
51+
(newContent = newContent.replace(item.test, item.replace)),
52+
);
53+
return newContent;
54+
})();
55+
56+
await message.author.send(
57+
`Re: ${message.url}\n\nI see you've provided a link to \`x.com\`. Please consider posting a new message having \`x.com\` replaced with \`xcancel.com\`, that way server members may view the message and thread without requiring an account.\n\n${updatedPhrase}\`\`\`${updatedLinks}\`\`\`\n\nHere is your entire message with adjusted links:\n\`\`\`${updatedMessage}\`\`\``,
58+
);
59+
}

src/events/on_message/on_message.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import check_links from './_check_links.js';
22
import spam_filter from './_spam_filter.js';
33
import autothread from './_autothread.js';
44
import slow_mode from './_slow_mode.js';
5+
import advise from './_advise.js';
56
import { event } from 'jellycommands';
67
import { STOP } from './_common.js';
78

@@ -16,6 +17,7 @@ export default event({
1617
check_links,
1718
autothread,
1819
slow_mode,
20+
advise,
1921
]) {
2022
try {
2123
await handler(message);

0 commit comments

Comments
 (0)