-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandler.js
66 lines (58 loc) · 2.43 KB
/
CommandHandler.js
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
const path = require('path');
const fs = require('fs');
const { AdminNames } = require('./config/config.json');
const commands = [];
module.exports = {
initCommands: async function initCommands() {
// Grab all the command folders from the commands directory
const foldersPath = path.join(__dirname, 'Commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('commandName' in command) {
commands.push([command.commandName, command.fileName, command.requireAdmin]);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
},
executeCommand: async function executeCommand(commandNameToExec, channelId, replyId, messageContents, name) {
for(let command of commands){
if (command[0] === commandNameToExec.toLowerCase()) {
exec = require(command[1]);
exec.execute(channelId, replyId, messageContents, name);
}
}
},
checkIfCommandIsRegistered: async function checkIfCommandIsRegistered(commandNameToCheck) {
//check if command exists in cache
for(let command of commands){
if (command[0] === commandNameToCheck.toLowerCase()) {
return true;
}
}
return false;
},
checkIfUserHasPermission: async function checkIfUserHasPermission(commandNameToCheck, name) {
//check if command exists in cache
for(let command of commands){
if (command[0] === commandNameToCheck.toLowerCase()) {
if(command[2] === true) {//check if admin is required
if(AdminNames.includes(name)){
return true;
}
return false;
}
return true;
}
}
return false;
},
};
//get and register all commands