Merge branch 'f0ckdev' into 'master'

F0ckdev

See merge request !42
This commit is contained in:
Flummi 2016-11-19 05:36:25 +00:00
commit 9528d30c61
3 changed files with 123 additions and 29 deletions

View File

@ -1,14 +1,23 @@
module.exports = (lib) => {
lib.bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
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');
});
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)
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.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,29 +229,34 @@ 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` = ?", kat, (err, rows) => {
rows.forEach(e => {
out[e.key] = ((type, value) => {
switch(type) {
case 'string':
return value;
break;
case 'int':
return parseInt(value);
break;
case 'bool':
return (value === 'true')?true:false;
break;
case 'json':
return JSON.parse(value);
break;
}
})(e.type, e.value);
});
if(cb) cb(out);
self.sql.query("select * from `f0ck`.`cfg` where `class` = ?"+(key?" && `key` = '"+key+"'":""), kat, (err, rows) => {
if(err || rows.length < 1)
cb(err);
else {
rows.forEach(e => {
out[e.key] = ((type, value) => {
switch(type) {
case 'string':
return value;
break;
case 'int':
return parseInt(value);
break;
case 'bool':
return (value === 'true')?true:false;
break;
case 'json':
return JSON.parse(value);
break;
}
})(e.type, e.value);
});
if(cb)
cb( key?out[key]:out );
}
});
};
@ -273,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;
};

62
src/trigger/trigger.js Normal file
View File

@ -0,0 +1,62 @@
module.exports = (lib) => {
lib.trigger.add({
name: 'trigger',
call: /^!trigger/i,
level: 100,
active: 1,
func: (e) => {
var args = e.message.split(' ');
lib.getcfg('trigger', cb => {
if(!cb)
cb = lib.genStdTrigger();
switch(args[1]) {
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):");
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);
});
}
});
};
};