Skip to content

Commit db1b157

Browse files
committed
feat: inject discord command id after registration
1 parent 4453cd2 commit db1b157

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

apps/test-bot/src/app/commands/(general)/help.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ export const chatInput: ChatInputCommand = async (ctx) => {
2323
const botVersion = $botVersion();
2424

2525
let i = 1;
26-
const commands = ctx.commandkit.commandsRouter
27-
.getData()
28-
.commands.map((c) => {
29-
return `${i++}. **\`/${c.name}\`** - ${c.category}`;
26+
const commands = ctx.commandkit.commandHandler
27+
.getCommandsArray()
28+
.map((c) => {
29+
const cmdName = c.data.command.name;
30+
const cmd = c.discordId
31+
? `</${cmdName}:${c.discordId}>`
32+
: `**\`/${cmdName}\`**`;
33+
return `${i++}. ${cmd} - ${c.command.category ?? 'N/A'}`;
3034
})
3135
.join('\n');
3236

packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,22 @@ interface AppCommandMiddleware {
9090
* Represents a loaded command with its metadata and configuration.
9191
*/
9292
export interface LoadedCommand {
93+
/**
94+
* The associated discord snowflake id for this command.
95+
* If the information is not yet available, this will be `null`.
96+
*/
97+
discordId: string | null;
98+
/**
99+
* The command data.
100+
*/
93101
command: Command;
102+
/**
103+
* The metadata for this command.
104+
*/
94105
metadata: CommandMetadata;
106+
/**
107+
* The data for this command.
108+
*/
95109
data: AppCommand;
96110
}
97111

@@ -787,6 +801,7 @@ export class AppCommandHandler {
787801
// Skip if path is null (directory-only command group) - external plugins
788802
if (command.path === null) {
789803
this.loadedCommands.set(id, {
804+
discordId: null,
790805
command,
791806
metadata: {
792807
guilds: [],
@@ -897,6 +912,7 @@ export class AppCommandHandler {
897912
}
898913

899914
this.loadedCommands.set(id, {
915+
discordId: null,
900916
command,
901917
metadata: {
902918
guilds: commandJson.guilds,

packages/commandkit/src/app/register/CommandRegistrar.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export class CommandRegistrar {
3737
/**
3838
* Gets the commands data.
3939
*/
40-
public getCommandsData(): (CommandData & { __metadata?: CommandMetadata })[] {
40+
public getCommandsData(): (CommandData & {
41+
__metadata?: CommandMetadata;
42+
__applyId(id: string): void;
43+
})[] {
4144
const handler = this.commandkit.commandHandler;
4245
// Use the public method instead of accessing private property
4346
const commands = handler.getCommandsArray();
@@ -50,15 +53,20 @@ export class CommandRegistrar {
5053

5154
const __metadata = cmd.metadata ?? cmd.data.metadata;
5255

53-
const collections: (CommandData & { __metadata?: CommandMetadata })[] =
54-
[];
56+
const collections: (CommandData & {
57+
__metadata?: CommandMetadata;
58+
__applyId(id: string): void;
59+
})[] = [];
5560

5661
if (cmd.data.chatInput) {
5762
collections.push({
5863
...json,
5964
type: ApplicationCommandType.ChatInput,
6065
description: json.description ?? 'No command description set.',
6166
__metadata,
67+
__applyId: (id: string) => {
68+
cmd.discordId = id;
69+
},
6270
});
6371
}
6472

@@ -72,6 +80,9 @@ export class CommandRegistrar {
7280
description_localizations: undefined,
7381
description: undefined,
7482
__metadata,
83+
__applyId: (id: string) => {
84+
cmd.discordId = id;
85+
},
7586
});
7687
}
7788

@@ -84,6 +95,9 @@ export class CommandRegistrar {
8495
description: undefined,
8596
options: undefined,
8697
__metadata,
98+
__applyId: (id: string) => {
99+
cmd.discordId = id;
100+
},
87101
});
88102
}
89103

@@ -138,7 +152,10 @@ export class CommandRegistrar {
138152
* Updates the global commands.
139153
*/
140154
public async updateGlobalCommands(
141-
commands: (CommandData & { __metadata?: CommandMetadata })[],
155+
commands: (CommandData & {
156+
__metadata?: CommandMetadata;
157+
__applyId(id: string): void;
158+
})[],
142159
) {
143160
if (!commands.length) return;
144161

@@ -162,9 +179,20 @@ export class CommandRegistrar {
162179
body: commands.map((c) => ({
163180
...c,
164181
__metadata: undefined,
182+
__applyId: undefined,
165183
})),
166184
},
167-
)) as CommandData[];
185+
)) as (CommandData & { id: string })[];
186+
187+
// inject the command id into the command
188+
data.forEach((c) => {
189+
if (!c.id) return;
190+
const cmd = commands.find(
191+
(co) => co.name === c.name && co.type === c.type,
192+
);
193+
if (!cmd) return;
194+
cmd.__applyId?.(c.id);
195+
});
168196

169197
Logger.info(
170198
`✨ Refreshed ${data.length} global application (/) commands`,
@@ -178,7 +206,10 @@ export class CommandRegistrar {
178206
* Updates the guild commands.
179207
*/
180208
public async updateGuildCommands(
181-
commands: (CommandData & { __metadata?: CommandMetadata })[],
209+
commands: (CommandData & {
210+
__metadata?: CommandMetadata;
211+
__applyId(id: string): void;
212+
})[],
182213
) {
183214
if (!commands.length) return;
184215

@@ -242,9 +273,19 @@ export class CommandRegistrar {
242273
body: guildCommands.map((b) => ({
243274
...b,
244275
__metadata: undefined,
276+
__applyId: undefined,
245277
})),
246278
},
247-
)) as CommandData[];
279+
)) as (CommandData & { id: string })[];
280+
281+
data.forEach((c) => {
282+
if (!c.id) return;
283+
const cmd = commands.find(
284+
(co) => co.name === c.name && co.type === c.type,
285+
);
286+
if (!cmd) return;
287+
cmd.__applyId?.(c.id);
288+
});
248289

249290
count += data.length;
250291
}

0 commit comments

Comments
 (0)