better sync

This commit is contained in:
Flummi 2016-09-15 19:17:07 +02:00
parent 32b830f929
commit 1d390e01b3
3 changed files with 72 additions and 30 deletions

View File

@ -178,4 +178,18 @@ Lib.prototype.loadEvents = () => {
require(__dirname+'/events/' + file)(Lib.prototype);
}
});
};
Lib.prototype.groupUserModes = modes => {
var modesToApply = [], currentPrefix, queuedModes;
while(modes.length) {
queuedModes = "";
currentPrefix = modes[0][0];
modes.some(mode => {
if(mode[0] === currentPrefix) queuedModes += mode[1];
else return true;
});
modesToApply.push(currentPrefix + queuedModes);
for(var i = 0; i < queuedModes.length; i++) modes.shift();
}
return modesToApply;
};

View File

@ -5,26 +5,18 @@ module.exports = (lib) => {
level: 100,
active: 1,
func: (e) => {
lib.bot.names(e.channel, e.network, (names) => {
lib.bot.names(e.channel, e.network, names => {
Object.keys(names).forEach(key => {
if(key === lib.bot.me.nick) {
return;
}
lib.bot.whois(key, e.network, (err, data) => {
if(data.nick != lib.bot.me.nick) {
lib.bot.mode(e.channel, '-qaohv' + (' '+data.nick).repeat(5), e.network);
var level = 0;
var modes = false;
if(data.account in lib.admins) {
level = lib.admins[data.account].level;
if(level >= 70)
modes = "+ao";
else if(level >= 60)
modes = "+o";
else if(level >= 50)
modes = "+h";
else if(level >= 10)
modes = "+v";
}
if(modes !== false)
lib.bot.mode(e.channel, modes + (' '+data.nick).repeat(modes.length-1), e.network);
if(data.channels['#kbot-dev']) {
var h = modesToString(data.channels['#kbot-dev']), q = "";
if(lib.admins[data.account])
q = levelToModes(lib.admins[data.account].level);
var diff = lib.groupUserModes(userModeDiff(h, q));
diff.forEach(entry => lib.bot.mode(e.channel, entry + (' ' + key).repeat(entry.length - 1), e.network));
}
});
});
@ -32,4 +24,50 @@ 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;
};

View File

@ -21,18 +21,8 @@ module.exports = (lib) => {
var modes = processModes(tmp.join(''));
if(modes.length) {
if(checkModes(modes, allowedModes)) {
var modesToApply = [], currentPrefix, queuedModes;
while(modes.length) {
queuedModes = "";
currentPrefix = modes[0][0];
modes.some(mode => {
if(mode[0] === currentPrefix) queuedModes += mode[1];
else return true;
});
modesToApply.push(currentPrefix + queuedModes + (' ' + user).repeat(queuedModes.length));
for(var i = 0; i < queuedModes.length; i++) modes.shift();
}
modesToApply.forEach(modeStr => lib.bot.mode(e.channel, modeStr, e.network));
modesToApply = lib.groupUserModes(modes);
modesToApply.forEach(modeStr => lib.bot.mode(e.channel, modeStr + (' ' + user).repeat(modeStr.length - 1), e.network));
}
else
e.reply('Error! Allowed modes are: ' + allowedModes.join(','));