|
| 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 | +} |
0 commit comments