level apply after change

This commit is contained in:
Flummi 2016-09-16 00:04:01 +02:00
parent 0534545b53
commit 0f2ab86469
3 changed files with 56 additions and 49 deletions

View File

@ -19,7 +19,8 @@ irccfg.forEach(e => {
"pass": e.pass,
"nick": e.nick,
"username": e.username,
"realname": e.realname
"realname": e.realname,
"throttling": e.throttling
});
console.log("Server "+e.name+" wurde geladen");
});
@ -193,4 +194,47 @@ self.groupUserModes = modes => {
for(var i = 0; i < queuedModes.length; i++) modes.shift();
}
return modesToApply;
};
self.userModeDiff = (h, q) => {
var ret = [], index;
while(h.length && q.length) {
if((index = q.indexOf(h[h.length - 1])) !== -1)
q = q.slice(0, index) + q.slice(index + 1);
else
ret.push('-' + h[h.length - 1]);
h = h.slice(0, -1);
}
var sign = q.length ? '+' : '-';
if(!q.length) q = h;
if(index = q.length)
while(index--)
ret.push(sign + q[index]);
return ret;
};
self.modesToString = modes => {
var modeStr = "";
modes.forEach(entry => {
if(entry === "~")
modeStr += "q";
else if(entry === "&")
modeStr += "a";
else if(entry === "@")
modeStr += "o";
else if(entry === "%")
modeStr += "h";
else if(entry === "+")
modeStr += "v";
});
return modeStr;
};
self.levelToModes = level => {
if(level >= 70)
return "ao";
if(level >= 60)
return "o";
if(level >= 50)
return "h";
if(level >= 10)
return "v";
return "";
};

View File

@ -20,12 +20,14 @@ module.exports = (lib) => {
if(level <= 0) {
lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[data.account].id], () => {
lib.loadUser();
applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
e.reply('deleted user '+data.nick+' ('+data.account+')');
});
}
else {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[data.account].id], () => {
lib.loadUser();
applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
e.reply('changed user '+data.nick+' ('+data.account+') to level '+level);
});
}
@ -33,6 +35,7 @@ module.exports = (lib) => {
else {
lib.sql.query('insert into `f0ck`.`user` (`account`,`level`,`network`) values (?,?,?)', [data.account, level, e.network], () => {
lib.loadUser();
applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
e.reply('added new user '+data.nick+' ('+data.account+') with level '+level);
});
}
@ -57,4 +60,10 @@ module.exports = (lib) => {
},
desc: 'get Userlevel'
});
};
var applyModes = (lib, h, q, channel, nick) => {
h = lib.modesToString(h);
q = lib.levelToModes(q);
lib.groupUserModes(lib.userModeDiff(h, q)).forEach(entry => lib.bot.mode(channel, entry + (' ' + nick).repeat(entry.length - 1)));
};

View File

@ -12,10 +12,10 @@ module.exports = (lib) => {
}
lib.bot.whois(key, e.network, (err, data) => {
if(data.channels[e.channel]) {
var h = modesToString(data.channels[e.channel]), q = "";
var h = lib.modesToString(data.channels[e.channel]), q = "";
if(lib.admins[data.account])
q = levelToModes(lib.admins[data.account].level);
var diff = lib.groupUserModes(userModeDiff(h, q));
var diff = lib.groupUserModes(lib.userModeDiff(h, q));
diff.forEach(entry => lib.bot.mode(e.channel, entry + (' ' + key).repeat(entry.length - 1), e.network));
}
});
@ -24,50 +24,4 @@ module.exports = (lib) => {
},
desc: 'sync'
});
};
var userModeDiff = (h, q) => {
var ret = [], index;
while(h.length && q.length) {
if((index = q.indexOf(h[h.length - 1])) !== -1)
q = q.slice(0, index) + q.slice(index + 1);
else
ret.push('-' + h[h.length - 1]);
h = h.slice(0, -1);
}
var sign = q.length ? '+' : '-';
if(!q.length) q = h;
if(index = q.length)
while(index--)
ret.push(sign + q[index]);
return ret;
};
var levelToModes = level => {
if(level >= 70)
return "ao";
if(level >= 60)
return "o";
if(level >= 50)
return "h";
if(level >= 10)
return "v";
return "";
};
var modesToString = modes => {
var modeStr = "";
modes.forEach(entry => {
if(entry === "~")
modeStr += "q";
else if(entry === "&")
modeStr += "a";
else if(entry === "@")
modeStr += "o";
else if(entry === "%")
modeStr += "h";
else if(entry === "+")
modeStr += "v";
});
return modeStr;
};