Skip to content

Commit eeb4c14

Browse files
authored
fix(Partials): Use more user objects available from the gateway (#4791)
1 parent bcb7c72 commit eeb4c14

File tree

6 files changed

+71
-60
lines changed

6 files changed

+71
-60
lines changed

src/client/actions/Action.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ class GenericAction {
8181
}
8282

8383
getMember(data, guild) {
84-
const id = data.user.id;
85-
return this.getPayload(
86-
{
87-
user: {
88-
id,
89-
},
90-
},
91-
guild.members,
92-
id,
93-
PartialTypes.GUILD_MEMBER,
94-
);
84+
return this.getPayload(data, guild.members, data.user.id, PartialTypes.GUILD_MEMBER);
9585
}
9686

9787
getUser(data) {
9888
const id = data.user_id;
9989
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
10090
}
91+
92+
getUserFromMember(data) {
93+
if (data.guild_id) {
94+
const guild = this.client.guilds.cache.get(data.guild_id);
95+
if (guild) {
96+
return this.getMember(data.member, guild).user;
97+
}
98+
}
99+
return this.getUser(data);
100+
}
101101
}
102102

103103
module.exports = GenericAction;

src/client/actions/ActionsManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ActionsManager {
3535
this.register(require('./GuildChannelsPositionUpdate'));
3636
this.register(require('./GuildIntegrationsUpdate'));
3737
this.register(require('./WebhooksUpdate'));
38+
this.register(require('./TypingStart'));
3839
}
3940

4041
register(Action) {

src/client/actions/GuildMemberRemove.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class GuildMemberRemoveAction extends Action {
99
const guild = client.guilds.cache.get(data.guild_id);
1010
let member = null;
1111
if (guild) {
12-
member = this.getMember(data, guild);
12+
member = this.getMember({ user: data.user }, guild);
1313
guild.memberCount--;
1414
if (member) {
1515
member.deleted = true;

src/client/actions/MessageReactionAdd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MessageReactionAdd extends Action {
1515
handle(data) {
1616
if (!data.emoji) return false;
1717

18-
const user = this.getUser(data);
18+
const user = this.getUserFromMember(data);
1919
if (!user) return false;
2020

2121
// Verify channel

src/client/actions/TypingStart.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const Action = require('./Action');
4+
const { Events } = require('../../util/Constants');
5+
const textBasedChannelTypes = ['dm', 'text', 'news'];
6+
7+
class TypingStart extends Action {
8+
handle(data) {
9+
const channel = this.getChannel(data);
10+
if (!textBasedChannelTypes.includes(channel.type)) {
11+
this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
12+
return;
13+
}
14+
15+
const user = this.getUserFromMember(data);
16+
const timestamp = new Date(data.timestamp * 1000);
17+
18+
if (channel && user) {
19+
if (channel._typing.has(user.id)) {
20+
const typing = channel._typing.get(user.id);
21+
22+
typing.lastTimestamp = timestamp;
23+
typing.elapsedTime = Date.now() - typing.since;
24+
this.client.clearTimeout(typing.timeout);
25+
typing.timeout = this.tooLate(channel, user);
26+
} else {
27+
const since = new Date();
28+
const lastTimestamp = new Date();
29+
channel._typing.set(user.id, {
30+
user,
31+
since,
32+
lastTimestamp,
33+
elapsedTime: Date.now() - since,
34+
timeout: this.tooLate(channel, user),
35+
});
36+
37+
/**
38+
* Emitted whenever a user starts typing in a channel.
39+
* @event Client#typingStart
40+
* @param {Channel} channel The channel the user started typing in
41+
* @param {User} user The user that started typing
42+
*/
43+
this.client.emit(Events.TYPING_START, channel, user);
44+
}
45+
}
46+
}
47+
48+
tooLate(channel, user) {
49+
return channel.client.setTimeout(() => {
50+
channel._typing.delete(user.id);
51+
}, 10000);
52+
}
53+
}
54+
55+
module.exports = TypingStart;
Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,5 @@
11
'use strict';
22

3-
const { Events } = require('../../../util/Constants');
4-
const textBasedChannelTypes = ['dm', 'text', 'news'];
5-
6-
module.exports = (client, { d: data }) => {
7-
const channel = client.channels.cache.get(data.channel_id);
8-
const user = client.users.cache.get(data.user_id);
9-
const timestamp = new Date(data.timestamp * 1000);
10-
11-
if (channel && user) {
12-
if (!textBasedChannelTypes.includes(channel.type)) {
13-
client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
14-
return;
15-
}
16-
17-
if (channel._typing.has(user.id)) {
18-
const typing = channel._typing.get(user.id);
19-
20-
typing.lastTimestamp = timestamp;
21-
typing.elapsedTime = Date.now() - typing.since;
22-
client.clearTimeout(typing.timeout);
23-
typing.timeout = tooLate(channel, user);
24-
} else {
25-
const since = new Date();
26-
const lastTimestamp = new Date();
27-
channel._typing.set(user.id, {
28-
user,
29-
since,
30-
lastTimestamp,
31-
elapsedTime: Date.now() - since,
32-
timeout: tooLate(channel, user),
33-
});
34-
35-
/**
36-
* Emitted whenever a user starts typing in a channel.
37-
* @event Client#typingStart
38-
* @param {Channel} channel The channel the user started typing in
39-
* @param {User} user The user that started typing
40-
*/
41-
client.emit(Events.TYPING_START, channel, user);
42-
}
43-
}
3+
module.exports = (client, packet) => {
4+
client.actions.TypingStart.handle(packet.d);
445
};
45-
46-
function tooLate(channel, user) {
47-
return channel.client.setTimeout(() => {
48-
channel._typing.delete(user.id);
49-
}, 10000);
50-
}

0 commit comments

Comments
 (0)