Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Partials): Use more user objects available from the gateway #4791

Merged
merged 7 commits into from
Sep 13, 2020
Merged
12 changes: 1 addition & 11 deletions src/client/actions/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,7 @@ class GenericAction {
}

getMember(data, guild) {
const id = data.user.id;
return this.getPayload(
{
user: {
id,
},
},
guild.members,
id,
PartialTypes.GUILD_MEMBER,
);
return this.getPayload(data, guild.members, data.user.id, PartialTypes.GUILD_MEMBER);
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
}

getUser(data) {
Expand Down
1 change: 1 addition & 0 deletions src/client/actions/ActionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ActionsManager {
this.register(require('./GuildChannelsPositionUpdate'));
this.register(require('./GuildIntegrationsUpdate'));
this.register(require('./WebhooksUpdate'));
this.register(require('./TypingStart'));
}

register(Action) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/actions/GuildMemberRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GuildMemberRemoveAction extends Action {
const guild = client.guilds.cache.get(data.guild_id);
let member = null;
if (guild) {
member = this.getMember(data, guild);
member = this.getMember({ user: data.user }, guild);
guild.memberCount--;
if (member) {
member.deleted = true;
Expand Down
10 changes: 9 additions & 1 deletion src/client/actions/MessageReactionAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ class MessageReactionAdd extends Action {
handle(data) {
if (!data.emoji) return false;

const user = this.getUser(data);
let user;
if (data.guild_id) {
const guild = this.client.guilds.cache.get(data.guild_id);
if (guild) {
user = this.getMember(data.member, guild).user;
}
} else {
user = this.getUser(data);
}
if (!user) return false;

// Verify channel
Expand Down
63 changes: 63 additions & 0 deletions src/client/actions/TypingStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const textBasedChannelTypes = ['dm', 'text', 'news'];

class TypingStart extends Action {
handle(data) {
const channel = this.getChannel(data);
let user;
if (data.guild_id) {
const guild = this.client.guilds.cache.get(data.guild_id);
if (guild) {
user = this.getMember(data.member, guild).user;
}
} else {
user = this.getUser(data);
}
wasdennnoch marked this conversation as resolved.
Show resolved Hide resolved
const timestamp = new Date(data.timestamp * 1000);

if (channel && user) {
if (!textBasedChannelTypes.includes(channel.type)) {
this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
return;
}
wasdennnoch marked this conversation as resolved.
Show resolved Hide resolved

if (channel._typing.has(user.id)) {
const typing = channel._typing.get(user.id);

typing.lastTimestamp = timestamp;
typing.elapsedTime = Date.now() - typing.since;
this.client.clearTimeout(typing.timeout);
typing.timeout = this.tooLate(channel, user);
} else {
const since = new Date();
const lastTimestamp = new Date();
channel._typing.set(user.id, {
user,
since,
lastTimestamp,
elapsedTime: Date.now() - since,
timeout: this.tooLate(channel, user),
});

/**
* Emitted whenever a user starts typing in a channel.
* @event Client#typingStart
* @param {Channel} channel The channel the user started typing in
* @param {User} user The user that started typing
*/
this.client.emit(Events.TYPING_START, channel, user);
}
}
}

tooLate(channel, user) {
return channel.client.setTimeout(() => {
channel._typing.delete(user.id);
}, 10000);
}
}

module.exports = TypingStart;
49 changes: 2 additions & 47 deletions src/client/websocket/handlers/TYPING_START.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
'use strict';

const { Events } = require('../../../util/Constants');
const textBasedChannelTypes = ['dm', 'text', 'news'];

module.exports = (client, { d: data }) => {
const channel = client.channels.cache.get(data.channel_id);
const user = client.users.cache.get(data.user_id);
const timestamp = new Date(data.timestamp * 1000);

if (channel && user) {
if (!textBasedChannelTypes.includes(channel.type)) {
client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
return;
}

if (channel._typing.has(user.id)) {
const typing = channel._typing.get(user.id);

typing.lastTimestamp = timestamp;
typing.elapsedTime = Date.now() - typing.since;
client.clearTimeout(typing.timeout);
typing.timeout = tooLate(channel, user);
} else {
const since = new Date();
const lastTimestamp = new Date();
channel._typing.set(user.id, {
user,
since,
lastTimestamp,
elapsedTime: Date.now() - since,
timeout: tooLate(channel, user),
});

/**
* Emitted whenever a user starts typing in a channel.
* @event Client#typingStart
* @param {Channel} channel The channel the user started typing in
* @param {User} user The user that started typing
*/
client.emit(Events.TYPING_START, channel, user);
}
}
module.exports = (client, packet) => {
client.actions.TypingStart.handle(packet.d);
};

function tooLate(channel, user) {
return channel.client.setTimeout(() => {
channel._typing.delete(user.id);
}, 10000);
}