79 lines
2 KiB
JavaScript
79 lines
2 KiB
JavaScript
import { createRequire } from 'module';
|
|
const require = createRequire(import.meta.url);
|
|
|
|
import Eris from 'eris';
|
|
import Logger, { levels } from './logger.js';
|
|
import CommandParser, { Command } from './parser.js';
|
|
import { filename, dirname } from './utils.js';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
|
|
const cfg = require('./config.json');
|
|
const dir = dirname(import.meta.url);
|
|
|
|
const bot = new Eris(cfg.token);
|
|
|
|
global.ctx = {
|
|
bot: bot,
|
|
log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN,
|
|
whitelist: cfg.user_whitelist || [],
|
|
set_ctx: (key, value) => {
|
|
global.ctx[key] = value;
|
|
},
|
|
};
|
|
const log = new Logger(filename(import.meta.url), global.ctx.log_level);
|
|
const parse = new CommandParser(global.ctx);
|
|
|
|
const checkGuild = (guild) => {
|
|
if (!cfg.whitelist.includes(guild.id)) {
|
|
log.info(`Leaving guild not on whitelist: ${guild.name}`);
|
|
guild.leave();
|
|
}
|
|
};
|
|
|
|
bot.on('ready', () => {
|
|
log.info('ready recieved.');
|
|
bot.guilds.forEach((guild) => checkGuild(guild));
|
|
});
|
|
|
|
bot.on('guildAvaliable', (guild) => checkGuild(guild));
|
|
|
|
bot.on('messageCreate', (msg) => parse.parseMsg(msg, global.ctx));
|
|
|
|
bot.on('error', (err) => log.error(err.toString()));
|
|
|
|
bot.connect();
|
|
|
|
async function load_commands() {
|
|
for (let file of files) {
|
|
let p = path.join(cmd_dir, file);
|
|
log.debug(p);
|
|
let obj;
|
|
try {
|
|
obj = await import('file:////' + p);
|
|
} catch (e) {
|
|
log.warn(`loading file ${file}, ran into issue: ${e.message}`);
|
|
continue;
|
|
}
|
|
if (obj.default != undefined) {
|
|
if (obj.default.constructor.name == 'CommandInitializer') {
|
|
obj.default.initialize(global.ctx);
|
|
let cmds = obj.default.getCommands();
|
|
if (parse.isCmd(cmds)) {
|
|
parse.addCommand(cmds);
|
|
} else if (cmds.constructor.name == 'Array') {
|
|
for (let cmd of cmds) {
|
|
parse.addCommand(cmd);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
log.warn('module ' + file + ' returned an undefined module.');
|
|
}
|
|
}
|
|
}
|
|
|
|
let cmd_dir = path.join(dir, 'cmd');
|
|
log.debug(dir);
|
|
let files = fs.readdirSync(cmd_dir);
|
|
load_commands();
|