Skip to content

Commit b46f568

Browse files
committed
changed to TS
1 parent 59f0188 commit b46f568

6 files changed

Lines changed: 285 additions & 176 deletions

File tree

index.js

Lines changed: 0 additions & 138 deletions
This file was deleted.

index.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { REST } from "@discordjs/rest";
2+
import { readdirSync } from "node:fs";
3+
import path from "node:path";
4+
import { DeleteOptions, DeployOptions } from "./types";
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+
if (!opts.appToken || !opts.appId) {
36+
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
37+
}
38+
let commands = [];
39+
let privateCommands = [];
40+
41+
const commandFiles = readdirSync(folderPath).filter((file) =>
42+
file.endsWith(".js")
43+
);
44+
45+
if (opts.logs)
46+
console.log(`🔁 Started refreshing global and guild commands.`);
47+
48+
try {
49+
const rest = new REST().setToken(opts.appToken);
50+
51+
for (const file of commandFiles) {
52+
const filePath = path.join(folderPath, file);
53+
const command = require(filePath);
54+
if (!("data" in command)) {
55+
console.error(
56+
`- Command '${command.name}' is missing the 'data' property!`
57+
);
58+
continue;
59+
} else if ("data" in command && Boolean(command.ignore ?? false)) {
60+
if (opts.logs)
61+
console.log(`- Command '${command.data.name}' is ignored!`);
62+
continue;
63+
}
64+
65+
if ((command.guildIds || []).length > 0) {
66+
privateCommands.push({
67+
data: command.data,
68+
guildIds: command.guildIds,
69+
});
70+
} else {
71+
commands.push(command.data);
72+
}
73+
}
74+
75+
let data: any = await rest.put(Routes.commands(opts.appId), {
76+
body: commands,
77+
});
78+
if (opts.logs) console.log(`✅ ${data.length} global commands refreshed`);
79+
80+
for (let cmd of privateCommands) {
81+
for (let gid of cmd.guildIds) {
82+
data = null;
83+
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
84+
body: cmd.data,
85+
});
86+
if (opts.logs) console.log(`✅ Guild command '${data.name}' refreshed`);
87+
}
88+
}
89+
return true;
90+
} catch (err) {
91+
console.error("❌ Error while refreshing commands:", err);
92+
return false;
93+
}
94+
}
95+
96+
/**
97+
* Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
98+
*/
99+
export async function deleteCommand(
100+
commandId: string,
101+
opts: DeleteOptions
102+
): Promise<void> {
103+
const guildId = opts.guildId ?? null;
104+
105+
const commandPath = guildId
106+
? Routes.guildCommand(opts.appId, guildId, commandId)
107+
: Routes.command(opts.appId, commandId);
108+
109+
if (commandId.match(/^\d+$/i)) {
110+
await new REST({ version: "10" })
111+
.setToken(opts.appToken)
112+
.delete(commandPath);
113+
} else {
114+
throw new Error("'commandId' is not a only-number-string!");
115+
}
116+
return;
117+
}

0 commit comments

Comments
 (0)