Skip to content

Commit 77caf26

Browse files
authored
Merge pull request #553 from underctrl-io/inject-discord-command0id
feat: inject discord command id after registration
2 parents 4453cd2 + a923ede commit 77caf26

File tree

10 files changed

+102
-25
lines changed

10 files changed

+102
-25
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

apps/website/docs/api-reference/commandkit/classes/app-command-handler.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## AppCommandHandler
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="181" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="195" packageName="commandkit" />
1717

1818
Handles application commands for CommandKit, including loading, registration, and execution.
1919
Manages both slash commands and message commands with middleware support.

apps/website/docs/api-reference/commandkit/classes/command-registrar.mdx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ Handles registration of Discord application commands (slash commands, context me
2020
```ts title="Signature"
2121
class CommandRegistrar {
2222
constructor(commandkit: CommandKit)
23-
getCommandsData() => (CommandData & { __metadata?: CommandMetadata })[];
23+
getCommandsData() => (CommandData & {
24+
__metadata?: CommandMetadata;
25+
__applyId(id: string): void;
26+
})[];
2427
register() => ;
25-
updateGlobalCommands(commands: (CommandData & { __metadata?: CommandMetadata })[]) => ;
26-
updateGuildCommands(commands: (CommandData & { __metadata?: CommandMetadata })[]) => ;
28+
updateGlobalCommands(commands: (CommandData & {
29+
__metadata?: CommandMetadata;
30+
__applyId(id: string): void;
31+
})[]) => ;
32+
updateGuildCommands(commands: (CommandData & {
33+
__metadata?: CommandMetadata;
34+
__applyId(id: string): void;
35+
})[]) => ;
2736
}
2837
```
2938

@@ -36,7 +45,7 @@ class CommandRegistrar {
3645
Creates an instance of CommandRegistrar.
3746
### getCommandsData
3847

39-
<MemberInfo kind="method" type={`() => (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a> })[]`} />
48+
<MemberInfo kind="method" type={`() => (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a>; __applyId(id: string): void; })[]`} />
4049

4150
Gets the commands data.
4251
### register
@@ -46,12 +55,12 @@ Gets the commands data.
4655
Registers loaded commands.
4756
### updateGlobalCommands
4857

49-
<MemberInfo kind="method" type={`(commands: (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a> })[]) => `} />
58+
<MemberInfo kind="method" type={`(commands: (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a>; __applyId(id: string): void; })[]) => `} />
5059

5160
Updates the global commands.
5261
### updateGuildCommands
5362

54-
<MemberInfo kind="method" type={`(commands: (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a> })[]) => `} />
63+
<MemberInfo kind="method" type={`(commands: (<a href='/docs/api-reference/commandkit/types/command-data#commanddata'>CommandData</a> &#38; { __metadata?: <a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a>; __applyId(id: string): void; })[]) => `} />
5564

5665
Updates the guild commands.
5766

apps/website/docs/api-reference/commandkit/interfaces/loaded-command.mdx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Represents a loaded command with its metadata and configuration.
1919

2020
```ts title="Signature"
2121
interface LoadedCommand {
22+
discordId: string | null;
2223
command: Command;
2324
metadata: CommandMetadata;
2425
data: AppCommand;
@@ -27,21 +28,27 @@ interface LoadedCommand {
2728

2829
<div className="members-wrapper">
2930

31+
### discordId
32+
33+
<MemberInfo kind="property" type={`string | null`} />
34+
35+
The associated discord snowflake id for this command.
36+
If the information is not yet available, this will be `null`.
3037
### command
3138

3239
<MemberInfo kind="property" type={`<a href='/docs/api-reference/commandkit/interfaces/command#command'>Command</a>`} />
3340

34-
41+
The command data.
3542
### metadata
3643

3744
<MemberInfo kind="property" type={`<a href='/docs/api-reference/commandkit/interfaces/command-metadata#commandmetadata'>CommandMetadata</a>`} />
3845

39-
46+
The metadata for this command.
4047
### data
4148

4249
<MemberInfo kind="property" type={`<a href='/docs/api-reference/commandkit/types/app-command#appcommand'>AppCommand</a>`} />
4350

44-
51+
The data for this command.
4552

4653

4754
</div>

apps/website/docs/api-reference/commandkit/interfaces/prepared-app-command-execution.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## PreparedAppCommandExecution
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="120" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="134" packageName="commandkit" />
1717

1818
Represents a prepared command execution with all necessary data and middleware.
1919

apps/website/docs/api-reference/commandkit/types/command-builder-like.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## CommandBuilderLike
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="129" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="143" packageName="commandkit" />
1717

1818
Type representing command builder objects supported by CommandKit.
1919

apps/website/docs/api-reference/commandkit/types/command-type-data.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## CommandTypeData
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="101" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="115" packageName="commandkit" />
1717

1818
Type representing command data identifier.
1919

apps/website/docs/api-reference/commandkit/types/resolvable-command.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## ResolvableCommand
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="106" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/app/handlers/AppCommandHandler.ts" sourceLine="120" packageName="commandkit" />
1717

1818
Type for commands that can be resolved by the handler.
1919

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)