Skip to content

Commit 2fb1f24

Browse files
committed
chore(i18n): add script to register commands to test guild
1 parent 60ced6c commit 2fb1f24

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

scripts/register-guild-commands.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'dotenv/config';
2+
import fs from 'fs/promises';
3+
import path from 'path';
4+
import { pathToFileURL } from 'url';
5+
import { REST } from '@discordjs/rest';
6+
import { Routes } from 'discord-api-types/v10';
7+
8+
const TOKEN = process.env.BOT_TOKEN;
9+
const CLIENT_ID = process.env.CLIENT_ID;
10+
const GUILD_ID = process.env.GUILD_ID;
11+
12+
if (!TOKEN || !CLIENT_ID || !GUILD_ID) {
13+
console.error('Required env vars: BOT_TOKEN, CLIENT_ID, GUILD_ID');
14+
process.exit(1);
15+
}
16+
17+
const rest = new REST({ version: '10' }).setToken(TOKEN);
18+
19+
async function getAllFiles(dir, fileList = []) {
20+
const entries = await fs.readdir(dir, { withFileTypes: true });
21+
for (const entry of entries) {
22+
const full = path.join(dir, entry.name);
23+
if (entry.isDirectory()) {
24+
if (entry.name === 'modules') continue;
25+
await getAllFiles(full, fileList);
26+
} else if (entry.isFile() && entry.name.endsWith('.js')) {
27+
fileList.push(full);
28+
}
29+
}
30+
return fileList;
31+
}
32+
33+
async function loadCommands() {
34+
const commands = [];
35+
const commandsPath = path.join(process.cwd(), 'src', 'commands');
36+
const files = await getAllFiles(commandsPath);
37+
38+
for (const file of files) {
39+
try {
40+
const fileUrl = pathToFileURL(file).href;
41+
const mod = await import(`${fileUrl}`);
42+
const cmd = mod.default || mod;
43+
if (cmd && cmd.data && typeof cmd.data.toJSON === 'function') {
44+
commands.push(cmd.data.toJSON());
45+
}
46+
} catch (err) {
47+
console.error(`Failed to load command file ${file}:`, err);
48+
}
49+
}
50+
51+
return commands;
52+
}
53+
54+
(async () => {
55+
try {
56+
const commands = await loadCommands();
57+
console.log(`Registering ${commands.length} commands to guild ${GUILD_ID}`);
58+
const res = await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands });
59+
console.log('Registration result:', Array.isArray(res) ? `${res.length} commands registered` : res);
60+
} catch (err) {
61+
console.error('Error registering guild commands:', err);
62+
process.exit(1);
63+
}
64+
})();

0 commit comments

Comments
 (0)