@@ -18,28 +18,41 @@ const Routes = {
1818} ;
1919
2020/**
21- * @typedef {Object } ClientData
22- * @property {string } token - The token of your Discord bot.
23- * @property {string } id - The ID of your Discord bot.
21+ * @typedef {Object } deployOptions
22+ * @property {string } appToken - The token of your Discord bot.
23+ * @property {string } appId - The ID of your Discord bot.
24+ * @property {boolean? } logs Whether to log what command was ignored, created, updated or deleted
2425 */
2526
27+ /**
28+ * @typedef {Object } DeleteOptions
29+ * @property {string } appToken The token of your Discord bot.
30+ * @property {string } appId The ID of your Discord bot.
31+ * @property {string | null } guildId The guild's ID to delete the command in (not needed for a global command)
32+ */
33+
34+ const DEFAULT_OPTS = {
35+ appToken : "" ,
36+ appId : "" ,
37+ logs : true ,
38+ } ;
39+
2640/**
2741 * Create, update and delete global and guild application commands.
2842 *
2943 * To update guild-specific commands correctly, make sure the bot is logged in.\
3044 * Otherwise the check for a guild ID is omitted, and you could make pointless requests which can also result in an error
3145 *
3246 * @param {string } folderPath The absolute path to your commands folder (the command files have to be directly in it!)
33- * @param {boolean } logs Whether to log what command was ignored, created, updated or deleted
34- * @param {ClientData } clientDetails The client's details
47+ * @param {deployOptions } opts the appId, appToken and if the actions should be logged
3548 * @returns {Promise<boolean> } `true` if the operation was successfull. Otherwise `false`.
3649 */
3750module . exports . deployCommands = async function deployCommands (
3851 folderPath ,
39- logs = false ,
40- clientDetails
52+ opts = DEFAULT_OPTS
4153) {
42- const clientId = clientDetails . id ;
54+ opts = Object . assign ( { } , DEFAULT_OPTS , opts || { } ) ;
55+ const clientId = opts . appId ;
4356
4457 let commands = [ ] ;
4558 let privateCommands = [ ] ;
@@ -48,7 +61,8 @@ module.exports.deployCommands = async function deployCommands(
4861 file . endsWith ( ".js" )
4962 ) ;
5063
51- if ( logs ) console . log ( `🔁 Started refreshing global and guild commands.` ) ;
64+ if ( opts . logs )
65+ console . log ( `🔁 Started refreshing global and guild commands.` ) ;
5266
5367 try {
5468 const currentCommands = await rest . get ( Routes . commands ( clientId ) ) ;
@@ -66,7 +80,7 @@ module.exports.deployCommands = async function deployCommands(
6680 ) ;
6781 continue ;
6882 } else if ( "data" in command && Boolean ( command . ignore ?? false ) ) {
69- if ( logs )
83+ if ( opts . logs )
7084 console . log ( `- Command '${ command . name } ' is ignored!` ) ;
7185 continue ;
7286 }
@@ -81,19 +95,20 @@ module.exports.deployCommands = async function deployCommands(
8195 }
8296 }
8397
84- const rest = new REST ( ) . setToken ( clientDetails . token ) ;
98+ const rest = new REST ( ) . setToken ( opts . token ) ;
8599
86100 let data ;
87101
88102 data = await rest . put ( Routes . commands ( clientId ) , { body : commands } ) ;
89- if ( logs ) console . log ( `✅ Global commands '${ data . length } ' refreshed` ) ;
103+ if ( opts . logs )
104+ console . log ( `✅ Global commands '${ data . length } ' refreshed` ) ;
90105
91106 for ( let cmd of privateCommands ) {
92107 for ( let gid of cmd . guildIds ) {
93108 data = await rest . post ( Routes . guildCommands ( clientId , gid ) , {
94109 body : cmd . data ,
95110 } ) ;
96- if ( logs )
111+ if ( opts . logs )
97112 console . log ( `✅ Guild command '${ data . name } ' refreshed` ) ;
98113 }
99114 }
@@ -107,51 +122,23 @@ module.exports.deployCommands = async function deployCommands(
107122/**
108123 * Shortcut method to delete an application command by its name or ID. **The client needs to be logged in!**
109124 *
110- * @param {string } command The commands's name or ID
111- * @param {string | null } guildId The guild's ID to delete the command in (not needed for a global command)
112- * @param {ClientData } clientDetails The client's details
113- * @returns {Promise<boolean> } `true` if the operation was successfull. Otherwise `false`.
125+ * @param {string } commandId The commands id. Note, that command names can only be unique among their command type.
126+ * @param {DeleteOptions } opts the bot's token, ID and the guildId (if applicable)
127+ * @returns {Promise<void> }
114128 */
115- module . exports . deleteCommand = async function deleteCommand (
116- command ,
117- guildId = null ,
118- clientDetails
119- ) {
120- const commandPath = ( cmdId , guildId = null ) => {
121- if ( guildId )
122- return Routes . guildCommand ( clientDetails . id , guildId , cmdId ) ;
123- return Routes . command ( clientDetails . id , cmdId ) ;
124- } ;
125-
126- try {
127- const rest = new REST ( ) . setToken ( clientDetails . token ) ;
128-
129- if ( / ^ \d + $ / i. test ( command ) ) {
130- await rest . delete ( commandPath ( command , guildId ) ) ;
131- } else {
132- const theCommand =
133- this . application . commands . cache . get ( command ) ??
134- ( await this . application . commands . fetch ( {
135- guildId : guildId ,
136- cache : true ,
137- } ) ) ;
138-
139- if ( ! theCommand ) {
140- console . error (
141- `❌ Command '${ command } ' not found in guild '${ guildId } '`
142- ) ;
143- return ;
144- }
145-
146- await rest . delete ( commandPath ( theCommand . id , guildId ) ) ;
147- if ( logs ) console . log ( `✅ Guild command '${ data . name } ' deleted` ) ;
148- }
149- return true ;
150- } catch ( err ) {
151- console . error (
152- `❌ Error while deleting a command in guild '${ guildId } ':` ,
153- err
154- ) ;
155- return false ;
129+ module . exports . deleteCommand = async function deleteCommand ( commandId , opts ) {
130+ const guildId = opts . guildId ?? null ;
131+
132+ const commandPath = guildId
133+ ? Routes . guildCommand ( opts . appId , guildId , commandId )
134+ : Routes . command ( opts . appId , commandId ) ;
135+
136+ if ( commandId . match ( / ^ \d + $ / i) ) {
137+ await new REST ( )
138+ . setToken ( opts . appToken )
139+ . delete ( commandPath ( commandId , guildId ) ) ;
140+ } else {
141+ throw new Error ( "command id is not a bigInt!" ) ;
156142 }
143+ return ;
157144} ;
0 commit comments