mirror of
https://github.com/reality/dbot.git
synced 2025-01-11 20:42:37 +01:00
Fix fucking async hell and add getAllChannels
This commit is contained in:
parent
e8757347ba
commit
61bd1312a6
@ -79,6 +79,17 @@ var api = function(dbot) {
|
||||
});
|
||||
},
|
||||
|
||||
'getAllChannels': function(callback) {
|
||||
var channels = [];
|
||||
this.db.scan('channel_users', function(channel) {
|
||||
channels.push(channel);
|
||||
}, function(err) {
|
||||
if(!err) {
|
||||
callback(channels);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
'isOnline': function(server, nick, channel, callback) {
|
||||
this.api.resolveUser(server, nick, function(user) {
|
||||
var possiNicks = [user].concat(user.aliases);
|
||||
@ -115,6 +126,9 @@ var api = function(dbot) {
|
||||
api['getAllUsers'].external = true;
|
||||
api['getAllUsers'].extMap = [ 'callback' ];
|
||||
|
||||
api['getAllChannels'].external = true;
|
||||
api['getAllChannels'].extMap = [ 'callback' ];
|
||||
|
||||
return api;
|
||||
};
|
||||
|
||||
|
@ -84,7 +84,6 @@ var users = function(dbot) {
|
||||
'channels': [],
|
||||
'aliases': []
|
||||
};
|
||||
this.internalAPI.addChannelUser(user, event.channel.name, function(err) { });
|
||||
needsUpdating = true;
|
||||
}
|
||||
|
||||
@ -127,26 +126,31 @@ var users = function(dbot) {
|
||||
dbot.instance.addListener('366', 'users', function(event) {
|
||||
this.api.getChannel(event.server, event.channel.name, function(channel) {
|
||||
var checkChannelUsers = function(channel) {
|
||||
_.each(event.channel.nicks, function(nick) {
|
||||
var nick = nick.name;
|
||||
var i = 0;
|
||||
var checkChannelNicks = function(chanicks) {
|
||||
if(i == chanicks.length) return;
|
||||
var nick = chanicks[i]; i++;
|
||||
this.api.resolveUser(event.server, nick, function(user) {
|
||||
if(!user) {
|
||||
this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) {
|
||||
channel.users.push(result.id);
|
||||
this.internalAPI.addChannelUser(result, event.channel.name, function(err) {
|
||||
checkChannelNicks(chanicks);
|
||||
});
|
||||
}.bind(this));
|
||||
} else {
|
||||
if(!_.include(user.channels, event.channel.name)) {
|
||||
channel.users.push(user.id);
|
||||
this.internalAPI.addChannelUser(user, event.channel.name, function(err) {
|
||||
checkChannelNicks(chanicks);
|
||||
});
|
||||
user.channels.push(event.channel.name);
|
||||
this.db.save('users', user.id, user, function(err) { });
|
||||
} else {
|
||||
checkChannelNicks(chanicks);
|
||||
}
|
||||
}
|
||||
}.bind(this));
|
||||
}, this);
|
||||
|
||||
process.nextTick(function() {
|
||||
this.db.save('channel_users', channel.id, channel, function(err) { });
|
||||
}.bind(this));
|
||||
}.bind(this);
|
||||
checkChannelNicks(_.keys(event.channel.nicks));
|
||||
}.bind(this);
|
||||
|
||||
if(!channel) { // Channel does not yet exist
|
||||
|
Loading…
Reference in New Issue
Block a user