Skip to content

Commit f6ba448

Browse files
committed
chore: update
1 parent a385102 commit f6ba448

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

src/events/on_message/_advise.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,56 @@
1-
import { ChannelType, type Message } from 'discord.js';
2-
import { has_link } from './_common.js';
31
import urlRegex from 'url-regex';
4-
5-
const replacementMap = [
6-
{ test: 'https://x.com', replace: 'https://xcancel.com' },
7-
];
2+
import {
3+
type Message,
4+
ChannelType,
5+
hideLinkEmbed,
6+
codeBlock,
7+
} from 'discord.js';
88

99
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;
10+
if (message.channel.type != ChannelType.GuildText) return;
1511

16-
// Get links
17-
const caughtLinks: string[] | undefined = message.content
12+
const links = message.content
1813
.match(urlRegex())
19-
?.filter(
20-
(link) =>
21-
replacementMap.findIndex((item) =>
22-
link.startsWith(item.test),
23-
) !== -1,
14+
?.filter((link) => link.startsWith('https://x.com'))
15+
.map((link) =>
16+
link.replace(/^https:\/\/x\.com/, 'https://xcancel.com'),
2417
);
25-
if (!caughtLinks) return;
2618

27-
const hasXLinks: boolean = caughtLinks.some((item) =>
28-
item.startsWith('https://x.com'),
29-
);
30-
if (!hasXLinks) return;
19+
if (!links || links.length === 0) {
20+
return;
21+
}
3122

32-
const updatedPhrase: string =
33-
caughtLinks.length > 1
23+
const updated_phrase: string =
24+
links.length > 1
3425
? 'Here are the updated links:'
3526
: 'Here is the updated link:';
3627

37-
const updatedLinks = caughtLinks.map((link) => {
38-
const replaceIdx = replacementMap.findIndex((item) =>
39-
link.startsWith(item.test),
40-
);
41-
return link.replace(
42-
replacementMap[replaceIdx].test,
43-
replacementMap[replaceIdx].replace,
44-
);
45-
});
28+
const updated_link_list = links
29+
.map((link) => `- ${hideLinkEmbed(link)}`)
30+
.join('\n');
4631

47-
const updatedLinkList = updatedLinks.map((link) => `- ${link}\n`).join('');
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+
});
4837

4938
try {
5039
await message.author.send(
51-
`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}\`\`\`${updatedLinkList}\`\`\`\n\nHere is your entire message with adjusted links:\n\`\`\`${updatedLinkList}\`\`\``,
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)}`,
5241
);
5342

5443
return; // mission complete
55-
} catch (_) {
44+
} catch {
5645
// assume user disabled DMs upon error
5746
}
5847

5948
// Plan B: inline reply
6049
try {
6150
await message.reply(
62-
`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\n${updatedLinkList}`,
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}`,
6352
);
64-
} catch (_) {
53+
} catch {
6554
// don't handle failures
6655
}
6756
}

0 commit comments

Comments
 (0)