99
1010import Discord , {
1111 ActivityType ,
12+ Client ,
1213 Events ,
1314 GatewayIntentBits ,
1415 type Interaction ,
16+ Message ,
1517 Partials ,
1618} from "discord.js" ;
1719import CommandHandler from "@/commandHandler" ;
1820import { env } from "@/config/env" ;
21+ import { suppressGitHubUnfurl } from "./util/suppressGitHubUnfurl" ;
1922
2023process . on ( "unhandledRejection" , reason => {
2124 console . log ( "Unhandled Rejection:" , reason ) ;
@@ -24,8 +27,9 @@ process.on("unhandledRejection", reason => {
2427const client = new Discord . Client ( {
2528 intents : [
2629 GatewayIntentBits . Guilds ,
27- GatewayIntentBits . GuildMessages ,
2830 GatewayIntentBits . GuildEmojisAndStickers ,
31+ GatewayIntentBits . GuildMessages ,
32+ GatewayIntentBits . MessageContent ,
2933 ] ,
3034 partials : [ Partials . Message , Partials . Channel , Partials . Reaction ] ,
3135} ) ;
@@ -61,24 +65,15 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
6165client . on ( Events . Error , e => {
6266 console . error ( "Discord client error!" , e ) ;
6367} ) ;
64- client . on ( Events . MessageCreate , async message => {
65- if ( message . author . bot ) return ;
66-
67- message = await message . fetch ( ) ;
6868
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+ } ) ;
7473
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 ) ;
8277} ) ;
8378
8479client . login ( env . DISCORD_TOKEN ) ;
0 commit comments