-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
84 lines (74 loc) · 2.22 KB
/
Copy pathdeploy-commands.js
File metadata and controls
84 lines (74 loc) · 2.22 KB
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
// Imports
const { Routes, PermissionsBitField } = require("discord.js");
const { clientId, token } = require("./config.json");
const fs = require("fs");
const path = require("path");
const Logger = require("./utils/Logger");
// Commands List
async function getCommands() {
const commands = [];
const foldersPath = path.join(__dirname, "commands");
const commandCategories = fs.readdirSync(foldersPath);
for (const category of commandCategories) {
const categoryPath = path.join(foldersPath, category);
const categoryFolders = fs.readdirSync(categoryPath);
for (const folder of categoryFolders) {
const commandFiles = fs
.readdirSync(path.join(categoryPath, folder))
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(categoryPath, folder, file);
const command = require(filePath);
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
Logger.error(
"DeployCommands",
`${filePath} missing data or execute function`
);
Logger.wait("DeployCommands", "Building commands...");
}
}
}
}
return commands;
}
// Update Commands
async function deployCommands() {
return new Promise(async (resolve) => {
Logger.wait("DeployCommands", "Building commands...");
const commands = await getCommands();
Logger.ok(
"DeployCommands",
`${commands.length} command built `
);
Logger.wait("DeployCommands", "Deploying commands...");
const { REST } = require("@discordjs/rest");
const rest = new REST({ timeout: 60000 }).setToken(token);
try {
const data = await rest.put(
Routes.applicationCommands(clientId),
{
body: commands,
}
);
Logger.ok(
"DeployCommands",
`${data.length} command saved `
);
resolve();
} catch (error) {
// Error
Logger.error(
"DeployCommands",
"An error occurred while deploying commands"
);
Logger.error("DeployCommands", error);
process.exit(1);
}
});
}
// Export
module.exports = {
deployCommands,
};