outsourced trigger

This commit is contained in:
Flummi 2017-11-20 17:30:20 +01:00
parent 98da8051f0
commit 0d3d5f7c8a
6 changed files with 120 additions and 48 deletions

View File

@ -1,55 +1,45 @@
import { logger } from "./inc/log.js"; import { logger } from "./inc/log.js";
import { read } from "./inc/cfg.js"; import { read, cfg } from "./inc/cfg.js";
import { wrapper, clients } from "./inc/wrapper.js"; import { wrapper, clients } from "./inc/wrapper.js";
const vm = require("vm"); const fs = require("fs");
read().then(() => { read().then(() => {
let bot = new wrapper(); let bot = new wrapper();
bot.on("message", e => { // Todo: eventhandler
let orig = e.message;
let tmp = orig.split(" ");
tmp.shift();
let args = tmp;
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
if (e.message.match(/^\.js /)) { // JS-Sandbox const self = {
args = e.message.substring(3); logger: logger,
const context = { cfg: cfg,
e: e, wrapper: wrapper,
c: clients clients: clients,
_trigger: new Map()
}; };
try {
let output = vm.runInNewContext(args, context); const files = fs.readdirSync(`${__dirname}/inc/trigger/`);
if(typeof output !== undefined && output) { for (let file in files) {
output = JSON.stringify(output); if (files[file].substr(-3, 3) === ".js") {
if (output !== "Converting circular structure to JSON") { logger.info(`(main) loading trigger: ${files[file]}`);
e.reply(output.length > 400 ? `holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)` : output); require(`${__dirname}/inc/trigger/${files[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] !== undefined) {
if (cfg.trigger[e.network + e.channel][trigger.name])
active = true;
} }
catch (err) { else
e.reply(err.message); active = trigger.active;
} if (!active)
} continue;
else if (e.message.match(/^\.kaffee/)) {
var Texte = [ trigger.f(e);
`serviert ${args.join(" ")} einen frisch gebrühten Kaffee aus aromatisch hochwertigen und laktosefreien Kaffeebohnen, die nach dem Vorbild der kolonisierten Ausbeutung herangewachsen und importiert worden sind`,
`serviert ${args.join(" ")} einen frisch gebrühten Kaffee aus aromatisch minderwertigen Kaffeebohnen, die zu einem überhöhten Preis und nach dem Prinzip des Fairtrade® herangewachsen und importiert worden sind`
];
e.replyAction(`${Texte[~~((Math.random() * Texte.length))]}; Prost!`);
}
else if (e.message.match(/^\.abschieben/)) {
let Dest = [
"nach Syrien", "in die Ukraine", "nach Timbuktu", "nach Uruguay",
"nach Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
"nach Bagdad", "nach Nordkorea", "nach Kurdistan", "nach Bayern",
"nach Japan", "nach Irak", "in den Iran", "nach Saudi-Arabien",
"nach Libyen", "nach Niger", "nach Uganda", "nach Afghanistan"
];
e.replyAction(`schiebt ${args[0]} ${Dest[~~(Math.random() * Dest.length)]} ab.`);
}
else if (e.message.match(/^\.butterkäse/)) {
e.replyAction(`drischt mit einem großen Stück Butterkäse auf ${args[0]} ein.`);
} }
logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`); logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`);
}); });

View File

@ -24,10 +24,8 @@ class tg {
id: msg.from.id id: msg.from.id
}); });
if (msg.date >= (~~(Date.now() / 1000) - 10) && msg.text !== undefined) { if (msg.date >= (~~(Date.now() / 1000) - 10) && msg.text !== undefined)
msg.text = msg.text.replace(/^\//g, ".");
this.emit("data", ["message", this.reply(msg)]); this.emit("data", ["message", this.reply(msg)]);
}
}); });
} }
send(id, msg) { send(id, msg) {

View File

@ -0,0 +1,22 @@
const Dest = [
"nach Syrien", "in die Ukraine", "nach Timbuktu", "nach Uruguay",
"nach Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
"nach Bagdad", "nach Nordkorea", "nach Kurdistan", "nach Bayern",
"nach Japan", "nach Irak", "in den Iran", "nach Saudi-Arabien",
"nach Libyen", "nach Niger", "nach Uganda", "nach Afghanistan"
];
module.exports = bot => {
bot._trigger.set("abschieben", {
call: /^(.|\/)abschieben/i,
level: 0,
active: true,
clients: ["irc", "tg"],
f: e => {
let args = e.message.trim().split(" ");
args.shift();
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
e.replyAction(`schiebt ${args[0]} ${Dest[~~(Math.random() * Dest.length)]} ab.`);
}
});
};

View File

@ -0,0 +1,14 @@
module.exports = bot => {
bot._trigger.set("butterkaese", {
call: /^(.|\/)butterkäse/i,
level: 0,
active: true,
clients: ["irc", "tg"],
f: e => {
let args = e.message.trim().split(" ");
args.shift();
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
e.replyAction(`drischt mit einem großen Stück Butterkäse auf ${args[0]} ein.`);
}
});
};

19
src/inc/trigger/kaffee.js Normal file
View File

@ -0,0 +1,19 @@
const texte = [
`einen frisch gebrühten Kaffee aus aromatisch hochwertigen und laktosefreien Kaffeebohnen, die nach dem Vorbild der kolonisierten Ausbeutung herangewachsen und importiert worden sind`,
`einen frisch gebrühten Kaffee aus aromatisch minderwertigen Kaffeebohnen, die zu einem überhöhten Preis und nach dem Prinzip des Fairtrade® herangewachsen und importiert worden sind`
];
module.exports = bot => {
bot._trigger.set("kaffee", {
call: /^(.|\/)kaffee/i,
level: 0,
active: true,
clients: ["irc", "tg"],
f: e => {
let args = e.message.trim().split(" ");
args.shift();
args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0];
e.replyAction(`serviert ${args.join(" ")} ${texte[~~(Math.random() * texte.length)]}; Prost!`);
}
});
};

View File

@ -0,0 +1,29 @@
const vm = require("vm");
module.exports = bot => {
bot._trigger.set("sandbox", {
call: /^\!js (.*)/i,
level: 100,
active: true,
clients: ["irc", "tg"],
f: e => {
const args = e.message.substring(3);
const context = {
e: e,
b: bot
};
try {
let output = vm.runInNewContext(args, context);
if (typeof output !== undefined && output) {
output = JSON.stringify(output);
if (output !== "Converting circular structure to JSON") {
e.reply(output.length > 400 ? `holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)` : output);
}
}
}
catch (err) {
e.reply(err.message);
}
}
});
};