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