File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments