Skip to content

Commit 67a2fe3

Browse files
committed
feat: add index.js and update main entry point to index.js
1 parent d310919 commit 67a2fe3

4 files changed

Lines changed: 221 additions & 3 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jspm_packages/
2323

2424
# Output directories
2525
dist/
26-
index.js
2726
build/
2827

2928
# IDE specific files

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;
26+
opts.logs = (_a = opts.logs) !== null && _a !== void 0 ? _a : true;
27+
if (!opts.appToken || !opts.appId) {
28+
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
29+
}
30+
let commands = [];
31+
let privateCommands = [];
32+
const commandFiles = readdirSync(folderPath).filter((file) =>
33+
file.endsWith(".js")
34+
);
35+
if (opts.logs)
36+
console.log(`🔁 Started refreshing global and guild commands.`);
37+
try {
38+
const rest = new REST().setToken(opts.appToken);
39+
for (const file of commandFiles) {
40+
const filePath = "file://" + path.join(folderPath, file);
41+
const command = (await import(filePath)).default;
42+
if (typeof command != "object" || !("data" in command)) {
43+
console.error(
44+
`- Command '${command.name}' is missing the 'data' property!`
45+
);
46+
continue;
47+
} else if (
48+
"data" in command &&
49+
Boolean((_b = command.ignore) !== null && _b !== void 0 ? _b : false)
50+
) {
51+
if (opts.logs)
52+
console.log(`- Command '${command.data.name}' is ignored!`);
53+
continue;
54+
}
55+
if ((command.guildIds || []).length > 0) {
56+
privateCommands.push({
57+
data: command.data,
58+
guildIds: command.guildIds,
59+
});
60+
} else {
61+
commands.push(command.data);
62+
}
63+
}
64+
let data = await rest.put(Routes.commands(opts.appId), {
65+
body: commands,
66+
});
67+
if (opts.logs) console.log(`✅ ${data.length} global commands refreshed`);
68+
for (let cmd of privateCommands) {
69+
for (let gid of cmd.guildIds) {
70+
data = null;
71+
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
72+
body: cmd.data,
73+
});
74+
if (opts.logs) console.log(`✅ Guild command '${data.name}' refreshed`);
75+
}
76+
}
77+
return true;
78+
} catch (err) {
79+
console.error("❌ Error while refreshing commands:", err);
80+
return false;
81+
}
82+
}
83+
/**
84+
* Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
85+
*/
86+
export async function deleteCommand(commandId, opts) {
87+
var _a;
88+
const guildId = (_a = opts.guildId) !== null && _a !== void 0 ? _a : null;
89+
const commandPath = guildId
90+
? Routes.guildCommand(opts.appId, guildId, commandId)
91+
: Routes.command(opts.appId, commandId);
92+
if (commandId.match(/^\d+$/i)) {
93+
await new REST({ version: "10" })
94+
.setToken(opts.appToken)
95+
.delete(commandPath);
96+
} else {
97+
throw new Error("'commandId' is not a only-number-string!");
98+
}
99+
return;
100+
}

index.ts

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "djs-command-helper",
33
"version": "4.1.1",
44
"description": "A simple, easy to use module that houses functions to can refresh global and guild specific commands for your Discord App.",
5-
"main": "./index.ts",
5+
"main": "./index.js",
66
"types": "./types.d.ts",
77
"type": "module",
8-
"scripts": { },
8+
"scripts": {},
99
"keywords": [
1010
"discord",
1111
"discord.js",

0 commit comments

Comments
 (0)