@@ -2,18 +2,18 @@ import { REST } from "@discordjs/rest";
22import { readdirSync } from "node:fs" ;
33import path from "node:path" ;
44const Routes = {
5- commands : ( appId ) => {
6- return `/applications/${ appId } /commands` ;
7- } ,
8- command : ( appId , cmdId ) => {
9- return `/applications/${ appId } /commands/${ cmdId } ` ;
10- } ,
11- guildCommands : ( appId , guildId ) => {
12- return `/applications/${ appId } /guilds/${ guildId } /commands` ;
13- } ,
14- guildCommand : ( appId , guildId , cmdId ) => {
15- return `/applications/${ appId } /guilds/${ guildId } /commands/${ cmdId } ` ;
16- } ,
5+ commands : ( appId ) => {
6+ return `/applications/${ appId } /commands` ;
7+ } ,
8+ command : ( appId , cmdId ) => {
9+ return `/applications/${ appId } /commands/${ cmdId } ` ;
10+ } ,
11+ guildCommands : ( appId , guildId ) => {
12+ return `/applications/${ appId } /guilds/${ guildId } /commands` ;
13+ } ,
14+ guildCommand : ( appId , guildId , cmdId ) => {
15+ return `/applications/${ appId } /guilds/${ guildId } /commands/${ cmdId } ` ;
16+ } ,
1717} ;
1818/**
1919 * Create, update and delete global and guild application commands.
@@ -22,77 +22,78 @@ const Routes = {
2222 * Otherwise the check for a guild ID is omitted, and you could make pointless requests which can also result in an error
2323 */
2424export async function deployCommands ( folderPath , opts ) {
25- var _a ;
26- if ( ! opts . appToken || ! opts . appId ) {
27- throw new Error ( "Missing 'appToken' or 'appId' in 'opts'!" ) ;
28- }
29- let commands = [ ] ;
30- let privateCommands = [ ] ;
31- const commandFiles = readdirSync ( folderPath ) . filter ( ( file ) => file . endsWith ( ".js" ) ) ;
32- if ( opts . logs )
33- console . log ( `🔁 Started refreshing global and guild commands.` ) ;
34- try {
35- const rest = new REST ( ) . setToken ( opts . appToken ) ;
36- for ( const file of commandFiles ) {
37- const filePath = path . join ( folderPath , file ) ;
38- const command = require ( filePath ) ;
39- if ( ! ( "data" in command ) ) {
40- console . error ( `- Command '${ command . name } ' is missing the 'data' property!` ) ;
41- continue ;
42- }
43- else if ( "data" in command && Boolean ( ( _a = command . ignore ) !== null && _a !== void 0 ? _a : false ) ) {
44- if ( opts . logs )
45- console . log ( `- Command '${ command . data . name } ' is ignored!` ) ;
46- continue ;
47- }
48- if ( ( command . guildIds || [ ] ) . length > 0 ) {
49- privateCommands . push ( {
50- data : command . data ,
51- guildIds : command . guildIds ,
52- } ) ;
53- }
54- else {
55- commands . push ( command . data ) ;
56- }
57- }
58- let data = await rest . put ( Routes . commands ( opts . appId ) , {
59- body : commands ,
60- } ) ;
25+ var _a ;
26+ if ( ! opts . appToken || ! opts . appId ) {
27+ throw new Error ( "Missing 'appToken' or 'appId' in 'opts'!" ) ;
28+ }
29+ let commands = [ ] ;
30+ let privateCommands = [ ] ;
31+ const commandFiles = readdirSync ( folderPath ) . filter ( ( file ) =>
32+ file . endsWith ( ".js" )
33+ ) ;
34+ if ( opts . logs )
35+ console . log ( `🔁 Started refreshing global and guild commands.` ) ;
36+ try {
37+ const rest = new REST ( ) . setToken ( opts . appToken ) ;
38+ for ( const file of commandFiles ) {
39+ const filePath = path . join ( folderPath , file ) ;
40+ const command = require ( filePath ) ;
41+ if ( ! ( "data" in command ) ) {
42+ console . error (
43+ `- Command '${ command . name } ' is missing the 'data' property!`
44+ ) ;
45+ continue ;
46+ } else if (
47+ "data" in command &&
48+ Boolean ( ( _a = command . ignore ) !== null && _a !== void 0 ? _a : false )
49+ ) {
6150 if ( opts . logs )
62- console . log ( `✅ ${ data . length } global commands refreshed` ) ;
63- for ( let cmd of privateCommands ) {
64- for ( let gid of cmd . guildIds ) {
65- data = null ;
66- data = await rest . post ( Routes . guildCommands ( opts . appId , gid ) , {
67- body : cmd . data ,
68- } ) ;
69- if ( opts . logs )
70- console . log ( `✅ Guild command '${ data . name } ' refreshed` ) ;
71- }
72- }
73- return true ;
51+ console . log ( `- Command '${ command . data . name } ' is ignored!` ) ;
52+ continue ;
53+ }
54+ if ( ( command . guildIds || [ ] ) . length > 0 ) {
55+ privateCommands . push ( {
56+ data : command . data ,
57+ guildIds : command . guildIds ,
58+ } ) ;
59+ } else {
60+ commands . push ( command . data ) ;
61+ }
7462 }
75- catch ( err ) {
76- console . error ( "❌ Error while refreshing commands:" , err ) ;
77- return false ;
63+ let data = await rest . put ( Routes . commands ( opts . appId ) , {
64+ body : commands ,
65+ } ) ;
66+ if ( opts . logs ) console . log ( `✅ ${ data . length } global commands refreshed` ) ;
67+ for ( let cmd of privateCommands ) {
68+ for ( let gid of cmd . guildIds ) {
69+ data = null ;
70+ data = await rest . post ( Routes . guildCommands ( opts . appId , gid ) , {
71+ body : cmd . data ,
72+ } ) ;
73+ if ( opts . logs ) console . log ( `✅ Guild command '${ data . name } ' refreshed` ) ;
74+ }
7875 }
76+ return true ;
77+ } catch ( err ) {
78+ console . error ( "❌ Error while refreshing commands:" , err ) ;
79+ return false ;
80+ }
7981}
8082/**
8183 * Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
8284 */
8385export async function deleteCommand ( commandId , opts ) {
84- var _a ;
85- const guildId = ( _a = opts . guildId ) !== null && _a !== void 0 ? _a : null ;
86- const commandPath = guildId
87- ? Routes . guildCommand ( opts . appId , guildId , commandId )
88- : Routes . command ( opts . appId , commandId ) ;
89- if ( commandId . match ( / ^ \d + $ / i) ) {
90- await new REST ( { version : "10" } )
91- . setToken ( opts . appToken )
92- . delete ( commandPath ) ;
93- }
94- else {
95- throw new Error ( "'commandId' is not a only-number-string!" ) ;
96- }
97- return ;
86+ var _a ;
87+ const guildId = ( _a = opts . guildId ) !== null && _a !== void 0 ? _a : null ;
88+ const commandPath = guildId
89+ ? Routes . guildCommand ( opts . appId , guildId , commandId )
90+ : Routes . command ( opts . appId , commandId ) ;
91+ if ( commandId . match ( / ^ \d + $ / i) ) {
92+ await new REST ( { version : "10" } )
93+ . setToken ( opts . appToken )
94+ . delete ( commandPath ) ;
95+ } else {
96+ throw new Error ( "'commandId' is not a only-number-string!" ) ;
97+ }
98+ return ;
9899}
0 commit comments