|
1 | | -const { Client, REST } = require("discord.js"); |
| 1 | +const { Client, REST, Collection, Routes } = require("discord.js"); |
2 | 2 | const { readdirSync } = require("node:fs"); |
3 | 3 | const path = require("node:path"); |
4 | 4 |
|
@@ -167,7 +167,7 @@ class cDClient extends Client { |
167 | 167 |
|
168 | 168 | let clientGuilds = []; |
169 | 169 |
|
170 | | - if (!this.isReady()) { |
| 170 | + if (!this.isReady() || this.ws.status != 5) { |
171 | 171 | // Wait for the guilds to cache before continuing |
172 | 172 | while (!this.guilds.cache.size) { |
173 | 173 | await new Promise((resolve) => |
@@ -228,42 +228,50 @@ class cDClient extends Client { |
228 | 228 | * |
229 | 229 | * @param {string} command The commands's name or ID |
230 | 230 | * @param {string | null} guildId The guild's ID to delete the command in (not needed for a global command) |
231 | | - * @returns {Promise<void>} |
| 231 | + * @param {Collection | null} commands The command-map like explained [in the guide](https://discordjs.guide/creating-your-bot/command-handling). This is important when using only the name of the command (the command name needs to be associated with the command id) |
| 232 | + * @returns {Promise<boolean>} `true` if the operation was successfull. Otherwise an error will be raised. |
232 | 233 | */ |
233 | | - async deleteCommand(command, guildId = null) { |
234 | | - if (!this.isReady()) { |
235 | | - console.error("The client must be logged in!"); |
236 | | - return; |
| 234 | + async deleteCommand(command, guildId = null, commands = new Collection()) { |
| 235 | + if (!this.isReady() || !this.ws.status == 0) { |
| 236 | + throw new Error("The client must be logged in!"); |
237 | 237 | } else if (guildId && !/^\d+$/i.test(guildId)) { |
238 | | - console.error("The guildId is invalid! Must be a numerous string."); |
239 | | - return; |
| 238 | + throw new Error( |
| 239 | + "The guildId is invalid! Must be a numerous string." |
| 240 | + ); |
240 | 241 | } |
241 | 242 |
|
242 | 243 | try { |
243 | 244 | if (/^\d+$/i.test(command)) { |
244 | 245 | await this.application.commands.delete(command, guildId); |
245 | 246 | } else { |
246 | | - const theCommand = |
247 | | - this.application.commands.cache.get(command) ?? |
248 | | - (await this.application.commands.fetch({ |
249 | | - guildId: guildId, |
250 | | - cache: true, |
251 | | - })); |
| 247 | + let theCommand = commands.find((cmd) => cmd.name === command); |
| 248 | + if (!theCommand) { |
| 249 | + const commands = await this.rest.get( |
| 250 | + Routes.applicationGuildCommands( |
| 251 | + this.application.id, |
| 252 | + guildId |
| 253 | + ) |
| 254 | + ); |
| 255 | + |
| 256 | + theCommand = commands.find((cmd) => cmd.name === command); |
| 257 | + } |
252 | 258 |
|
253 | 259 | if (!theCommand) { |
254 | | - console.error( |
| 260 | + throw new Error( |
255 | 261 | `❌ Command '${command}' not found in guild '${guildId}'` |
256 | 262 | ); |
257 | | - return; |
258 | 263 | } |
259 | 264 |
|
260 | 265 | await this.application.commands.delete(theCommand.id, guildId); |
261 | 266 | } |
262 | 267 | } catch (err) { |
263 | 268 | console.error( |
264 | | - `❌ Error while deleting a command in guild '${guildId}'`, |
| 269 | + `❌ Error while deleting command '${command}' in guild '${guildId}':`, |
265 | 270 | err |
266 | 271 | ); |
| 272 | + throw new Error( |
| 273 | + `Error while deleting command '${command}' in guild '${guildId}'` |
| 274 | + ); |
267 | 275 | } |
268 | 276 | return; |
269 | 277 | } |
|
0 commit comments