@@ -10,8 +10,6 @@ const DEFAULT_OPTS = {
1010 noLogs : false ,
1111} ;
1212
13- // TODO: Error "Client is not a constructor"
14-
1513/**
1614 * A Discord Client, that is basically [discord.js' ``Client``](https://discord.js.org/docs/packages/discord.js/main/Client:Class) but with two functions added for command handling.
1715 */
@@ -95,8 +93,8 @@ module.exports = class cDClient extends Client {
9593
9694 // TODO: Update it to client.application.commands and fetch
9795 let currentCommands = this . application . commands . cache ;
98- if ( ! currentCommands )
99- if ( this ) {
96+ if ( ! currentCommands . size )
97+ if ( this . isReady ( ) ) {
10098 currentCommands = await this . application . commands . fetch ( ) ;
10199 } else {
102100 currentCommands = await rest . get (
@@ -107,7 +105,6 @@ module.exports = class cDClient extends Client {
107105 const _new = [ ] ;
108106 const updated = [ ] ;
109107 const toDelete = [ ] ;
110- let data , gid ;
111108
112109 // Initialising
113110 for ( const command of commands ) {
@@ -161,36 +158,53 @@ module.exports = class cDClient extends Client {
161158 console . log ( `✔️ Updated '${ cmd . name } ' global commands.` )
162159 ) ;
163160
161+ let gid ;
162+
164163 if ( privateCommands . length ) {
165164 // TODO: Add support for sharding
166165
167166 let clientGuilds = [ ] ;
168167
169- // TODO: Only do this, if the client is present so that no client is needed
170- // Wait for the guilds to cache before continuing
171- while ( ! this . guilds . cache . size ) {
172- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
168+ if ( ! this . isReady ( ) ) {
169+ // Wait for the guilds to cache before continuing
170+ while ( ! this . guilds . cache . size ) {
171+ await new Promise ( ( resolve ) =>
172+ setTimeout ( resolve , 1000 )
173+ ) ;
174+ }
175+ clientGuilds = this . guilds . cache ;
173176 }
174- clientGuilds = this . guilds . cache ;
175177
176178 if ( logOptions . status )
177179 console . log ( `🔁 Updating guild commands...` ) ;
180+
178181 let updatedPrivates = 0 ;
179182 for ( let command of privateCommands ) {
180183 for ( gid of command . guildIds ) {
181- // TODO: Only check the bot's guilds if they are present + add something to catch an error if a guild's ID is not found
182- if ( clientGuilds . find ( ( guild ) => guild . id === gid ) ) {
183- data = await rest . post (
184- `/applications/${ clientId } /guilds/${ gid } /commands` ,
185- {
186- body : command . data ,
187- }
188- ) ;
189- if ( logOptions . updated )
190- console . log (
191- `✔️ Updated command '${ command . data . name } ' in guild ${ gid } .`
184+ if (
185+ ( clientGuilds &&
186+ clientGuilds . find (
187+ ( guild ) => guild . id === gid
188+ ) ) ||
189+ ! clientGuilds
190+ ) {
191+ try {
192+ data = await rest . post (
193+ `/applications/${ clientId } /guilds/${ gid } /commands` ,
194+ {
195+ body : command . data ,
196+ }
197+ ) ;
198+ if ( logOptions . updated )
199+ console . log (
200+ `✔️ Updated command '${ command . data . name } ' in guild ${ gid } .`
201+ ) ;
202+ updatedPrivates ++ ;
203+ } catch ( err ) {
204+ console . error (
205+ `❌ Couldn't update '${ command . data . name } '; Maybe the guild ${ gid } wasn't found in the current guilds.`
192206 ) ;
193- updatedPrivates ++ ;
207+ }
194208 } else {
195209 console . error (
196210 `❌ Couldn't update '${ command . data . name } ' since guild ${ gid } wasn't found in the current guilds.`
0 commit comments