-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-comands.js
62 lines (54 loc) · 1.76 KB
/
deploy-comands.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
import { REST, Routes } from "discord.js";
import config from "./config.json" assert { type: "json" };
import fs from "node:fs/promises";
import path from "node:path";
async function loadCommands(foldersPath) {
const commands = [];
const commandFoldersOrFiles = await fs.readdir(foldersPath);
for (const entry of commandFoldersOrFiles) {
const entryPath = path.join(foldersPath, entry);
let commandFiles;
const isDirectory = (await fs.stat(entryPath)).isDirectory();
if (isDirectory) {
commandFiles = (await fs.readdir(entryPath)).filter((file) =>
file.endsWith(".js")
);
commandFiles = commandFiles.map((file) => path.join(entryPath, file));
} else if (entry.endsWith(".js")) {
commandFiles = [entryPath];
} else {
continue;
}
for (const filePath of commandFiles) {
try {
const { default: command } = await import(`file://${filePath}`);
command.data && commands.push(command.data.toJSON());
} catch (error) {
console.error(`Error loading command ${filePath}:`, error);
}
}
}
return commands;
}
async function refreshCommands(commands) {
const rest = new REST({ version: "10" }).setToken(config.token);
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
const data = await rest.put(
Routes.applicationGuildCommands(config.clientId, config.guild),
{ body: commands }
);
console.log(
`Successfully reloaded ${data.length} application (/) commands.`
);
} catch (error) {
console.error(error);
}
}
(async () => {
const foldersPath = path.join(path.resolve(), "src/commands");
const commands = await loadCommands(foldersPath);
await refreshCommands(commands);
})();