feat(bot): add support for User Apps and Guild Installs with all interaction contexts
husteno-o committed 1 months ago
4bebcc3
1 parent f0edb98
commit 4bebcc3
2 files changed
+30 -8
Showing 2 changed files+30-8
▼apps/bot/src/events/ready.ts
+29-5
| @@ -1,7 +1,12 @@ | ||
1 | 1 | import { initDatabase } from "@devpulse/database"; |
2 | 2 | import { logger } from "@devpulse/logger"; |
3 | 3 | import { type DueReminder, scheduler } from "@devpulse/scheduler"; |
4 | -import { ActivityType, type Client } from "discord.js"; | |
4 | +import { | |
5 | + ActivityType, | |
6 | + ApplicationIntegrationType, | |
7 | + type Client, | |
8 | + InteractionContextType, | |
9 | +} from "discord.js"; | |
5 | 10 | import { commands } from "../commands/index.js"; |
6 | 11 | import { BrandColors } from "../ui/colors.js"; |
7 | 12 | import { createBaseEmbed } from "../ui/embeds.js"; |
| @@ -23,12 +28,31 @@ export async function handleReady(client: Client<true>): Promise<void> { | ||
23 | 28 | status: "online", |
24 | 29 | }); |
25 | 30 | |
26 | - // 3. Register Slash Commands | |
31 | + // 3. Register Slash Commands (Supporting Server Installs & User Account Installs) | |
27 | 32 | try { |
28 | - logger.info("Registering slash commands with Discord REST API..."); | |
29 | - const commandData = commands.map((c) => c.data); | |
33 | + logger.info("Registering slash commands with Discord REST API (Server + User App support)..."); | |
34 | + const commandData = commands.map((c) => { | |
35 | + const data = c.data as any; | |
36 | + if (typeof data.setIntegrationTypes === "function") { | |
37 | + data.setIntegrationTypes([ | |
38 | + ApplicationIntegrationType.GuildInstall, | |
39 | + ApplicationIntegrationType.UserInstall, | |
40 | + ]); | |
41 | + } | |
42 | + if (typeof data.setContexts === "function") { | |
43 | + data.setContexts([ | |
44 | + InteractionContextType.Guild, | |
45 | + InteractionContextType.BotDM, | |
46 | + InteractionContextType.PrivateChannel, | |
47 | + ]); | |
48 | + } | |
49 | + return data; | |
50 | + }); | |
30 | 51 | await client.application.commands.set(commandData); |
31 | - logger.info({ commandCount: commands.length }, "Slash commands registered successfully"); | |
52 | + logger.info( | |
53 | + { commandCount: commands.length }, | |
54 | + "Slash commands registered successfully with Server & User App contexts", | |
55 | + ); | |
32 | 56 | } catch (err) { |
33 | 57 | logger.error({ err }, "Failed to register slash commands"); |
34 | 58 | } |
35 | 59 | |
▼apps/bot/src/index.ts
+1-3
| @@ -23,9 +23,7 @@ export function createBotClient(options?: { enableMessageContent?: boolean }): C | ||
23 | 23 | |
24 | 24 | client.once("ready", (c) => handleReady(c)); |
25 | 25 | client.on("interactionCreate", (i) => handleInteraction(i)); |
26 | - if (allowMessageContent) { | |
27 | - client.on("messageCreate", (m) => handleMessageCreate(m)); | |
28 | - } | |
26 | + client.on("messageCreate", (m) => handleMessageCreate(m)); | |
29 | 27 | |
30 | 28 | return client; |
31 | 29 | } |
32 | 30 | |