Skip to content

Commit a87c0f2

Browse files
committed
feat: add command deployment and deletion functionality
1 parent b2266f3 commit a87c0f2

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ jspm_packages/
2121
# Optional eslint cache
2222
.eslintcache
2323

24-
# Output directories
25-
dist/
26-
build/
27-
2824
# IDE specific files
2925
.vscode/
3026
.idea/

dist/index.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { REST } from "@discordjs/rest";
2+
import { readdirSync } from "node:fs";
3+
import path from "node:path";
4+
const Routes = {
5+
commands: (appId) => {
6+
return `/applications/${appId}/commands`;
7+
},
8+
command: (appId, cmdId) => {
9+
return `/applications/${appId}/commands/${cmdId}`;
10+
},
11+
guildCommands: (appId, guildId) => {
12+
return `/applications/${appId}/guilds/${guildId}/commands`;
13+
},
14+
guildCommand: (appId, guildId, cmdId) => {
15+
return `/applications/${appId}/guilds/${guildId}/commands/${cmdId}`;
16+
},
17+
};
18+
/**
19+
* Create, update and delete global and guild application commands.
20+
*
21+
* To update guild-specific commands correctly, make sure the bot is logged in.\
22+
* Otherwise the check for a guild ID is omitted, and you could make pointless requests which can also result in an error
23+
*/
24+
export async function deployCommands(folderPath, opts) {
25+
var _a, _b, _c;
26+
opts.logs = (_a = opts.logs) !== null && _a !== void 0 ? _a : true;
27+
const FILE_EXTENSION = (_b = opts.fileExtension) !== null && _b !== void 0 ? _b : ".js";
28+
if (!opts.appToken || !opts.appId) {
29+
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
30+
}
31+
let commands = [];
32+
let privateCommands = [];
33+
const commandFiles = readdirSync(folderPath).filter((file) => file.endsWith(FILE_EXTENSION));
34+
if (opts.logs)
35+
console.log(`🔁 Started refreshing global and guild commands.`);
36+
try {
37+
const rest = new REST().setToken(opts.appToken);
38+
for (const file of commandFiles) {
39+
const filePath = "file://" + path.join(folderPath, file);
40+
const command = (await import(filePath)).default;
41+
if (typeof command != "object" || !("data" in command)) {
42+
console.error(`- Command '${command.name}' is missing the 'data' property!`);
43+
continue;
44+
}
45+
else if ("data" in command && Boolean((_c = command.ignore) !== null && _c !== void 0 ? _c : false)) {
46+
if (opts.logs)
47+
console.log(`- Command '${command.data.name}' is ignored!`);
48+
continue;
49+
}
50+
if ((command.guildIds || []).length > 0) {
51+
privateCommands.push({
52+
data: command.data,
53+
guildIds: command.guildIds,
54+
});
55+
}
56+
else {
57+
commands.push(command.data);
58+
}
59+
}
60+
let data = await rest.put(Routes.commands(opts.appId), {
61+
body: commands,
62+
});
63+
if (opts.logs)
64+
console.log(`✅ ${data.length} global commands refreshed`);
65+
for (let cmd of privateCommands) {
66+
for (let gid of cmd.guildIds) {
67+
data = null;
68+
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
69+
body: cmd.data,
70+
});
71+
if (opts.logs)
72+
console.log(`✅ Guild command '${data.name}' refreshed`);
73+
}
74+
}
75+
return true;
76+
}
77+
catch (err) {
78+
console.error("❌ Error while refreshing commands:", err);
79+
return false;
80+
}
81+
}
82+
/**
83+
* Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
84+
*/
85+
export async function deleteCommand(commandId, opts) {
86+
var _a;
87+
const guildId = (_a = opts.guildId) !== null && _a !== void 0 ? _a : null;
88+
const commandPath = guildId
89+
? Routes.guildCommand(opts.appId, guildId, commandId)
90+
: Routes.command(opts.appId, commandId);
91+
if (commandId.match(/^\d+$/i)) {
92+
await new REST({ version: "10" })
93+
.setToken(opts.appToken)
94+
.delete(commandPath);
95+
}
96+
else {
97+
throw new Error("'commandId' is not a only-number-string!");
98+
}
99+
return;
100+
}

package-lock.json

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

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-helper",
3-
"version": "4.2.0",
3+
"version": "4.2.1",
44
"description": "A simple, easy to use module that houses functions to can refresh global and guild specific commands for your Discord App.",
55
"main": "./dist/index.js",
66
"types": "./types.d.ts",

0 commit comments

Comments
 (0)