Repository navigation
Expand file tree
/
Copy pathregister-guild-commands.js
More file actions
64 lines (56 loc) · 1.95 KB
/
Copy pathregister-guild-commands.js
File metadata and controls
64 lines (56 loc) · 1.95 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
import 'dotenv/config';
import fs from 'fs/promises';
import path from 'path';
import { pathToFileURL } from 'url';
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v10';
const TOKEN = process.env.BOT_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const GUILD_ID = process.env.GUILD_ID;
if (!TOKEN || !CLIENT_ID || !GUILD_ID) {
console.error('Required env vars: BOT_TOKEN, CLIENT_ID, GUILD_ID');
process.exit(1);
}
const rest = new REST({ version: '10' }).setToken(TOKEN);
async function getAllFiles(dir, fileList = []) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name === 'modules') continue;
await getAllFiles(full, fileList);
} else if (entry.isFile() && entry.name.endsWith('.js')) {
fileList.push(full);
}
}
return fileList;
}
async function loadCommands() {
const commands = [];
const commandsPath = path.join(process.cwd(), 'src', 'commands');
const files = await getAllFiles(commandsPath);
for (const file of files) {
try {
const fileUrl = pathToFileURL(file).href;
const mod = await import(`${fileUrl}`);
const cmd = mod.default || mod;
if (cmd && cmd.data && typeof cmd.data.toJSON === 'function') {
commands.push(cmd.data.toJSON());
}
} catch (err) {
console.error(`Failed to load command file ${file}:`, err);
}
}
return commands;
}
(async () => {
try {
const commands = await loadCommands();
console.log(`Registering ${commands.length} commands to guild ${GUILD_ID}`);
const res = await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands });
console.log('Registration result:', Array.isArray(res) ? `${res.length} commands registered` : res);
} catch (err) {
console.error('Error registering guild commands:', err);
process.exit(1);
}
})();