11import { Injectable , OnModuleInit } from '@nestjs/common' ;
2- import { Client , GatewayDispatchEvents } from '@discordjs/core' ;
2+ import {
3+ ApplicationCommandType ,
4+ Client ,
5+ GatewayDispatchEvents ,
6+ InteractionType ,
7+ } from '@discordjs/core' ;
38import { MessageBrokerService } from '../message-broker/message-broker.service' ;
49import { AppConfigService } from '../app-config/app-config.service' ;
510
@@ -12,12 +17,68 @@ export class DiscordClientService implements OnModuleInit {
1217 ) { }
1318
1419 async onModuleInit ( ) {
15- await this . listenToSupporterGuildMemberJoined ( ) ;
20+ console . log ( 'Registering commands...' ) ;
21+ await this . registerCommands ( ) ;
22+ console . log ( 'Listening to events...' ) ;
23+ await this . listenToEvents ( ) ;
1624 }
1725
18- async listenToSupporterGuildMemberJoined ( ) {
26+ async registerCommands ( ) {
27+ const botClientId = this . configService . getBotClientId ( ) ;
28+
29+ if ( ! botClientId ) {
30+ console . log (
31+ 'No BOT_PRESENCE_DISCORD_BOT_CLIENT_ID found. Skipping registration of commands.' ,
32+ ) ;
33+ return ;
34+ }
35+
36+ try {
37+ const commands =
38+ await this . client . api . applicationCommands . getGlobalCommands (
39+ botClientId ,
40+ ) ;
41+
42+ if ( commands . some ( ( c ) => c . name === 'help' ) ) {
43+ console . log ( 'Help command already registered.' ) ;
44+ return ;
45+ }
46+
47+ await this . client . api . applicationCommands . createGlobalCommand (
48+ botClientId ,
49+ {
50+ name : 'help' ,
51+ description : 'Show information on how to use MonitoRSS.' ,
52+ type : ApplicationCommandType . ChatInput ,
53+ } ,
54+ ) ;
55+
56+ console . log ( 'Help command registered successfully.' ) ;
57+ } catch ( err ) {
58+ console . error ( 'Error registering commands:' , err ) ;
59+ }
60+ }
61+
62+ async listenToEvents ( ) {
1963 const supporterGuildId = this . configService . getSupporterGuildId ( ) ;
2064
65+ this . client . on ( GatewayDispatchEvents . InteractionCreate , ( interaction ) => {
66+ if ( interaction . data . type !== InteractionType . ApplicationCommand ) {
67+ return ;
68+ }
69+
70+ if ( interaction . data . data . name === 'help' ) {
71+ this . client . api . interactions
72+ . reply ( interaction . data . id , interaction . data . token , {
73+ content :
74+ 'To add and manage feeds, please visit the "Control Panel" at <https://monitorss.xyz> to add and control feeds.\n\nFor support, please either reach out to support@monitorss.xyz or join the support Discord server at https://discord.gg/pudv7Rx.' ,
75+ } )
76+ . catch ( ( err ) => {
77+ console . error ( 'Error replying to interaction:' , err ) ;
78+ } ) ;
79+ }
80+ } ) ;
81+
2182 if ( ! supporterGuildId ) {
2283 return ;
2384 }
0 commit comments