Skip to content

fix: better url mapping vxtwitter🧵 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
27 changes: 18 additions & 9 deletions src/handlers/handle-guild-message-creation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Message } from 'discord.js';
import { MessageType } from 'discord.js';

const urlMappings = [
{
pattern: /https?:\/\/(mobile\.)?twitter\.com\/\S+/g,
replacement: 'https://vxtwitter.com',
},
];
export const handleGuildMessageCreation = async (message: Message) => {
if (message.author.bot) {
return;
Expand All @@ -10,17 +16,20 @@ export const handleGuildMessageCreation = async (message: Message) => {
return;
}

const urls = message.content.match(/https?:\/\/\S+/g);
if (urls === null) {
return;
}
let modifiedContent = message.content;
let hasModification = false;

const newContent = message.content.replaceAll(
/https?:\/\/(mobile\.)?twitter\.com/g,
'https://vxtwitter.com'
);
for (const { pattern, replacement } of urlMappings) {
if (pattern.test(modifiedContent)) {
modifiedContent = modifiedContent.replace(pattern, replacement);
hasModification = true;
}
}

const newMessage = [`<@${message.author.id}>`, newContent].join('\n');
if (!hasModification) {
return;
}
const newMessage = [`<@${message.author.id}>`, modifiedContent].join('\n');

await message.channel.send(newMessage);
await message.delete();
Expand Down