-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
72 lines (59 loc) · 2.17 KB
/
deploy-commands.js
File metadata and controls
72 lines (59 loc) · 2.17 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
import 'dotenv/config';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { REST, Routes } from 'discord.js';
// Fix __dirname for ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load environment variables
const { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } = process.env;
// Parse flags
const isDev = process.argv.includes('--dev');
const isClear = process.argv.includes('--clear');
// Set up REST client
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);
// Load command files unless clearing
const commands = [];
if (!isClear) {
const commandsDir = path.join(__dirname, 'src', 'commands');
const folder = isDev ? 'dev' : 'global';
const commandsPath = path.join(commandsDir, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const { default: command } = await import(path.join(commandsPath, file));
if (command && command.data) {
commands.push(command.data.toJSON());
}
}
}
async function deploy() {
try {
if (isDev) {
const route = Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID);
if (isClear) {
console.log('🧹 Clearing guild commands...');
await rest.put(route, { body: [] });
console.log(`✅ Cleared all guild commands from GUILD_ID: ${GUILD_ID}`);
} else {
console.log(`🚀 Deploying ${commands.length} guild command(s)...`);
await rest.put(route, { body: commands });
console.log(`✅ Successfully deployed guild commands`);
}
} else {
const route = Routes.applicationCommands(CLIENT_ID);
if (isClear) {
console.log('🧹 Clearing global commands...');
await rest.put(route, { body: [] });
console.log(`✅ Cleared all global commands`);
} else {
console.log(`🌍 Deploying ${commands.length} global command(s)...`);
await rest.put(route, { body: commands });
console.log(`✅ Successfully deployed global commands`);
}
}
} catch (error) {
console.error('❌ Error during deployment:', error);
}
}
deploy();