-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (82 loc) · 3.17 KB
/
app.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
82
83
84
85
86
87
88
const appDir = process.cwd();
const functions = require(appDir + '/lib/globalFunctions.js');
const settings = require(appDir + '/settings.json');
const Discord = require('discord.js');
const client = new Discord.Client();
client.mbVersion = '0.4';
const fs = require('fs');
require(appDir + '/util/eventLoader.js')(client);
functions.log('------------------------------------------------------');
functions.log(` Starting MediaButler Discord Bot v${client.mbVersion}`);
functions.log('------------------------------------------------------');
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir('./commands/', (err, files) => {
if (err) {
// Can't find ./commands/ folder. Log to console and quit
functions.log(err, "RED"); //log error to console
process.exit(1); //exit app
}
functions.log(`Found total of ${files.length} modules to load`);
if (files.length >= 1) {
//Run through each command folder and load into memory
files.forEach(f => {
const props = require(`./commands/${f}`);
if (props.conf.enabled) {
functions.log(` Loading command: ${props.help.name}.`);
if (props.start) props.start(client);
client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
})
}
else {
functions.log(` ${props.help.name}: Command disabled, Skipping..`);
}
});
functions.log(' Finished loading modules, loading sub commands..');
}
else {
// No Commands found. Log to console and quit
functions.log('No commands to load, quitting out', "RED"); // Log error to console
process.exit(1); // exit app
}
});
client.reload = (command) => {
const p = new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`${appDir}/commands/${command}`)];
const cmd = require(`${appDir}/commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if (cmd === command) client.aliases.delete(alias);
});
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (err) {
reject(err);
}
});
return p;
};
client.elevation = (message) => {
let permlvl = 0;
if (message.member.hasPermission('MANAGE_MESSAGES')) permlvl = 2;
if (message.member.hasPermission('ADMINISTRATOR')) permlvl = 3;
if (message.author.id == message.guild.ownerID) permlvl = 4;
return permlvl;
}
functions.log('Connecting to discord...');
client.login(settings.token);
process.on('SIGINT', () => {
functions.log('Gracefully shutting down from SIGINT (CTRL + C)', 'RED');
functions.log('Logging out of discord...');
client.destroy()
.then(() => {
functions.log('Shutting down. Bye!');
process.exit(0);
});
});