Uwev2/src/bot.mjs
2018-02-27 09:24:32 +01:00

73 lines
1.8 KiB
JavaScript

import { logger } from "./inc/log";
import { read, cfg } from "./inc/cfg";
import { wrapper } from "./inc/wrapper";
import triggers from "./inc/trigger/main";
read().then(() => {
let bot = new wrapper();
const self = {
_trigger: new Map(),
trigger: trigger
};
triggers.forEach(mod => mod(self));
bot.on("message", e => {
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 => {
const versions = [
"AmIRC.1 (8 Bit) for Commodore Amiga 500",
"HexChat 0.72 [x86] / Windows 95c [500MHz]"
];
e.write(`notice ${e.user.nick} :\u0001VERSION ${versions[~~(Math.random() * versions.length)]}\u0001`);
});
})
.catch(err => {
logger.error(`(main) ${err.message}`);
});
function trigger(args) {
this.call = args.call;
this.help = args.help || false;
this.level = args.level || 0;
this.active = args.hasOwnProperty("active") ? args.active : true;
this.clients = args.clients || ["irc", "tg", "discord"];
this.f = args.f;
}
const parseArgs = (msg) => {
let args = msg.trim().split(" ");
let cmd = args.shift();
return {
cmd: cmd.replace(/^(\.|\/|\!)/, ""),
args: args
};
};