Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions apps/test-bot/src/app/commands/(general)/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ export const chatInput: ChatInputCommand = async (ctx) => {
const botVersion = $botVersion();

let i = 1;
const commands = ctx.commandkit.commandsRouter
.getData()
.commands.map((c) => {
return `${i++}. **\`/${c.name}\`** - ${c.category}`;
const commands = ctx.commandkit.commandHandler
.getCommandsArray()
.map((c) => {
const cmdName = c.data.command.name;
const cmd = c.discordId
? `</${cmdName}:${c.discordId}>`
: `**\`/${cmdName}\`**`;
return `${i++}. ${cmd} - ${c.command.category ?? 'N/A'}`;
})
.join('\n');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AppCommandHandler

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

Handles application commands for CommandKit, including loading, registration, and execution.
Manages both slash commands and message commands with middleware support.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ Handles registration of Discord application commands (slash commands, context me
```ts title="Signature"
class CommandRegistrar {
constructor(commandkit: CommandKit)
getCommandsData() => (CommandData & { __metadata?: CommandMetadata })[];
getCommandsData() => (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[];
register() => ;
updateGlobalCommands(commands: (CommandData & { __metadata?: CommandMetadata })[]) => ;
updateGuildCommands(commands: (CommandData & { __metadata?: CommandMetadata })[]) => ;
updateGlobalCommands(commands: (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[]) => ;
updateGuildCommands(commands: (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[]) => ;
}
```

Expand All @@ -36,7 +45,7 @@ class CommandRegistrar {
Creates an instance of CommandRegistrar.
### getCommandsData

<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> })[]`} />
<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; })[]`} />

Gets the commands data.
### register
Expand All @@ -46,12 +55,12 @@ Gets the commands data.
Registers loaded commands.
### updateGlobalCommands

<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> })[]) => `} />
<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; })[]) => `} />

Updates the global commands.
### updateGuildCommands

<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> })[]) => `} />
<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; })[]) => `} />

Updates the guild commands.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Represents a loaded command with its metadata and configuration.

```ts title="Signature"
interface LoadedCommand {
discordId: string | null;
command: Command;
metadata: CommandMetadata;
data: AppCommand;
Expand All @@ -27,21 +28,27 @@ interface LoadedCommand {

<div className="members-wrapper">

### discordId

<MemberInfo kind="property" type={`string | null`} />

The associated discord snowflake id for this command.
If the information is not yet available, this will be `null`.
### command

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


The command data.
### metadata

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


The metadata for this command.
### data

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


The data for this command.


</div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## PreparedAppCommandExecution

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## CommandBuilderLike

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

Type representing command builder objects supported by CommandKit.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## CommandTypeData

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

Type representing command data identifier.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## ResolvableCommand

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

Type for commands that can be resolved by the handler.

Expand Down
16 changes: 16 additions & 0 deletions packages/commandkit/src/app/handlers/AppCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,22 @@ interface AppCommandMiddleware {
* Represents a loaded command with its metadata and configuration.
*/
export interface LoadedCommand {
/**
* The associated discord snowflake id for this command.
* If the information is not yet available, this will be `null`.
*/
discordId: string | null;
/**
* The command data.
*/
command: Command;
/**
* The metadata for this command.
*/
metadata: CommandMetadata;
/**
* The data for this command.
*/
data: AppCommand;
}

Expand Down Expand Up @@ -787,6 +801,7 @@ export class AppCommandHandler {
// Skip if path is null (directory-only command group) - external plugins
if (command.path === null) {
this.loadedCommands.set(id, {
discordId: null,
command,
metadata: {
guilds: [],
Expand Down Expand Up @@ -897,6 +912,7 @@ export class AppCommandHandler {
}

this.loadedCommands.set(id, {
discordId: null,
command,
metadata: {
guilds: commandJson.guilds,
Expand Down
55 changes: 48 additions & 7 deletions packages/commandkit/src/app/register/CommandRegistrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export class CommandRegistrar {
/**
* Gets the commands data.
*/
public getCommandsData(): (CommandData & { __metadata?: CommandMetadata })[] {
public getCommandsData(): (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[] {
const handler = this.commandkit.commandHandler;
// Use the public method instead of accessing private property
const commands = handler.getCommandsArray();
Expand All @@ -50,15 +53,20 @@ export class CommandRegistrar {

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

const collections: (CommandData & { __metadata?: CommandMetadata })[] =
[];
const collections: (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[] = [];

if (cmd.data.chatInput) {
collections.push({
...json,
type: ApplicationCommandType.ChatInput,
description: json.description ?? 'No command description set.',
__metadata,
__applyId: (id: string) => {
cmd.discordId = id;
},
});
}

Expand All @@ -72,6 +80,9 @@ export class CommandRegistrar {
description_localizations: undefined,
description: undefined,
__metadata,
__applyId: (id: string) => {
cmd.discordId = id;
},
});
}

Expand All @@ -84,6 +95,9 @@ export class CommandRegistrar {
description: undefined,
options: undefined,
__metadata,
__applyId: (id: string) => {
cmd.discordId = id;
},
});
}

Expand Down Expand Up @@ -138,7 +152,10 @@ export class CommandRegistrar {
* Updates the global commands.
*/
public async updateGlobalCommands(
commands: (CommandData & { __metadata?: CommandMetadata })[],
commands: (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[],
) {
if (!commands.length) return;

Expand All @@ -162,9 +179,20 @@ export class CommandRegistrar {
body: commands.map((c) => ({
...c,
__metadata: undefined,
__applyId: undefined,
})),
},
)) as CommandData[];
)) as (CommandData & { id: string })[];

// inject the command id into the command
data.forEach((c) => {
if (!c.id) return;
const cmd = commands.find(
(co) => co.name === c.name && co.type === c.type,
);
if (!cmd) return;
cmd.__applyId?.(c.id);
});

Logger.info(
`✨ Refreshed ${data.length} global application (/) commands`,
Expand All @@ -178,7 +206,10 @@ export class CommandRegistrar {
* Updates the guild commands.
*/
public async updateGuildCommands(
commands: (CommandData & { __metadata?: CommandMetadata })[],
commands: (CommandData & {
__metadata?: CommandMetadata;
__applyId(id: string): void;
})[],
) {
if (!commands.length) return;

Expand Down Expand Up @@ -242,9 +273,19 @@ export class CommandRegistrar {
body: guildCommands.map((b) => ({
...b,
__metadata: undefined,
__applyId: undefined,
})),
},
)) as CommandData[];
)) as (CommandData & { id: string })[];

data.forEach((c) => {
if (!c.id) return;
const cmd = commands.find(
(co) => co.name === c.name && co.type === c.type,
);
if (!cmd) return;
cmd.__applyId?.(c.id);
});

count += data.length;
}
Expand Down
Loading