|
1 |
| -import { ChannelType, type Message } from 'discord.js'; |
2 |
| -import { has_link } from './_common.js'; |
3 | 1 | 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'; |
8 | 8 |
|
9 | 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; |
| 10 | + if (message.channel.type != ChannelType.GuildText) return; |
15 | 11 |
|
16 |
| - // Get links |
17 |
| - const caughtLinks: string[] | undefined = message.content |
| 12 | + const links = message.content |
18 | 13 | .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'), |
24 | 17 | );
|
25 |
| - if (!caughtLinks) return; |
26 | 18 |
|
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 | + } |
31 | 22 |
|
32 |
| - const updatedPhrase: string = |
33 |
| - caughtLinks.length > 1 |
| 23 | + const updated_phrase: string = |
| 24 | + links.length > 1 |
34 | 25 | ? 'Here are the updated links:'
|
35 | 26 | : 'Here is the updated link:';
|
36 | 27 |
|
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'); |
46 | 31 |
|
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 | + }); |
48 | 37 |
|
49 | 38 | try {
|
50 | 39 | 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)}`, |
52 | 41 | );
|
53 | 42 |
|
54 | 43 | return; // mission complete
|
55 |
| - } catch (_) { |
| 44 | + } catch { |
56 | 45 | // assume user disabled DMs upon error
|
57 | 46 | }
|
58 | 47 |
|
59 | 48 | // Plan B: inline reply
|
60 | 49 | try {
|
61 | 50 | 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}`, |
63 | 52 | );
|
64 |
| - } catch (_) { |
| 53 | + } catch { |
65 | 54 | // don't handle failures
|
66 | 55 | }
|
67 | 56 | }
|
0 commit comments