Skip to content

Commit 85fb21b

Browse files
committed
added fixes for deleteCommand()
1 parent a1e40dc commit 85fb21b

35 files changed

Lines changed: 348 additions & 2380 deletions

README.md

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,32 @@ _The `src` folder it can be any folder. Just the `commands` folder should be a s
2828

2929
> **Note that for using this you need to install [discord.js](https://discordjs.guide/)!**
3030
31+
<details>
32+
<summary>Fields in the command file (Click to expand)</summary>
33+
34+
| Key name | Description | Default |
35+
| -------- | --------------------------------------------------------------------------------------------------------- | ------- |
36+
| ignore | If set to `true` then this command will be ignored upon refreshing | `false` |
37+
| guildIds | An Array of guild ids in which the command should be registered/updated ; command is global if not set | [] |
38+
| data | The raw command data [Learn more about it here](https://discordjs.guide/creating-your-bot/slash-commands) | `-` |
39+
| run | The function to call (It's only important for your own logic - so name this whatever you want) | `-` |
40+
41+
`-` means that it doesn't have a default value
42+
43+
</details>
44+
3145
```js
3246
const { SlashCommandBuilder } = require("discord.js");
3347

3448
module.exports = {
35-
ignore: true, // If set to true, this command will not ignored when refreshing all commands
36-
guildIds: [
37-
// If set, the command will be registered/updated in all guilds | This wont automatically delete them from guilds!
38-
"123456789",
39-
"987654321",
40-
],
41-
data: new SlashCommandBuilder() // Your command data
49+
ignore: true,
50+
guildIds: ["123456789", "987654321"], // Note: This wont automatically delete them from guilds!
51+
data: new SlashCommandBuilder()
4252
.setName("ping")
4353
.setDescription("Replies with Pong!"),
44-
run: async (interaction) => {
45-
// The function to call whenever the command is executed
46-
await interaction.reply("Pong!");
47-
},
48-
// Other way if you dont want an anonymous function
54+
55+
// The function to call whenever the command is executed (Doesnt matter when calling client.deployCommands())
4956
async run(interaction) {
50-
// The function to call whenever the command is executed
5157
await interaction.reply("Pong!");
5258
},
5359
};
@@ -60,41 +66,77 @@ const { Events, GatewayIntentBits } = require("discord.js");
6066
// Name it whatever you want
6167
const Client = require("djs-command-deployer");
6268
const { join: pathJoin } = require("node:path");
69+
require("dotenv").config();
6370

64-
const { token } = require("./config.json");
65-
66-
// Set up your client like shown in (https://discordjs.guide/creating-your-bot/main-file)
71+
// Set up your client ; use the guide for example (https://discordjs.guide/creating-your-bot/main-file)
6772

6873
// Create a new client instance
69-
let client = new CommandClient({ intents: [GatewayIntentBits.Guilds] });
74+
let client = new Client({ intents: [GatewayIntentBits.Guilds] });
7075

7176
// When the client is ready, run this code (only once).
72-
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
73-
// It makes some properties non-nullable.
7477
client.once(Events.ClientReady, (readyClient) => {
7578
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
7679

77-
// Call the CommandDeployer to refresh your commands
80+
// Call deployCommands to refresh your commands
7881
readyClient.deployCommands(
7982
pathJoin(__dirname, "commands", "utility")
8083
// Optional: logOptions object
8184
);
8285
});
8386

8487
// Log in to Discord with your client's token
85-
client.login(token);
88+
client.login(process.env.DISCORD_TOKEN);
8689
```
8790

8891
### Delete a guild-command
8992

9093
There are two options to do this: Either with the command's ID or his name.
9194

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.\
95+
In this example we are building a manager-command that has StringOptions for the command and the guild id where one can paste in the ID or the name.\
9396

94-
```js
95-
const
96-
```
97+
````js
98+
const { SlashCommandBuilder } = require("discord.js");
9799

100+
module.exports = {
101+
guildIds: ["the-id-of-my-private-guild"],
102+
data: new SlashCommandBuilder()
103+
.setName("manage-commands")
104+
.setDescription("Replies with Pong!")
105+
.addStringOption((op) =>
106+
op
107+
.setName("command")
108+
.setDescription(
109+
"The command's ID or name to be removed in the given server"
110+
)
111+
)
112+
.addStringOption((op) =>
113+
op
114+
.setName("server-id")
115+
.setDescription("The server's ID to remove the command from")
116+
),
117+
118+
// The function to call whenever the command is executed (Doesnt matter when calling client.deployCommands())
119+
async run(interaction) {
120+
const serverId = ctx.options.getString("server-id");
121+
const command = ctx.options.getString("command");
122+
await interaction.deferReply({ ephemeral: true })
123+
try {
124+
await interaction.client.deleteGuildCommand(command, serverId);
125+
} catch (err) {
126+
console.error("Command not deleted due to", err);
127+
return await interaction.editReply({
128+
embeds: [
129+
new EmbedBuilder()
130+
.setTitle("❌ Command not deleted due to an error")
131+
.setDescription('```' + err + '```')
132+
.setColor(0xee0000);
133+
]
134+
});
135+
}
136+
await interaction.editReply("✅ Command deleted");
137+
},
138+
};
139+
````
98140

99141
#### `logOptions`
100142

@@ -109,7 +151,7 @@ const
109151

110152
### TODO
111153

112-
- [ ] L164 | Add support for sharding
154+
- [ ] L168 | Add support for sharding
113155

114156
- [ ] Finish `this.deleteCommand`
115157

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ module.exports = class cDClient extends Client {
231231
* Delete an application command by its name or ID. **The client needs to be logged in!**
232232
*
233233
* @param {string} command The commands's name or ID | the name will be parsed first
234-
* @param {string} guildId The guild's ID to delete the command in
234+
* @param {string | null} guildId The guild's ID to delete the command in (not needed for a global command)
235235
* @returns {Promise<void>}
236236
*/
237-
async deleteGuildCommand(command, guildId) {
237+
async deleteCommand(command, guildId = null) {
238238
if (!this.isReady()) {
239239
console.error("The client must be logged in!");
240240
return;
241-
} else if (!/^\d+$/i.test(guildId)) {
241+
} else if (guildId && !/^\d+$/i.test(guildId)) {
242242
console.error("The guildId is invalid! Must be a numerous string.");
243243
return;
244244
}
@@ -261,10 +261,7 @@ module.exports = class cDClient extends Client {
261261
return;
262262
}
263263

264-
await this.application.commands.delete(
265-
theCommand.id,
266-
guildId
267-
);
264+
await this.application.commands.delete(theCommand.id, guildId);
268265
}
269266
} catch (err) {
270267
console.error(

node_modules/.package-lock.json

Lines changed: 50 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/discord.js/node_modules/@discordjs/collection/CHANGELOG.md renamed to node_modules/@discordjs/collection/CHANGELOG.md

File renamed without changes.

0 commit comments

Comments
 (0)