Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/events/on_message/_advise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import urlRegex from 'url-regex';
import {
type Message,
ChannelType,
hideLinkEmbed,
codeBlock,
} from 'discord.js';

export default async function mutate_content(message: Message) {
if (message.channel.type != ChannelType.GuildText) return;

const links = message.content
.match(urlRegex())
?.filter((link) => link.startsWith('https://x.com'))
.map((link) =>
link.replace(/^https:\/\/x\.com/, 'https://xcancel.com'),
);

if (!links || links.length === 0) {
return;
}

const updated_phrase: string =
links.length > 1
? 'Here are the updated links:'
: 'Here is the updated link:';

const updated_link_list = links
.map((link) => `- ${hideLinkEmbed(link)}`)
.join('\n');

const updated_content = message.content.replace(urlRegex(), (match) => {
return match.startsWith('https://x.com')
? match.replace(/^https:\/\/x\.com/, 'https://xcancel.com')
: match;
});

try {
await message.author.send(
`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)}`,
);

return; // mission complete
} catch {
// assume user disabled DMs upon error
}

// Plan B: inline reply
try {
await message.reply(
`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}`,
);
} catch {
// don't handle failures
}
}
2 changes: 2 additions & 0 deletions src/events/on_message/on_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import check_links from './_check_links.js';
import spam_filter from './_spam_filter.js';
import autothread from './_autothread.js';
import slow_mode from './_slow_mode.js';
import advise from './_advise.js';
import { event } from 'jellycommands';
import { STOP } from './_common.js';

Expand All @@ -16,6 +17,7 @@ export default event({
check_links,
autothread,
slow_mode,
advise,
]) {
try {
await handler(message);
Expand Down