Skip to content

Commit 29ad085

Browse files
committed
feat: add commands utils
1 parent a09150d commit 29ad085

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/util/commands.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type {
2+
Client,
3+
CommandInteraction,
4+
RESTPostAPIApplicationCommandsJSONBody,
5+
} from 'discord.js';
6+
import type { Command } from '../commands/types.js';
7+
8+
export const createCommand = (
9+
data: RESTPostAPIApplicationCommandsJSONBody,
10+
execute: (interaction: CommandInteraction) => Promise<void> | void
11+
): Command => {
12+
return { data, execute } satisfies Command;
13+
};
14+
15+
export const createCommands = (
16+
commands: Array<{
17+
data: RESTPostAPIApplicationCommandsJSONBody;
18+
execute: (interaction: CommandInteraction) => Promise<void> | void;
19+
}>
20+
): Command[] => {
21+
return commands.map(({ data, execute }) => createCommand(data, execute));
22+
};
23+
24+
export const registerCommands = async (
25+
client: Client,
26+
commands: Map<string, Command>
27+
): Promise<void> => {
28+
const commandArray = Array.from(commands.values()).map((cmd) => cmd.data);
29+
30+
try {
31+
await client.application?.commands.set(commandArray);
32+
commandArray.forEach((cmd) => {
33+
console.log(`Registered command: ${cmd.type}, ${cmd.name}`);
34+
});
35+
console.log(`Registered ${commandArray.length} commands globally.`);
36+
} catch (error) {
37+
console.error('Error registering commands:', error);
38+
}
39+
};

0 commit comments

Comments
 (0)