-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
128 lines (112 loc) · 4.75 KB
/
deploy-commands.js
File metadata and controls
128 lines (112 loc) · 4.75 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { REST, Routes } from 'discord.js';
import dotenv from 'dotenv';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { promises as fsPromises } from 'fs';
dotenv.config();
const { BOT_TOKEN, CLIENT_ID, GUILD_ID } = process.env;
if (!BOT_TOKEN) {
console.error("❌ BOT_TOKEN is not set in .env. Commands cannot be deployed.");
process.exit(1);
}
if (!CLIENT_ID) {
console.error("❌ CLIENT_ID is not set in .env. Commands cannot be deployed.");
process.exit(1);
}
const commands = [];
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const commandsPath = path.join(__dirname, 'src', 'commands', 'slash');
async function loadCommands() {
const commandFiles = await fsPromises.readdir(commandsPath).catch(e => {
console.error(`Error reading commands directory ${commandsPath}:`, e);
return [];
});
for (const file of commandFiles) {
if (!file.endsWith('.js')) continue;
const filePath = path.join(commandsPath, file);
try {
const command = await import(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} catch (error) {
console.error(`Error loading command from ${filePath}:`, error);
}
}
}
const rest = new REST({ version: '10' }).setToken(BOT_TOKEN);
async function deployGuildCommands() {
if (!GUILD_ID) {
console.error("❌ GUILD_ID is not set in .env. Guild commands cannot be deployed.");
process.exit(1);
}
await loadCommands();
try {
console.log(`Started refreshing ${commands.length} application (/) commands for guild ${GUILD_ID}.`);
const data = await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands for guild ${GUILD_ID}.`);
if (data.length > 0) {
console.log("✅ Slash commands deployed to this guild.");
} else {
console.log("⚠️ No slash commands were deployed (commands array is empty).");
}
} catch (error) {
console.error('Error deploying guild commands:', error);
}
}
async function deployGlobalCommands() {
await loadCommands();
try {
console.log(`Started refreshing ${commands.length} application (/) commands globally.`);
const data = await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands globally.`);
if (data.length > 0) {
console.log("✅ Slash commands deployed globally.");
} else {
console.log("⚠️ No slash commands were deployed globally (commands array is empty).");
}
} catch (error) {
console.error('Error deploying global commands:', error);
}
}
async function removeAllCommands() {
try {
console.log('Removing all global commands...');
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: [] });
console.log('✅ All global commands removed.');
if (GUILD_ID) {
console.log(`Removing all commands in guild ${GUILD_ID}...`);
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: [] });
console.log(`✅ All guild commands removed from guild ${GUILD_ID}.`);
} else {
console.log('⚠️ No GUILD_ID set, skipping guild commands removal.');
}
} catch (error) {
console.error('Error removing commands:', error);
}
}
const args = process.argv.slice(2);
if (args.includes('--guild')) {
deployGuildCommands();
} else if (args.includes('--global')) {
deployGlobalCommands();
} else if(args.includes('--remove-slash')) {
await removeAllCommands();
}
else {
console.log("Usage: node deploy-commands.js [--guild | --global]");
console.log(" --guild: Deploy commands to the guild specified by GUILD_ID in .env (for testing)");
console.log(" --global: Deploy commands globally (for production, takes up to 1 hour to propagate)");
console.log("\nTo deploy commands, ensure the 'commands' array in this file contains the commands you want, then run:");
console.log(" node deploy-commands.js --guild (to deploy to your test guild)");
console.log(" node deploy-commands.js --global (to deploy globally)");
}