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();
self.cfg = {};
module.exports = Cfg;
function Cfg() {
self.cfg;
self.read(cfg => self.cfg = cfg);
}
self.read = (cb, kat, key) => {
@ -47,28 +48,31 @@ 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();
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();
self.read(cfg => {
self.cfg = cfg
if(cb) cb(err);
});
});
}
});
};

View File

@ -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");

View File

@ -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:
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);
break;
}
},
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;
};
};