mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
fix(Partials): Use more user objects available from the gateway (#4791)
This commit is contained in:
@@ -81,23 +81,23 @@ class GenericAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getMember(data, guild) {
|
getMember(data, guild) {
|
||||||
const id = data.user.id;
|
return this.getPayload(data, guild.members, data.user.id, PartialTypes.GUILD_MEMBER);
|
||||||
return this.getPayload(
|
|
||||||
{
|
|
||||||
user: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
guild.members,
|
|
||||||
id,
|
|
||||||
PartialTypes.GUILD_MEMBER,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getUser(data) {
|
getUser(data) {
|
||||||
const id = data.user_id;
|
const id = data.user_id;
|
||||||
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
|
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getUserFromMember(data) {
|
||||||
|
if (data.guild_id) {
|
||||||
|
const guild = this.client.guilds.cache.get(data.guild_id);
|
||||||
|
if (guild) {
|
||||||
|
return this.getMember(data.member, guild).user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.getUser(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = GenericAction;
|
module.exports = GenericAction;
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ class ActionsManager {
|
|||||||
this.register(require('./GuildChannelsPositionUpdate'));
|
this.register(require('./GuildChannelsPositionUpdate'));
|
||||||
this.register(require('./GuildIntegrationsUpdate'));
|
this.register(require('./GuildIntegrationsUpdate'));
|
||||||
this.register(require('./WebhooksUpdate'));
|
this.register(require('./WebhooksUpdate'));
|
||||||
|
this.register(require('./TypingStart'));
|
||||||
}
|
}
|
||||||
|
|
||||||
register(Action) {
|
register(Action) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class GuildMemberRemoveAction extends Action {
|
|||||||
const guild = client.guilds.cache.get(data.guild_id);
|
const guild = client.guilds.cache.get(data.guild_id);
|
||||||
let member = null;
|
let member = null;
|
||||||
if (guild) {
|
if (guild) {
|
||||||
member = this.getMember(data, guild);
|
member = this.getMember({ user: data.user }, guild);
|
||||||
guild.memberCount--;
|
guild.memberCount--;
|
||||||
if (member) {
|
if (member) {
|
||||||
member.deleted = true;
|
member.deleted = true;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class MessageReactionAdd extends Action {
|
|||||||
handle(data) {
|
handle(data) {
|
||||||
if (!data.emoji) return false;
|
if (!data.emoji) return false;
|
||||||
|
|
||||||
const user = this.getUser(data);
|
const user = this.getUserFromMember(data);
|
||||||
if (!user) return false;
|
if (!user) return false;
|
||||||
|
|
||||||
// Verify channel
|
// Verify channel
|
||||||
|
|||||||
55
src/client/actions/TypingStart.js
Normal file
55
src/client/actions/TypingStart.js
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
'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);
|
||||||
|
if (!textBasedChannelTypes.includes(channel.type)) {
|
||||||
|
this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = this.getUserFromMember(data);
|
||||||
|
const timestamp = new Date(data.timestamp * 1000);
|
||||||
|
|
||||||
|
if (channel && user) {
|
||||||
|
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;
|
||||||
@@ -1,50 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Events } = require('../../../util/Constants');
|
module.exports = (client, packet) => {
|
||||||
const textBasedChannelTypes = ['dm', 'text', 'news'];
|
client.actions.TypingStart.handle(packet.d);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function tooLate(channel, user) {
|
|
||||||
return channel.client.setTimeout(() => {
|
|
||||||
channel._typing.delete(user.id);
|
|
||||||
}, 10000);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user