Skip to content

Commit 11c396b

Browse files
Yanzi-devpotb
authored andcommitted
fix: better url mapping vxtwitter (#23)
1 parent 89ffb64 commit 11c396b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/handlers/handle-guild-message-creation.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { Message } from 'discord.js';
22
import { MessageType } from 'discord.js';
33

4+
const urlMappings = [
5+
{
6+
pattern: /https?:\/\/(mobile\.)?twitter\.com\/(\S+)\/status\/(\d+)/g,
7+
replacement: 'https://vxtwitter.com/$2/status/$3',
8+
},
9+
];
10+
411
export const handleGuildMessageCreation = async (message: Message) => {
512
if (message.author.bot) {
613
return;
@@ -10,17 +17,20 @@ export const handleGuildMessageCreation = async (message: Message) => {
1017
return;
1118
}
1219

13-
const urls = message.content.match(/https?:\/\/\S+/g);
14-
if (urls === null) {
15-
return;
16-
}
20+
let modifiedContent = message.content;
21+
let hasModification = false;
1722

18-
const newContent = message.content.replaceAll(
19-
/https?:\/\/(mobile\.)?twitter\.com/g,
20-
'https://vxtwitter.com'
21-
);
23+
for (const { pattern, replacement } of urlMappings) {
24+
if (pattern.test(modifiedContent)) {
25+
modifiedContent = modifiedContent.replace(pattern, replacement);
26+
hasModification = true;
27+
}
28+
}
2229

23-
const newMessage = [`<@${message.author.id}>`, newContent].join('\n');
30+
if (!hasModification) {
31+
return;
32+
}
33+
const newMessage = [`<@${message.author.id}>`, modifiedContent].join('\n');
2434

2535
await message.channel.send(newMessage);
2636
await message.delete();

0 commit comments

Comments
 (0)