trigger saugen

This commit is contained in:
Flummi 2016-11-23 11:41:42 +01:00
parent 30769cf8f6
commit 8c80c98ec3
3 changed files with 63 additions and 39 deletions

View File

@ -12,9 +12,10 @@ var haDC = () => {
}; };
haDC(); haDC();
self.cfg = {};
module.exports = Cfg; module.exports = Cfg;
function Cfg() { function Cfg() {
self.cfg; self.read(cfg => self.cfg = cfg);
} }
self.read = (cb, kat, key) => { self.read = (cb, kat, key) => {
@ -47,28 +48,31 @@ self.read = (cb, kat, key) => {
} }
})(e.type, e.value); })(e.type, e.value);
}); });
if(cb) if(cb) cb( key?out[kat][key]:out );
cb( key?out[kat][key]:out );
else
if(!kat && !key)
self.cfg = out;
} }
}); });
}; };
self.write = (kat, key, value, type, cb) => { 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) => { self.sql.query("select * from `f0ck`.`cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
if(rows.length > 0) { if(rows.length > 0) {
self.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => { self.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
self.read(); self.read(cfg => {
self.cfg = cfg
if(cb) cb(err); if(cb) cb(err);
}); });
});
} }
else { else {
self.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => { self.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => {
self.read(); self.read(cfg => {
self.cfg = cfg
if(cb) cb(err); if(cb) cb(err);
}); });
});
} }
}); });
}; };

View File

@ -12,7 +12,7 @@ module.exports = (lib) => {
name: 'cfg', name: 'cfg',
call: /^!cfg/i, call: /^!cfg/i,
level: 100, level: 100,
active: 1, active: 0,
func: (e) => { func: (e) => {
// irc, main, websrv, trigger // irc, main, websrv, trigger
var msg = e.message.replace('!cfg.',''); var msg = e.message.replace('!cfg.','');
@ -46,9 +46,9 @@ module.exports = (lib) => {
if(kat in kats) { if(kat in kats) {
var key = cmd[1]; var key = cmd[1];
if(kats[kat].indexOf(key) !== -1) { if(kats[kat].indexOf(key) !== -1) {
lib.getcfg(kat, (cb) => { lib.config.read(cb => {
e.reply(kat+"."+key+" = "+JSON.stringify(cb)); e.reply(kat+"."+key+" = "+JSON.stringify(cb));
}, key); }, kat, key);
} }
else { else {
e.reply("key not found"); e.reply("key not found");

View File

@ -6,38 +6,58 @@ module.exports = (lib) => {
active: 1, active: 1,
func: (e) => { func: (e) => {
var args = e.message.split(' '); 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(); lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger();
switch(args[1]) { else // check existing config
case "std": lib.cfg.trigger[e.network+e.channel] = extend(lib.genStdTrigger(), lib.cfg.trigger[e.network+e.channel]);
e.reply(JSON.stringify(lib.genStdTrigger()));
break; if(args.length > 0) {
case "toggle": switch(args[0]) {
if(trigger = args[2].toLowerCase()) { case "reset":
lib.cfg.trigger[e.network+e.channel] = triggertoggle(lib.cfg.trigger[e.network+e.channel], trigger); lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger();
lib.config.write('trigger', e.network+e.channel, JSON.stringify(lib.cfg.trigger[e.network+e.channel]), 'json', cbwc => { lib.config.write('trigger', e.network+e.channel, lib.cfg.trigger[e.network+e.channel], 'json', cbwc => {
if(!cbwc) e.reply("trigger resetted");
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);
}); });
}
break; break;
default: 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 = ""; var output = "";
e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):"); e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):");
for(var key in lib.cfg.trigger[e.network+e.channel]) 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; output += lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][key]?'green':'red')+" "+key;
e.reply(output); e.reply(output);
break;
} }
}, },
desc: 'muh' desc: 'muh'
}); });
var triggertoggle = (trigger, key) => { var extend = (obj, src) => {
if(key in trigger) Object.keys(src).forEach(key => obj[key] = src[key]);
trigger[key] = !trigger[key]; return obj;
return trigger; };
var triggerexists = (search) => {
var out = false;
lib.trigger.trigger.forEach(entry => {
if(entry.name === search) {
out = true;
return;
}
});
return out;
}; };
}; };