Skip to content

Commit 2d6a5d6

Browse files
feat: add cool links voting and autothread
1 parent 3b05dc9 commit 2d6a5d6

6 files changed

+156
-3
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DISCORD_TOKEN=
22
DISCORD_CLIENT_ID=
33
DISCORD_GUILD_ID=
4+
COOL_LINKS_CHANNEL_ID=
45
REDIS_URL=

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"eslint-plugin-simple-import-sort": "10.0.0",
3232
"eslint-plugin-sonarjs": "0.19.0",
3333
"eslint-plugin-unused-imports": "3.0.0",
34+
"open-graph-scraper": "6.2.2",
3435
"prettier": "3.0.0",
3536
"tsup": "7.1.0",
3637
"type-fest": "3.13.0",

pnpm-lock.yaml

Lines changed: 125 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const config = {
55
token: env.get('DISCORD_TOKEN').required().asString(),
66
clientId: env.get('DISCORD_CLIENT_ID').required().asString(),
77
guildId: env.get('DISCORD_GUILD_ID').required().asString(),
8+
coolLinksChannelId: env.get('COOL_LINKS_CHANNEL_ID').required().asString(),
89
},
910
redis: {
1011
url: env.get('REDIS_URL').required().asString(),

src/cool-links-management.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Message } from 'discord.js';
2+
import ogs from 'open-graph-scraper';
3+
4+
export const coolLinksManagement = async (message: Message) => {
5+
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
6+
const detectedURLs = message.content.match(urlRegex);
7+
8+
if (detectedURLs === null) {
9+
await message.delete();
10+
return;
11+
}
12+
13+
await message.react('✅');
14+
await message.react('❌');
15+
const { result, error } = await ogs({ url: detectedURLs[0] });
16+
const threadName = error ? message.content : `${result.ogSiteName} - ${result.ogTitle}`;
17+
await message.startThread({
18+
name: threadName,
19+
});
20+
};

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

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

4+
import { config } from '../config';
5+
import { coolLinksManagement } from '../cool-links-management';
6+
47
const urlMappings = [
58
{
69
pattern: /https?:\/\/(mobile\.)?twitter\.com\/(\S+)\/status\/(\d+)/g,
@@ -17,6 +20,11 @@ export const handleGuildMessageCreation = async (message: Message) => {
1720
return;
1821
}
1922

23+
if (message.channelId === config.discord.coolLinksChannelId) {
24+
await coolLinksManagement(message);
25+
return;
26+
}
27+
2028
let modifiedContent = message.content;
2129
let hasModification = false;
2230

0 commit comments

Comments
 (0)