forked from Spiderjockey02/Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
86 lines (74 loc) · 2.54 KB
/
bot.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
// Dependencies
const Client = require('./base/Egglord.js');
require('./structures');
const bot = new Client(),
{ promisify } = require('util'),
readdir = promisify(require('fs').readdir),
path = require('path');
// Load commands
(async () => {
// load commands
await loadCommands();
// load events
await loadEvents();
// load translations
bot.translations = await require('./helpers/LanguageManager')();
// Connect bot to database
bot.mongoose.init(bot);
// load up adult site block list
bot.fetchAdultSiteList();
// Connect bot to discord API
const token = bot.config.token;
bot.login(token).catch(e => bot.logger.error(e.message));
})();
// load commands
async function loadCommands() {
const cmdFolders = (await readdir('./src/commands/')).filter((v, i, a) => a.indexOf(v) === i);
bot.logger.log('=-=-=-=-=-=-=- Loading command(s): 137 -=-=-=-=-=-=-=');
// loop through each category
cmdFolders.forEach(async (dir) => {
if (bot.config.disabledPlugins.includes(dir) || dir == 'command.example.js') return;
const commands = (await readdir('./src/commands/' + dir + '/')).filter((v, i, a) => a.indexOf(v) === i);
// loop through each command in the category
commands.forEach((file) => {
if (bot.config.disabledCommands.includes(file.replace('.js', ''))) return;
try {
const cmd = new (require(`./commands/${dir}/${file}`))(bot);
cmd.load(bot);
} catch (err) {
if (bot.config.debug) console.log(err);
bot.logger.error(`Unable to load command ${file}: ${err}`);
}
});
});
}
// load events
async function loadEvents() {
const evtFolder = await readdir('./src/events/');
bot.logger.log('=-=-=-=-=-=-=- Loading events(s): 44 -=-=-=-=-=-=-=');
evtFolder.forEach(async folder => {
const folders = await readdir('./src/events/' + folder + '/');
folders.forEach(async file => {
delete require.cache[file];
const { name } = path.parse(file);
try {
const event = new (require(`./events/${folder}/${file}`))(bot, name);
bot.logger.log(`Loading Event: ${name}`);
// Make sure the right manager gets the event
if (event.conf.child) {
bot[event.conf.child].on(name, (...args) => event.run(bot, ...args));
} else {
bot.on(name, (...args) => event.run(bot, ...args));
}
} catch (err) {
bot.logger.error(`Failed to load Event: ${name} error: ${err.message}`);
}
});
});
}
// handle unhandledRejection errors
process.on('unhandledRejection', err => {
bot.logger.error(`Unhandled promise rejection: ${err.message}.`);
// show full error if debug mode is on
console.log(err);
});