-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
88 lines (76 loc) · 3.66 KB
/
deploy-commands.js
File metadata and controls
88 lines (76 loc) · 3.66 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
const { REST, Routes } = require('discord.js');
require('dotenv').config();
const { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } = process.env;
const fs = require('node:fs');
const path = require('node:path');
const commands = [];
const commandDirectories = ['commands']; // for multiple folder loading use ['folder1' 'folder2'] but issue #5 is still a problem
const commandNames = new Set(); // Store unique command names for testing not loading server/serverinfo.js
const duplicateNames = new Set(); // Store duplicate names for testing not loading server/serverinfo.js
// Function to read commands recursively
function loadCommandsFromDirectory(directory) {
const dirPath = path.join(__dirname, directory);
if (!fs.existsSync(dirPath)) return;
const files = fs.readdirSync(dirPath, { withFileTypes: true });
for (const file of files) {
const filePath = path.join(dirPath, file.name);
if (file.isDirectory()) {
loadCommandsFromDirectory(path.join(directory, file.name)); // Recursively search subfolders
} else if (file.name.endsWith('.js')) {
const command = require(filePath);
if ('data' in command && 'execute' in command) {
const commandName = command.data.name;
//console.log(`✅ Command found in: ${filePath} with ${command.data.name}`)
// Check for duplicate command names
if (commandNames.has(commandName)) {
console.log(`⚠️ Duplicate command found: /${commandName} (in file: ${filePath})`);
duplicateNames.add(commandName);
} else {
commandNames.add(commandName);
console.log(`✅ Loaded command from: ${filePath}`);
const json = command.data.toJSON();
if ( typeof json.name === 'string' && typeof json.description === 'string' && json.name.length <= 32 && json.description.length <= 100) {
commands.push(json);
} else {
console.warn(`⚠️ Skipping invalid command at ${filePath} - name/description invalid`);
}
}
} else {
console.log(`⚠️ [WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
}
// Load all commands from specified directories
for (const directory of commandDirectories) {
loadCommandsFromDirectory(directory);
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(DISCORD_TOKEN);
// Deploy the commands
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
let data;
// Routes.applicationCommands(CLIENT_ID)
try {
data = await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.timeEnd('register');
console.log(`✅ Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.timeEnd('register');
console.error('❌ Error registering commands:');
if (error?.response?.data) {
console.dir(error.response.data, { depth: null });
} else {
console.error(error);
}
}
console.log(`✅ Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(`❌ Error deploying commands:`, error);
}
})();