Uwev2/src/bot.js
2017-11-27 01:29:09 +01:00

69 lines
1.8 KiB
JavaScript

import { logger } from "./inc/log.js";
import { read, cfg } from "./inc/cfg.js";
import { wrapper, clients } from "./inc/wrapper.js";
const fs = require("fs");
read().then(() => {
let bot = new wrapper();
const self = {
_trigger: new Map(),
trigger: trigger
};
fs.readdirSync(`${__dirname}/inc/trigger/`).forEach(file => {
if(file.substr(-3, 3) === ".js") {
logger.info(`(main) loading trigger: ${file}`);
require(`${__dirname}/inc/trigger/${file}`)(self);
}
});
bot.on("message", e => { // Todo: eventhandler
for (var [name, trigger] of self._trigger.entries()) {
if (!trigger.call.exec(e.message))
continue;
if (!trigger.clients.includes(e.type))
continue;
let active = false;
if (e.type === "irc" && cfg.trigger[e.network + e.channel]) {
if (cfg.trigger[e.network + e.channel][trigger.name])
active = true;
}
else
active = trigger.active;
if (!active)
continue;
if (trigger.level > e.user.level.level) {
e.reply(`no permission, min level ${trigger.level} required`);
break;
}
e = Object.assign(e, parseArgs(e.message));
trigger.f(e);
}
logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`);
});
bot.on("ctcp:version", e => {
e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`);
});
});
function trigger(args) {
this.call = args.call;
this.level = args.level || 0;
this.active = args.hasOwnProperty("active") ? args.active : true;
this.clients = args.clients || ["irc", "tg"];
this.f = args.f;
}
const parseArgs = (msg) => {
let args = msg.trim().split(" ");
let cmd = args.shift();
return {
cmd: cmd.replace(/^(\.|\/|\!)/, ""),
args: args
};
};