Skip to content

Commit d53259c

Browse files
committed
feat: replace twitter links with vxtwitter alternative (#5)
1 parent 04b6610 commit d53259c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Message } from 'discord.js';
2+
import { MessageType } from 'discord.js';
3+
4+
export const handleGuildMessageCreation = async (message: Message) => {
5+
if (message.author.bot) {
6+
return;
7+
}
8+
9+
if (message.type !== MessageType.Default) {
10+
return;
11+
}
12+
13+
const urls = message.content.match(/https?:\/\/\S+/g);
14+
if (urls === null) {
15+
return;
16+
}
17+
18+
const newContent = message.content.replaceAll(
19+
/https?:\/\/(mobile\.)?twitter\.com/g,
20+
'https://vxtwitter.com'
21+
);
22+
23+
const newMessage = [`<@${message.author.id}>`, newContent].join('\n');
24+
25+
await message.channel.send(newMessage);
26+
await message.delete();
27+
};

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client, REST, Routes } from 'discord.js';
33
import { voiceOnDemandCommand } from './commands';
44
import { config } from './config';
55
import { deleteExistingCommands } from './delete-existing-commands';
6+
import { handleGuildMessageCreation } from './handlers/handle-guild-message-creation';
67
import { handleInteractionCreation } from './handlers/handle-interaction-creation';
78
import { handleVoiceChannelDeletion } from './handlers/handle-voice-channel-deletion';
89
import { handleVoiceStateUpdate } from './handlers/handle-voice-state-update';
@@ -24,7 +25,7 @@ const bootstrap = async (client: Client) => {
2425
};
2526

2627
const client = new Client({
27-
intents: ['Guilds', 'GuildVoiceStates', 'GuildMembers'],
28+
intents: ['Guilds', 'GuildVoiceStates', 'GuildMembers', 'GuildMessages', 'MessageContent'],
2829
});
2930

3031
await bootstrap(client);
@@ -41,6 +42,10 @@ client.on('interactionCreate', async (interaction) => {
4142
await handleInteractionCreation(interaction);
4243
});
4344

45+
client.on('messageCreate', async (message) => {
46+
await handleGuildMessageCreation(message);
47+
});
48+
4449
const rest = new REST({ version: '10' }).setToken(discord.token);
4550

4651
await deleteExistingCommands(rest, discord);

0 commit comments

Comments
 (0)