channeltrigger toggable

This commit is contained in:
Flummi 2016-11-19 06:33:59 +01:00
parent e53573c28a
commit b2e8119155
3 changed files with 88 additions and 31 deletions

View File

@ -1,7 +1,15 @@
module.exports = (lib) => {
lib.bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
if(e.message.match(entry.call)) {
let active = false;
if(lib.trigger.chans[e.network+e.channel.getName()] !== undefined) {
if(lib.trigger.chans[e.network+e.channel.getName()][entry.name])
active = true;
}
else
active = entry.active;
if(active) {
lib.log('trigger: '+entry.name);
lib.getUserlevel(e.user.getNick(), e.network, e.channel.getName(), (cb) => {
if(cb.level >= entry.level)
@ -10,6 +18,7 @@ module.exports = (lib) => {
e.reply('no permission, min level '+entry.level+' required');
});
}
}
});
});
};

View File

@ -13,6 +13,7 @@ self.admins = [];
self.debug = false;
self.trigger = {
trigger: [],
chans: {},
add: (args) => {
self.trigger.trigger.push({
name: args.name,
@ -45,6 +46,7 @@ function Lib() {
self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
self.loadTrigger();
self.loadEvents();
self.getChanTrigger();
});
}
@ -227,10 +229,10 @@ self.levelToModes = level => {
return "";
};
self.getcfg = (kat, cb) => {
self.getcfg = (kat, cb, key) => {
// main, websrv, irc
var out = {};
self.sql.query("select * from `f0ck`.`cfg` where `class` = ?"+(arguments[2]?" && `key` = '"+arguments[2]+"'":""), kat, (err, rows) => {
self.sql.query("select * from `f0ck`.`cfg` where `class` = ?"+(key?" && `key` = '"+key+"'":""), kat, (err, rows) => {
if(err || rows.length < 1)
cb(err);
else {
@ -252,7 +254,8 @@ self.getcfg = (kat, cb) => {
}
})(e.type, e.value);
});
if(cb) cb(out);
if(cb)
cb( key?out[key]:out );
}
});
};
@ -277,3 +280,19 @@ self.loadIRC = () => {
});
});
};
self.getChanTrigger = () => {
self.trigger.chans = {};
self.getcfg('trigger', cb => {
for(let tmp in cb)
self.trigger.chans[tmp] = cb[tmp];
});
};
self.genStdTrigger = () => {
var out = {};
self.trigger.trigger.forEach((entry, key) => {
out[entry.name] = entry.active?true:false;
});
return out;
};

View File

@ -6,28 +6,57 @@ module.exports = (lib) => {
active: 1,
func: (e) => {
var args = e.message.split(' ');
lib.getcfg('trigger', cb => {
if(!cb)
cb = lib.genStdTrigger();
switch(args[1]) {
case "cfg":
lib.getcfg('trigger_'+e.network+'_'+e.channel, cb => {
if(!cb) {
e.reply(cb);
}
else {
e.reply('muh');
case "std":
e.reply(JSON.stringify(lib.genStdTrigger()));
break;
case "toggle":
if(trigger = args[2].toLowerCase()) {
cb = triggertoggle(cb, trigger);
writecfg('trigger', e.network+e.channel, JSON.stringify(cb), cbwc => {
if(!cbwc) {
e.reply("trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(cb[trigger]?'green':'red')+" "+(cb[trigger]?"activated":"deactivated"));
lib.getChanTrigger();
}
else
e.reply(cbwc);
});
}
break;
default:
var output = "";
e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):");
lib.trigger.trigger.forEach(e => {
output += lib.bot.format.get( (e.active)?'green':'red' )+" "+e.name;
});
for(var key in cb)
output += lib.bot.format.get(cb[key]?'green':'red')+" "+key;
e.reply(output);
break;
}
}, e.network+e.channel);
},
desc: 'muh'
});
var triggertoggle = (trigger, key) => {
if(key in trigger)
trigger[key] = !trigger[key];
return trigger;
};
var writecfg = (kat, key, value, cb) => {
lib.sql.query("select * from `f0ck`.`cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
if(rows.length > 0) {
lib.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
if(cb) cb(err);
});
}
else {
lib.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, 'json'], (err) => {
if(cb) cb(err);
});
}
});
};
};