Skip to content

Commit 813aa49

Browse files
committed
refactor: improve commands router
1 parent 42464ba commit 813aa49

File tree

10 files changed

+850
-381
lines changed

10 files changed

+850
-381
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SlashCommandProps, CommandData } from 'commandkit';
2+
3+
export const command: CommandData = {
4+
name: 'cat',
5+
description: 'This is a random cat command',
6+
};
7+
8+
export async function chatInput({ interaction }: SlashCommandProps) {
9+
const { data } = await fetch('https://aws.random.cat/meow').then((res) =>
10+
res.json(),
11+
);
12+
13+
return interaction.reply({
14+
embeds: [
15+
{
16+
title: 'Random Cat',
17+
image: {
18+
url: data.file,
19+
},
20+
color: 0x7289da,
21+
},
22+
],
23+
});
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SlashCommandProps, CommandData } from 'commandkit';
2+
3+
export const command: CommandData = {
4+
name: 'dog',
5+
description: 'This is a random dog command',
6+
};
7+
8+
export async function chatInput({ interaction }: SlashCommandProps) {
9+
const { data } = await fetch('https://random.dog/woof.json').then((res) =>
10+
res.json(),
11+
);
12+
13+
return interaction.reply({
14+
embeds: [
15+
{
16+
title: 'Random Dog',
17+
image: {
18+
url: data.url,
19+
},
20+
color: 0x7289da,
21+
},
22+
],
23+
});
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SlashCommandProps, CommandData } from 'commandkit';
2+
3+
export const command: CommandData = {
4+
name: 'computer',
5+
description: 'Random computer name',
6+
};
7+
8+
export async function chatInput({ interaction }: SlashCommandProps) {
9+
const names = ['HAL 9000', 'R2-D2', 'C-3PO', 'Data', 'Bender', 'GLaDOS'];
10+
11+
const name = names[Math.floor(Math.random() * names.length)];
12+
13+
return interaction.reply({
14+
content: `Random computer name: ${name}`,
15+
});
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SlashCommandProps, CommandData } from 'commandkit';
2+
3+
export const command: CommandData = {
4+
name: 'person',
5+
description: 'Random person name',
6+
};
7+
8+
export async function chatInput({ interaction }: SlashCommandProps) {
9+
const names = ['John Doe', 'Jane Doe', 'Alice', 'Bob', 'Charlie'];
10+
11+
const name = names[Math.floor(Math.random() * names.length)];
12+
13+
return interaction.reply({
14+
content: `Random person name: ${name}`,
15+
});
16+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
"katex@>=0.12.0 <=0.16.20": ">=0.16.21"
4040
},
4141
"packageManager": "[email protected]"
42-
}
42+
}

packages/commandkit/src/CommandKit.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { CommandKitPluginRuntime } from './plugins/runtime/CommandKitPluginRunti
1717
import { loadConfigFile } from './config/loader';
1818
import { COMMANDKIT_IS_DEV } from './utils/constants';
1919
import { registerDevHooks } from './utils/dev-hooks';
20+
import { writeFileSync } from 'node:fs';
2021

2122
export interface CommandKitConfiguration {
2223
defaultLocale: Locale;
@@ -199,7 +200,8 @@ export class CommandKit extends EventEmitter {
199200

200201
async #initCommands() {
201202
if (this.commandsRouter.isValidPath()) {
202-
await this.commandsRouter.scan();
203+
const commands = await this.commandsRouter.scan();
204+
await writeFileSync('./commands.json', JSON.stringify(commands, null, 2));
203205
}
204206

205207
await this.commandHandler.loadCommands();

0 commit comments

Comments
 (0)