Skip to content

Commit 37f7366

Browse files
committed
Add help global command
1 parent be07402 commit 37f7366

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

services/bot-presence/src/app-config/app-config.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export class AppConfigService {
1313
);
1414
}
1515

16+
getBotClientId() {
17+
return this.configService.get<string | undefined>(
18+
'BOT_PRESENCE_DISCORD_BOT_CLIENT_ID',
19+
);
20+
}
21+
1622
getSupporterGuildId() {
1723
return this.configService.get('BOT_PRESENCE_SUPPORTER_GUILD_ID');
1824
}

services/bot-presence/src/discord-client/discord-client.service.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { 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';
38
import { MessageBrokerService } from '../message-broker/message-broker.service';
49
import { 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
}

services/bot-presence/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
33

44
async function bootstrap() {
5+
console.log('Starting app...');
56
const app = await NestFactory.create(AppModule.forRoot());
67
app.enableShutdownHooks();
78
await app.init();

0 commit comments

Comments
 (0)