mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
fix(TypingStart): typing methods returning falsy values (#3939)
* fix: typing methods returning falsy values * fix: eslint * fix: no provided parameters; updated jsdocs * fix(Typings): reflect typingStop * refactor: since and lastTimestamp merged into one constructable value * feat(Typings): document Channel#_typing * feat: emit TypingData on typingStart; update jsdocs * feat(Typings): move _typing to TextBasedChannel; sort props alphabetic * feat(Event): remove typingStop * feat(Typings): update typings - remove typingStop * feat(Event): remove typingStop from Constants * feat(Typing): remove TypingData class - redundant * refactor(Events): remove TYPING_STOP event * refactor(TypingData): now is an interface for _typing * fix(TypingStart): timeout variable, removed emit for TypingData * feat(Typing): timeout property on Channel#_typing, remove redundancy * fix(Typings): extra overload(s) Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * Update index.d.ts * fix(Typings): remove "private" from interface — invalid TS * feat(Typings): add PartialUser in case partials are enabled Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * feat(Typings): document 'timeout' property of TypingData Co-authored-by: Crawl <icrawltogo@gmail.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
0f38d807c7
commit
495cfa96c2
@@ -5,8 +5,32 @@ const { Events } = require('../../../util/Constants');
|
||||
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 (channel.type === 'voice') {
|
||||
client.emit(Events.WARN, `Discord sent a typing packet to a voice 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
|
||||
@@ -15,4 +39,11 @@ module.exports = (client, { d: data }) => {
|
||||
*/
|
||||
client.emit(Events.TYPING_START, channel, user);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function tooLate(channel, user) {
|
||||
return channel.client.setTimeout(() => {
|
||||
channel._typing.delete(user.id);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
23
typings/index.d.ts
vendored
23
typings/index.d.ts
vendored
@@ -1952,6 +1952,7 @@ declare module 'discord.js' {
|
||||
}
|
||||
|
||||
interface TextBasedChannelFields extends PartialTextBasedChannelFields {
|
||||
_typing: Map<string, TypingData>;
|
||||
lastPinTimestamp: number | null;
|
||||
readonly lastPinAt: Date;
|
||||
typing: boolean;
|
||||
@@ -2780,15 +2781,11 @@ declare module 'discord.js' {
|
||||
partial: true;
|
||||
fetch(): Promise<T>;
|
||||
} & {
|
||||
[K in keyof Omit<T,
|
||||
'client' |
|
||||
'createdAt' |
|
||||
'createdTimestamp' |
|
||||
'id' |
|
||||
'partial' |
|
||||
'fetch' | O>
|
||||
// tslint:disable-next-line:ban-types
|
||||
]: T[K] extends Function ? T[K] : T[K] | null;
|
||||
[K in keyof Omit<
|
||||
T,
|
||||
'client' | 'createdAt' | 'createdTimestamp' | 'id' | 'partial' | 'fetch'
|
||||
>]: // tslint:disable-next-line:ban-types
|
||||
T[K] extends Function ? T[K] : T[K] | null;
|
||||
};
|
||||
|
||||
interface PartialDMChannel extends Partialize<DMChannel,
|
||||
@@ -2960,6 +2957,14 @@ declare module 'discord.js' {
|
||||
|
||||
type TargetUser = number;
|
||||
|
||||
interface TypingData {
|
||||
user: User | PartialUser;
|
||||
since: Date;
|
||||
lastTimestamp: Date;
|
||||
elapsedTime: number;
|
||||
timeout: NodeJS.Timeout;
|
||||
}
|
||||
|
||||
type UserResolvable = User | Snowflake | Message | GuildMember;
|
||||
|
||||
type VerificationLevel = 'NONE' | 'LOW' | 'MEDIUM' | 'HIGH' | 'VERY_HIGH';
|
||||
|
||||
Reference in New Issue
Block a user