Skip to content

Commit a1e40dc

Browse files
committed
initial pre-release for deleteGuildCommand
1 parent b68b90c commit a1e40dc

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ client.once(Events.ClientReady, (readyClient) => {
8585
client.login(token);
8686
```
8787

88+
### Delete a guild-command
89+
90+
There are two options to do this: Either with the command's ID or his name.
91+
92+
In this example we are building a manager-command that has a StringOption for the command where one can paste in the ID or the name.\
93+
94+
```js
95+
const
96+
```
97+
98+
8899
#### `logOptions`
89100

90101
| Option name | Description / Example | Default |

index.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const DEFAULT_OPTS = {
1414
* 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.
1515
*/
1616
module.exports = class cDClient extends Client {
17+
checkForCredentials({ token }) {
18+
if (!(this.token || token || this.isReady())) {
19+
console.error(
20+
"Either token must be given or the client must be logged in!"
21+
);
22+
return false;
23+
}
24+
return true;
25+
}
26+
1727
/**
1828
* Create, update and delete global and guild application commands.
1929
*
@@ -25,13 +35,7 @@ module.exports = class cDClient extends Client {
2535
* @param {DEFAULT_OPTS} logOptions Whether to log what command was ignored, created, updated or deleted
2636
*/
2737
async deployCommands(folderPath, token = null, logOptions = DEFAULT_OPTS) {
28-
if (!(this.token || token || this.isReady())) {
29-
console.error(
30-
"Either token must be given or the client must be logged in!"
31-
);
32-
return;
33-
}
34-
38+
if (this.checkForCredentials({ token })) return;
3539
if (logOptions.noLogs) {
3640
logOptions.ignored = false;
3741
logOptions.created = false;
@@ -224,15 +228,49 @@ module.exports = class cDClient extends Client {
224228
}
225229

226230
/**
231+
* Delete an application command by its name or ID. **The client needs to be logged in!**
227232
*
228233
* @param {string} command The commands's name or ID | the name will be parsed first
229234
* @param {string} guildId The guild's ID to delete the command in
230235
* @returns {Promise<void>}
231236
*/
232-
async deleteGuildCommand(commandName, guildId) {
237+
async deleteGuildCommand(command, guildId) {
238+
if (!this.isReady()) {
239+
console.error("The client must be logged in!");
240+
return;
241+
} else if (!/^\d+$/i.test(guildId)) {
242+
console.error("The guildId is invalid! Must be a numerous string.");
243+
return;
244+
}
245+
233246
try {
247+
if (/^\d+$/i.test(command)) {
248+
await this.application.commands.delete(command, guildId);
249+
} else {
250+
const theCommand =
251+
this.application.commands.cache.get(command) ??
252+
(await this.application.commands.fetch({
253+
guildId: guildId,
254+
cache: true,
255+
}));
256+
257+
if (!theCommand) {
258+
console.error(
259+
`Command '${command}' not found in guild '${guildId}'`
260+
);
261+
return;
262+
}
263+
264+
await this.application.commands.delete(
265+
theCommand.id,
266+
guildId
267+
);
268+
}
234269
} catch (err) {
235-
console.error("Error while deleting a guild command", error);
270+
console.error(
271+
`Error while deleting a command in guild '${guildId}'`,
272+
err
273+
);
236274
}
237275
return;
238276
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "djs-command-deployer",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "",
55
"main": "./index.js",
66
"scripts": { },

0 commit comments

Comments
 (0)