Make presences track users and guilds, emit them in presenceUpdate

This commit is contained in:
Amish Shah
2018-08-10 16:46:14 +01:00
parent fe8ece0192
commit 08eff66939
7 changed files with 32 additions and 13 deletions

View File

@@ -23,6 +23,8 @@ class PresenceUpdateHandler extends AbstractHandler {
}
if (guild) {
let oldPresence = guild.presences.get(user.id);
if (oldPresence) oldPresence = oldPresence._clone();
let member = guild.members.get(user.id);
if (!member && data.status !== 'offline') {
member = guild.members.add({
@@ -35,17 +37,13 @@ class PresenceUpdateHandler extends AbstractHandler {
}
if (member) {
if (client.listenerCount(Events.PRESENCE_UPDATE) === 0) {
guild.presences.add(data);
guild.presences.add(Object.assign(data, { guild: this }));
return;
}
const oldMember = member._clone();
if (member.presence) {
oldMember.frozenPresence = member.presence._clone();
}
guild.presences.add(data);
client.emit(Events.PRESENCE_UPDATE, oldMember, member);
guild.presences.add(Object.assign(data, { guild: this }));
client.emit(Events.PRESENCE_UPDATE, oldPresence, member.presence);
} else {
guild.presences.add(data);
guild.presences.add(Object.assign(data, { guild: this }));
}
}
}

View File

@@ -9,6 +9,7 @@ class ReadyHandler extends AbstractHandler {
client.ws.heartbeat();
client.presences.clientPresence.userID = data.user.id;
if (!ClientUser) ClientUser = require('../../../../structures/ClientUser');
const clientUser = new ClientUser(client, data.user);
client.user = clientUser;
@@ -17,7 +18,6 @@ class ReadyHandler extends AbstractHandler {
for (const guild of data.guilds) client.guilds.add(guild);
for (const privateDM of data.private_channels) client.channels.add(privateDM);
for (const presence of data.presences || []) client.presences.add(presence);
if (!client.users.has('1')) {
client.users.add({

View File

@@ -16,6 +16,8 @@ class ClientPresenceStore extends PresenceStore {
afk: false,
since: null,
activity: null,
user: { id: null },
guild_id: null,
});
}

View File

@@ -226,7 +226,7 @@ class Guild extends Base {
if (data.presences) {
for (const presence of data.presences) {
this.presences.add(presence);
this.presences.add(Object.assign(presence, { guild: this }));
}
}

View File

@@ -114,7 +114,7 @@ class GuildMember extends Base {
* @readonly
*/
get presence() {
return this.frozenPresence || this.guild.presences.get(this.id) || new Presence(this.client);
return this.guild.presences.get(this.id) || new Presence(this.client);
}
/**

View File

@@ -14,9 +14,27 @@ const { ActivityTypes, ActivityFlags } = require('../util/Constants');
class Presence {
constructor(client, data = {}) {
Object.defineProperty(this, 'client', { value: client });
this.userID = data.user.id;
this.guild = data.guild;
this.patch(data);
}
/**
* The user of this presence
* @type {User}
*/
get user() {
return this.client.users.get(this.userID);
}
/**
* The member of this presence
* @type {GuildMember}
*/
get member() {
return this.guild.members.get(this.userID);
}
patch(data) {
/**
* The status of the presence:

View File

@@ -29,8 +29,9 @@ var count = 0;
process.on('unhandledRejection', console.log);
client.on('voiceStateUpdate', (a, b) => {
console.log(a ? a.channelID : null, b ? b.channelID : null, b.member.user.username);
client.on('presenceUpdate', (a, b) => {
if (b.userID !== '66564597481480192') return;
console.log(a ? a.status : null, b.status, b.user.username);
});
client.on('message', m => {