Uwev2/src/bot.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-11-19 22:03:13 +00:00
import { logger } from "./inc/log.js";
2017-11-20 16:30:20 +00:00
import { read, cfg } from "./inc/cfg.js";
2017-11-18 10:53:58 +00:00
import { wrapper, clients } from "./inc/wrapper.js";
2017-11-20 16:30:20 +00:00
const fs = require("fs");
2017-11-07 17:22:41 +00:00
read().then(() => {
2017-11-08 11:56:04 +00:00
let bot = new wrapper();
2017-11-20 16:30:20 +00:00
const self = {
2017-11-26 23:00:54 +00:00
_trigger: new Map(),
trigger: trigger
2017-11-20 16:30:20 +00:00
};
2017-11-26 23:09:41 +00:00
fs.readdirSync(`${__dirname}/inc/trigger/`).forEach(file => {
2017-11-26 16:13:57 +00:00
if(file.substr(-3, 3) === ".js") {
logger.info(`(main) loading trigger: ${file}`);
require(`${__dirname}/inc/trigger/${file}`)(self);
2017-11-20 16:30:20 +00:00
}
2017-11-26 23:09:41 +00:00
});
2017-11-20 16:30:20 +00:00
2017-11-18 10:53:58 +00:00
bot.on("message", e => { // Todo: eventhandler
2017-11-20 16:30:20 +00:00
for (var [name, trigger] of self._trigger.entries()) {
if (!trigger.call.exec(e.message))
continue;
if (!trigger.clients.includes(e.type))
continue;
2017-11-08 21:29:43 +00:00
2017-11-20 16:30:20 +00:00
let active = false;
2017-11-22 04:44:02 +00:00
if (e.type === "irc" && cfg.trigger[e.network + e.channel]) {
2017-11-20 16:30:20 +00:00
if (cfg.trigger[e.network + e.channel][trigger.name])
active = true;
2017-11-09 14:34:21 +00:00
}
2017-11-20 16:30:20 +00:00
else
active = trigger.active;
if (!active)
continue;
2017-11-25 12:31:26 +00:00
if (trigger.level > e.user.level.level) {
e.reply(`no permission, min level ${trigger.level} required`);
break;
}
2017-11-26 23:00:54 +00:00
2017-11-20 16:30:20 +00:00
trigger.f(e);
2017-11-10 09:21:38 +00:00
}
2017-11-19 22:03:13 +00:00
logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`);
2017-11-08 11:56:04 +00:00
});
2017-11-19 22:03:13 +00:00
2017-11-18 10:53:58 +00:00
bot.on("ctcp:version", e => {
e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`);
2017-11-09 14:34:21 +00:00
});
2017-11-26 23:00:54 +00:00
});
2017-11-26 23:09:41 +00:00
function trigger(args) {
2017-11-26 23:00:54 +00:00
this.call = args.call;
this.level = args.level || 0;
2017-11-26 23:09:41 +00:00
this.active = args.hasOwnProperty("active") ? args.active : true;
2017-11-26 23:00:54 +00:00
this.clients = args.clients || ["irc", "tg"];
this.f = args.f;
}