Actually add GUILD_SYNC

This commit is contained in:
Amish Shah
2016-09-03 17:59:10 +01:00
parent 513fdabd19
commit a172fffd76
10 changed files with 69 additions and 6 deletions

View File

@@ -173,6 +173,13 @@ class Client extends EventEmitter {
return timeout;
}
syncGuilds(guilds = this.guilds.array()) {
return this.ws.send({
op: 12,
d: guilds.map(g => g.id),
});
}
/**
* Caches a user, or obtains it from the cache if it's already cached.
* If the user isn't already cached, it will only be obtainable by OAuth bot accounts.

View File

@@ -48,7 +48,7 @@ class ClientManager {
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: Date.now(),
});
}, true);
}, time);
}

View File

@@ -20,6 +20,7 @@ class ActionsManager {
this.register('GuildRoleUpdate');
this.register('UserGet');
this.register('UserUpdate');
this.register('GuildSync');
}
register(name) {

View File

@@ -0,0 +1,29 @@
const Action = require('./Action');
class GuildSync extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.id);
if (guild) {
data.presences = data.presences || [];
data.members = data.members || [];
for (const presence of data.presences) {
const user = client.users.get(presence.user.id);
if (user) {
user.status = presence.status;
user.game = presence.game;
}
}
for (const syncMember of data.members) {
const member = guild.members.get(syncMember.user.id);
if (member) {
guild._updateMember(member, syncMember);
}
}
}
}
}
module.exports = GuildSync;

View File

@@ -71,7 +71,10 @@ class WebSocketManager {
* @param {Object} packet An object that can be JSON stringified
* @returns {void}
*/
send(data) {
send(data, force = false) {
if (force) {
return this.ws.send(JSON.stringify(data));
}
this._queue.push(JSON.stringify(data));
this.doQueue();
}

View File

@@ -40,6 +40,7 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.VOICE_SERVER_UPDATE, 'VoiceServerUpdate');
this.register(Constants.WSEvents.MESSAGE_DELETE_BULK, 'MessageDeleteBulk');
this.register(Constants.WSEvents.CHANNEL_PINS_UPDATE, 'ChannelPinsUpdate');
this.register(Constants.WSEvents.GUILD_SYNC, 'GuildSync');
}
get client() {

View File

@@ -0,0 +1,14 @@
const AbstractHandler = require('./AbstractHandler');
class GuildSyncHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
client.actions.GuildSync.handle(data);
}
}
module.exports = GuildSyncHandler;

View File

@@ -21,6 +21,12 @@ class ReadyHandler extends AbstractHandler {
client.dataManager.newChannel(privateDM);
}
if (!client.user.bot) {
client.setInterval(client.syncGuilds.bind(client), 30000);
}
client.once('ready', client.syncGuilds.bind(client));
this.packetManager.ws.sessionID = data.session_id;
this.packetManager.ws.checkIfReady('abc');

View File

@@ -625,11 +625,12 @@ class Guild {
return this.members.get(this.ownerID);
}
/**
* Syncs this guild (already done automatically every 30 seconds)
* @returns {void}
*/
sync() {
return this.client.ws.send({
op: 12,
d: [this.id],
});
this.client.syncGuilds([this]);
}
}

View File

@@ -201,6 +201,7 @@ exports.WSEvents = {
VOICE_SERVER_UPDATE: 'VOICE_SERVER_UPDATE',
MESSAGE_DELETE_BULK: 'MESSAGE_DELETE_BULK',
CHANNEL_PINS_UPDATE: 'CHANNEL_PINS_UPDATE',
GUILD_SYNC: 'GUILD_SYNC',
};
const PermissionFlags = exports.PermissionFlags = {