-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (58 loc) · 1.79 KB
/
index.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
const logger = require('./logger.js');
logger.warn('Initializing...');
const opii = {};
opii.Discord = require('discord.js');
opii.client = new opii.Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
logger.warn('Loading config...');
require('dotenv').config()
logger.warn('Loading events...');
require('./events.js')(opii, logger);
opii.commands = {};
opii.registerCommand = function (name, data) {
opii.commands[name] = {
desc: data.desc,
cat: data.cat,
run: data.run,
restricted: data.restricted,
};
};
const loadCommand = async (file) => {
return new Promise((resolve) => {
let cmdName = file.replace(/\.[^/.]+$/, '');
if (cmdName.startsWith('_')) return resolve();
try {
let data = require(`./cmds/${cmdName}`);
opii.registerCommand(cmdName, data);
// logger.info(`Command '${cmdName}' loaded.`);
}
catch (err) {
logger.error(`Command ${file} failed to load:\n${err.stack}`);
return resolve();
}
resolve();
});
};
const loadCommands = async () => {
return new Promise((resolve) => {
let fs = require('fs');
fs.readdir('./cmds/', async (err, files) => {
if (err) {
logger.error('FS errored:', err);
return resolve();
}
files.forEach(async (file) => await loadCommand(file));
logger.info(`${Object.keys(opii.commands).length} command(s) loaded.`);
resolve();
});
});
};
opii.colorutils = require('./colorutils.js');
opii.isAdmin = id => process.env.BOT_ADMINS.split(",").includes(id);
(async () => {
logger.warn('Loading commands...');
await loadCommands();
logger.warn('Attempting to log in...');
opii.client.login(process.env.DISCORD_TOKEN);
})();
process.on('uncaughtException', (err) => logger.error('Uncaught exception:', err));
process.on('unhandledRejection', (err, promise) => logger.error('Unhandled rejection: ', promise));