Skip to content

Commit 7199a2f

Browse files
authored
Merge pull request #56 from theetrain/feat/tweets
feat(on_message): audit messages
2 parents 9b31adc + f6ba448 commit 7199a2f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/events/on_message/_advise.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import urlRegex from 'url-regex';
2+
import {
3+
type Message,
4+
ChannelType,
5+
hideLinkEmbed,
6+
codeBlock,
7+
} from 'discord.js';
8+
9+
export default async function mutate_content(message: Message) {
10+
if (message.channel.type != ChannelType.GuildText) return;
11+
12+
const links = message.content
13+
.match(urlRegex())
14+
?.filter((link) => link.startsWith('https://x.com'))
15+
.map((link) =>
16+
link.replace(/^https:\/\/x\.com/, 'https://xcancel.com'),
17+
);
18+
19+
if (!links || links.length === 0) {
20+
return;
21+
}
22+
23+
const updated_phrase: string =
24+
links.length > 1
25+
? 'Here are the updated links:'
26+
: 'Here is the updated link:';
27+
28+
const updated_link_list = links
29+
.map((link) => `- ${hideLinkEmbed(link)}`)
30+
.join('\n');
31+
32+
const updated_content = message.content.replace(urlRegex(), (match) => {
33+
return match.startsWith('https://x.com')
34+
? match.replace(/^https:\/\/x\.com/, 'https://xcancel.com')
35+
: match;
36+
});
37+
38+
try {
39+
await message.author.send(
40+
`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${updated_phrase}\n${updated_link_list}\n\nHere is your entire message with adjusted links:\n${codeBlock(updated_content)}`,
41+
);
42+
43+
return; // mission complete
44+
} catch {
45+
// assume user disabled DMs upon error
46+
}
47+
48+
// Plan B: inline reply
49+
try {
50+
await message.reply(
51+
`I converted your \`x.com\` links to use \`xcancel.com\` so that server members won't require an account to view content and threads:\n${updated_link_list}`,
52+
);
53+
} catch {
54+
// don't handle failures
55+
}
56+
}

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)