Skip to content

Commit 46fad93

Browse files
authored
Merge pull request #532 from underctrl-io/fix-json-command-handler
Properly handle different command types
2 parents 6fba793 + ab16ba2 commit 46fad93

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

apps/test-bot/commandkit.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { tasks } from '@commandkit/tasks';
99
export default defineConfig({
1010
plugins: [
1111
i18n(),
12-
legacy({ skipBuiltInValidations: true }),
12+
// legacy({ skipBuiltInValidations: true }),
1313
devtools(),
1414
cache(),
1515
ai(),
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
import { MessageContextMenuCommand } from 'commandkit';
1+
import {
2+
ChatInputCommand,
3+
MessageCommand,
4+
MessageContextMenuCommand,
5+
UserContextMenuCommand,
6+
} from 'commandkit';
27
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js';
38

49
export const command = new ContextMenuCommandBuilder()
510
.setName('translate')
6-
.setType(ApplicationCommandType.Message);
11+
.setType(ApplicationCommandType.User);
12+
13+
// export const command: CommandData = {
14+
// name: 'translate',
15+
// };
16+
17+
export const userContextMenu: UserContextMenuCommand = async ({
18+
interaction,
19+
}) => {
20+
interaction.reply('test');
21+
};
722

823
export const messageContextMenu: MessageContextMenuCommand = async ({
924
interaction,
1025
}) => {
1126
interaction.reply('test');
1227
};
28+
29+
export const chatInput: ChatInputCommand = async ({ interaction }) => {
30+
interaction.reply('test');
31+
};
32+
33+
export const message: MessageCommand = async ({ message }) => {
34+
message.reply('test');
35+
};

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ApplicationCommandType,
23
AutocompleteInteraction,
34
Awaitable,
45
Collection,
@@ -783,7 +784,7 @@ export class AppCommandHandler {
783784
*/
784785
private async loadCommand(id: string, command: Command) {
785786
try {
786-
// Skip if path is null (directory-only command group)
787+
// Skip if path is null (directory-only command group) - external plugins
787788
if (command.path === null) {
788789
this.loadedCommands.set(id, {
789790
command,
@@ -796,8 +797,6 @@ export class AppCommandHandler {
796797
data: {
797798
command: {
798799
name: command.name,
799-
description: `${command.name} command`,
800-
type: 1,
801800
},
802801
},
803802
});
@@ -836,7 +835,7 @@ export class AppCommandHandler {
836835
| string
837836
| undefined;
838837

839-
// since `description` is optional in `CommandData` type, set a fallback description if none is provided
838+
// since `CommandData.description` is optional, set a fallback description if none provided
840839
if (!commandDescription && commandFileData.chatInput) {
841840
commandDescription = 'No command description set.';
842841
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,37 @@ export class CommandRegistrar {
5050

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

53-
const collections: (CommandData & { __metadata?: CommandMetadata })[] = [
54-
{
53+
const collections: (CommandData & { __metadata?: CommandMetadata })[] =
54+
[];
55+
56+
if (cmd.data.chatInput) {
57+
collections.push({
5558
...json,
59+
type: ApplicationCommandType.ChatInput,
60+
description: json.description ?? 'No command description set.',
5661
__metadata,
57-
},
58-
];
62+
});
63+
}
5964

6065
// Handle context menu commands
61-
if (
62-
cmd.data.userContextMenu &&
63-
json.type !== ApplicationCommandType.User
64-
) {
66+
if (cmd.data.userContextMenu) {
6567
collections.push({
6668
...json,
6769
name: __metadata?.nameAliases?.user ?? json.name,
6870
type: ApplicationCommandType.User,
6971
options: undefined,
7072
description_localizations: undefined,
71-
// @ts-ignore
7273
description: undefined,
73-
// @ts-ignore
7474
__metadata,
7575
});
7676
}
7777

78-
if (
79-
cmd.data.messageContextMenu &&
80-
json.type !== ApplicationCommandType.Message
81-
) {
78+
if (cmd.data.messageContextMenu) {
8279
collections.push({
8380
...json,
8481
name: __metadata?.nameAliases?.message ?? json.name,
8582
type: ApplicationCommandType.Message,
8683
description_localizations: undefined,
87-
// @ts-ignore
8884
description: undefined,
8985
options: undefined,
9086
__metadata,

0 commit comments

Comments
 (0)