9
9
10
10
import Discord , {
11
11
ActivityType ,
12
+ Client ,
12
13
Events ,
13
14
GatewayIntentBits ,
14
15
type Interaction ,
16
+ Message ,
15
17
Partials ,
16
18
} from "discord.js" ;
17
19
import CommandHandler from "@/commandHandler" ;
18
20
import { env } from "@/config/env" ;
21
+ import { suppressGitHubUnfurl } from "./util/suppressGitHubUnfurl" ;
19
22
20
23
process . on ( "unhandledRejection" , reason => {
21
24
console . log ( "Unhandled Rejection:" , reason ) ;
@@ -24,8 +27,9 @@ process.on("unhandledRejection", reason => {
24
27
const client = new Discord . Client ( {
25
28
intents : [
26
29
GatewayIntentBits . Guilds ,
27
- GatewayIntentBits . GuildMessages ,
28
30
GatewayIntentBits . GuildEmojisAndStickers ,
31
+ GatewayIntentBits . GuildMessages ,
32
+ GatewayIntentBits . MessageContent ,
29
33
] ,
30
34
partials : [ Partials . Message , Partials . Channel , Partials . Reaction ] ,
31
35
} ) ;
@@ -61,24 +65,15 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
61
65
client . on ( Events . Error , e => {
62
66
console . error ( "Discord client error!" , e ) ;
63
67
} ) ;
64
- client . on ( Events . MessageCreate , async message => {
65
- if ( message . author . bot ) return ;
66
-
67
- message = await message . fetch ( ) ;
68
68
69
- for ( const embed of message . embeds ) {
70
- if ( ! embed . url ) continue ;
71
-
72
- const url = new URL ( embed . url ) ;
73
- if ( url . host !== "github.com" ) continue ;
69
+ // Message updates contain full data. Typings are corrected in a newer discord.js version.
70
+ client . on ( Events . MessageUpdate , async ( _ , newMessage ) => {
71
+ await suppressGitHubUnfurl ( newMessage as Message ) ;
72
+ } ) ;
74
73
75
- const segments = url . pathname . split ( "/" ) ;
76
- const githubUrlType : string | undefined = segments [ 3 ] ;
77
- if ( githubUrlType === "tree" || githubUrlType === "blob" ) {
78
- await message . suppressEmbeds ( ) ;
79
- return ;
80
- }
81
- }
74
+ client . on ( Events . MessageCreate , async ( message : Message < boolean > ) => {
75
+ if ( message . author . bot ) return ;
76
+ await suppressGitHubUnfurl ( message ) ;
82
77
} ) ;
83
78
84
79
client . login ( env . DISCORD_TOKEN ) ;
0 commit comments