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

View File

@ -13,6 +13,7 @@ self.admins = [];
self.debug = false; self.debug = false;
self.trigger = { self.trigger = {
trigger: [], trigger: [],
chans: {},
add: (args) => { add: (args) => {
self.trigger.trigger.push({ self.trigger.trigger.push({
name: args.name, name: args.name,
@ -45,6 +46,7 @@ function Lib() {
self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
self.loadTrigger(); self.loadTrigger();
self.loadEvents(); self.loadEvents();
self.getChanTrigger();
}); });
} }
@ -227,10 +229,10 @@ self.levelToModes = level => {
return ""; return "";
}; };
self.getcfg = (kat, cb) => { self.getcfg = (kat, cb, key) => {
// main, websrv, irc // main, websrv, irc
var out = {}; 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) if(err || rows.length < 1)
cb(err); cb(err);
else { else {
@ -252,7 +254,8 @@ self.getcfg = (kat, cb) => {
} }
})(e.type, e.value); })(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, active: 1,
func: (e) => { func: (e) => {
var args = e.message.split(' '); var args = e.message.split(' ');
lib.getcfg('trigger', cb => {
switch(args[1]) { if(!cb)
case "cfg": cb = lib.genStdTrigger();
lib.getcfg('trigger_'+e.network+'_'+e.channel, cb => { switch(args[1]) {
if(!cb) { case "std":
e.reply(cb); 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);
});
} }
else { break;
e.reply('muh'); default:
} var output = "";
}); e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):");
break; for(var key in cb)
default: output += lib.bot.format.get(cb[key]?'green':'red')+" "+key;
var output = ""; e.reply(output);
e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):"); break;
lib.trigger.trigger.forEach(e => { }
output += lib.bot.format.get( (e.active)?'green':'red' )+" "+e.name; }, e.network+e.channel);
});
e.reply(output);
break;
}
}, },
desc: 'muh' 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);
});
}
});
};
}; };