-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.js
81 lines (68 loc) · 2.82 KB
/
classes.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const dbHandler = require('../classes/database');
const db = dbHandler.getConnection();
/*
Class Handler
Scans the ./commands folder for .js files to include,
then returns the class information back to the caller
*/
module.exports = {
async getClasses(client) {
var classes = {};
var count = 1; //For fun, let's count how many files we load
var folders = await readdir(`./commands/`);
var triggers = [];
//Prepare to reinsert all of the modules / commands
//This really only needs to happen during production
if(process.env.LOADER_DATABASE !== 'ignore') {
await db.query('TRUNCATE commands');
}
/*
Looping through the folders of /commands
Will not add *.js files yet
*/
for(var i = 0; i < folders.length; i++) {
var folder = folders[i];
var files = await readdir(`./commands/${folders[i]}/`);
for(var x = 0; x < files.length; x++) {
var file = files[x];
if(file.endsWith('.js')) {
var inc = require(`./../commands/${folder}/${file}`);
classes[file] = new inc(client, db);
//Load classes' config info
var config = classes[file].getConfig();
//Some classes *might* not have triggers
if(config.info.trigger) {
//If the trigger is already loaded, tell the console and exit
if(triggers.includes(config.info.trigger)) {
console.error(`${count}> [TRIGGER CONFLICT] Error loading class /commands/${folder}/${file}`);
process.exit(1);
}
if(config.info.aliases) {
for(var a = 0; a < config.info.aliases.length; a++) {
var alias = config.info.aliases[a];
if(triggers.includes(alias)) {
console.error(`${count}> [ALIAS CONFLICT] Error loading class /commands/${folder}/${file}`);
process.exit(1);
}
triggers.push(alias);
}
}
//Add trigger to the trigger list
if(process.env.LOADER_DATABASE !== 'ignore') {
//Wait for the bot to catch up
await db.query(`INSERT INTO commands (module, command, config) VALUES("${folder}", "${file.replace('.js', '')}", "${JSON.stringify(config.info).replace(/\"/ig,'\\"')}")`);
}
triggers.push(config.info.trigger);
}
console.log(`${count}> Loaded class /commands/${folder}/${file}`); //Hooray, tell the world!
count++;
}
}
}
return classes;
}
}