diff --git a/src/features/x-cancel-generator.ts b/src/features/x-cancel-generator.ts new file mode 100644 index 00000000..f5a069a7 --- /dev/null +++ b/src/features/x-cancel-generator.ts @@ -0,0 +1,19 @@ +import type { ChannelHandlers } from "../types/index.js"; + +export const TWITTER_REGEX = + /https?:\/\/(?:www\.)?(?:x|twitter)\.com\/\w+\/status\/\d+/i; + +const xCancelGenerator: ChannelHandlers = { + handleMessage: async ({ msg }) => { + const match = TWITTER_REGEX.exec(msg.content); + if (!match || msg.author.bot) return; // Ignore bots to prevent loops + msg.suppressEmbeds(true); + const [url] = match; + const alternativeUrl = url.replace(/(x|twitter)\.com/i, "xcancel.com"); + await msg.channel.send( + `[Converted to \`xcancel.com\` for members with no \`x\` account](${alternativeUrl})`, + ); + }, +}; + +export default xCancelGenerator; diff --git a/src/index.ts b/src/index.ts index 4899d6da..74f39ddc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,7 @@ import voiceActivity from "./features/voice-activity.js"; import type { ChannelHandlers } from "./types/index.js"; import { scheduleMessages } from "./features/scheduled-messages.js"; import tsPlaygroundLinkShortener from "./features/tsplay.js"; +import xCancelGenerator from "./features/x-cancel-generator.js"; import { CHANNELS, initCachedChannels } from "./constants/channels.js"; import { scheduleTask } from "./helpers/schedule.js"; import { discordToken, isProd } from "./helpers/env.js"; @@ -202,6 +203,7 @@ addHandler("*", [ autoban, emojiMod, tsPlaygroundLinkShortener, + xCancelGenerator, troll, ]);