From 8c80c98ec39f93a1574e63eb45a3dc75ff722587 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 23 Nov 2016 11:41:42 +0100 Subject: [PATCH] trigger saugen --- src/cfg.js | 24 ++++++++------ src/trigger/cfg.js | 6 ++-- src/trigger/trigger.js | 72 +++++++++++++++++++++++++++--------------- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/cfg.js b/src/cfg.js index 5918e62..46bf874 100644 --- a/src/cfg.js +++ b/src/cfg.js @@ -12,9 +12,10 @@ var haDC = () => { }; haDC(); +self.cfg = {}; module.exports = Cfg; function Cfg() { - self.cfg; + self.read(cfg => self.cfg = cfg); } self.read = (cb, kat, key) => { @@ -47,27 +48,30 @@ self.read = (cb, kat, key) => { } })(e.type, e.value); }); - if(cb) - cb( key?out[kat][key]:out ); - else - if(!kat && !key) - self.cfg = out; + if(cb) cb( key?out[kat][key]:out ); } }); }; self.write = (kat, key, value, type, cb) => { + if(type === "json") + value = JSON.stringify(value); + self.sql.query("select * from `f0ck`.`cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => { if(rows.length > 0) { self.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => { - self.read(); - if(cb) cb(err); + self.read(cfg => { + self.cfg = cfg + if(cb) cb(err); + }); }); } else { self.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => { - self.read(); - if(cb) cb(err); + self.read(cfg => { + self.cfg = cfg + if(cb) cb(err); + }); }); } }); diff --git a/src/trigger/cfg.js b/src/trigger/cfg.js index af1ae54..ceca85f 100644 --- a/src/trigger/cfg.js +++ b/src/trigger/cfg.js @@ -12,7 +12,7 @@ module.exports = (lib) => { name: 'cfg', call: /^!cfg/i, level: 100, - active: 1, + active: 0, func: (e) => { // irc, main, websrv, trigger var msg = e.message.replace('!cfg.',''); @@ -46,9 +46,9 @@ module.exports = (lib) => { if(kat in kats) { var key = cmd[1]; if(kats[kat].indexOf(key) !== -1) { - lib.getcfg(kat, (cb) => { + lib.config.read(cb => { e.reply(kat+"."+key+" = "+JSON.stringify(cb)); - }, key); + }, kat, key); } else { e.reply("key not found"); diff --git a/src/trigger/trigger.js b/src/trigger/trigger.js index b63d263..6f01487 100644 --- a/src/trigger/trigger.js +++ b/src/trigger/trigger.js @@ -6,38 +6,58 @@ module.exports = (lib) => { active: 1, func: (e) => { var args = e.message.split(' '); - if(!lib.cfg.trigger[e.network+e.channel]) + args.shift(); + if(!lib.cfg.trigger[e.network+e.channel]) // generate config for channel lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger(); - switch(args[1]) { - case "std": - e.reply(JSON.stringify(lib.genStdTrigger())); - break; - case "toggle": - if(trigger = args[2].toLowerCase()) { - lib.cfg.trigger[e.network+e.channel] = triggertoggle(lib.cfg.trigger[e.network+e.channel], trigger); - lib.config.write('trigger', e.network+e.channel, JSON.stringify(lib.cfg.trigger[e.network+e.channel]), 'json', cbwc => { - if(!cbwc) - e.reply("trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][trigger]?'green':'red')+" "+(lib.cfg.trigger[e.network+e.channel][trigger]?'activated':'deactivated')); - else - e.reply(cbwc); + else // check existing config + lib.cfg.trigger[e.network+e.channel] = extend(lib.genStdTrigger(), lib.cfg.trigger[e.network+e.channel]); + + if(args.length > 0) { + switch(args[0]) { + case "reset": + lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger(); + lib.config.write('trigger', e.network+e.channel, lib.cfg.trigger[e.network+e.channel], 'json', cbwc => { + e.reply("trigger resetted"); }); - } - break; - default: - var output = ""; - e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):"); - for(var key in lib.cfg.trigger[e.network+e.channel]) - output += lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][key]?'green':'red')+" "+key; - e.reply(output); - break; + break; + default: + for(let i = 0; i < args.length; i++) { + let trigger = args[i].toLowerCase(); + if(triggerexists(trigger)) { + lib.cfg.trigger[e.network+e.channel][trigger] = !lib.cfg.trigger[e.network+e.channel][trigger]; + lib.config.write('trigger', e.network+e.channel, lib.cfg.trigger[e.network+e.channel], 'json', cbwc => { + e.reply(!cbwc?"trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][trigger]?'green':'red')+" "+(lib.cfg.trigger[e.network+e.channel][trigger]?'activated':'deactivated'):cbwc); + }); + } + else + e.reply("trigger "+trigger+" not found D:"); + } + break; + } + } + else { + var output = ""; + e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):"); + for(var key in lib.cfg.trigger[e.network+e.channel]) + output += lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][key]?'green':'red')+" "+key; + e.reply(output); } }, desc: 'muh' }); - var triggertoggle = (trigger, key) => { - if(key in trigger) - trigger[key] = !trigger[key]; - return trigger; + var extend = (obj, src) => { + Object.keys(src).forEach(key => obj[key] = src[key]); + return obj; + }; + var triggerexists = (search) => { + var out = false; + lib.trigger.trigger.forEach(entry => { + if(entry.name === search) { + out = true; + return; + } + }); + return out; }; }; \ No newline at end of file