mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
fix: Handle partial data for Typing#user (#7542)
This commit is contained in:
@@ -39,7 +39,7 @@ class UserManager extends CachedManager {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
dmChannel(userId) {
|
dmChannel(userId) {
|
||||||
return this.client.channels.cache.find(c => c.type === ChannelType.DM && c.recipient.id === userId) ?? null;
|
return this.client.channels.cache.find(c => c.type === ChannelType.DM && c.recipientId === userId) ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { userMention } = require('@discordjs/builders');
|
||||||
const { ChannelType } = require('discord-api-types/v9');
|
const { ChannelType } = require('discord-api-types/v9');
|
||||||
const { Channel } = require('./Channel');
|
const { Channel } = require('./Channel');
|
||||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||||
const MessageManager = require('../managers/MessageManager');
|
const MessageManager = require('../managers/MessageManager');
|
||||||
|
const Partials = require('../util/Partials');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a direct message channel between two users.
|
* Represents a direct message channel between two users.
|
||||||
@@ -28,11 +30,17 @@ class DMChannel extends Channel {
|
|||||||
super._patch(data);
|
super._patch(data);
|
||||||
|
|
||||||
if (data.recipients) {
|
if (data.recipients) {
|
||||||
|
const recipient = data.recipients[0];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The recipient on the other end of the DM
|
* The recipient's id
|
||||||
* @type {User}
|
* @type {Snowflake}
|
||||||
*/
|
*/
|
||||||
this.recipient = this.client.users._add(data.recipients[0]);
|
this.recipientId = recipient.id;
|
||||||
|
|
||||||
|
if ('username' in recipient || this.client.options.partials.includes(Partials.Users)) {
|
||||||
|
this.client.users._add(recipient);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('last_message_id' in data) {
|
if ('last_message_id' in data) {
|
||||||
@@ -63,13 +71,22 @@ class DMChannel extends Channel {
|
|||||||
return typeof this.lastMessageId === 'undefined';
|
return typeof this.lastMessageId === 'undefined';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The recipient on the other end of the DM
|
||||||
|
* @type {?User}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get recipient() {
|
||||||
|
return this.client.users.resolve(this.recipientId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch this DMChannel.
|
* Fetch this DMChannel.
|
||||||
* @param {boolean} [force=true] Whether to skip the cache check and request the API
|
* @param {boolean} [force=true] Whether to skip the cache check and request the API
|
||||||
* @returns {Promise<DMChannel>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
fetch(force = true) {
|
fetch(force = true) {
|
||||||
return this.recipient.createDM(force);
|
return this.client.users.createDM(this.recipientId, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,7 +98,7 @@ class DMChannel extends Channel {
|
|||||||
* console.log(`Hello from ${channel}!`);
|
* console.log(`Hello from ${channel}!`);
|
||||||
*/
|
*/
|
||||||
toString() {
|
toString() {
|
||||||
return this.recipient.toString();
|
return userMention(this.recipientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
||||||
|
|||||||
5
packages/discord.js/typings/index.d.ts
vendored
5
packages/discord.js/typings/index.d.ts
vendored
@@ -883,7 +883,8 @@ export class EnumResolvers extends null {
|
|||||||
export class DMChannel extends TextBasedChannelMixin(Channel, ['bulkDelete']) {
|
export class DMChannel extends TextBasedChannelMixin(Channel, ['bulkDelete']) {
|
||||||
private constructor(client: Client, data?: RawDMChannelData);
|
private constructor(client: Client, data?: RawDMChannelData);
|
||||||
public messages: MessageManager;
|
public messages: MessageManager;
|
||||||
public recipient: User;
|
public recipientId: Snowflake;
|
||||||
|
public get recipient(): User | null;
|
||||||
public type: ChannelType.DM;
|
public type: ChannelType.DM;
|
||||||
public fetch(force?: boolean): Promise<this>;
|
public fetch(force?: boolean): Promise<this>;
|
||||||
}
|
}
|
||||||
@@ -2256,7 +2257,7 @@ export class ThreadMemberFlagsBitField extends BitField<ThreadMemberFlagsString>
|
|||||||
export class Typing extends Base {
|
export class Typing extends Base {
|
||||||
private constructor(channel: TextBasedChannel, user: PartialUser, data?: RawTypingData);
|
private constructor(channel: TextBasedChannel, user: PartialUser, data?: RawTypingData);
|
||||||
public channel: TextBasedChannel;
|
public channel: TextBasedChannel;
|
||||||
public user: PartialUser;
|
public user: User | PartialUser;
|
||||||
public startedTimestamp: number;
|
public startedTimestamp: number;
|
||||||
public get startedAt(): Date;
|
public get startedAt(): Date;
|
||||||
public get guild(): Guild | null;
|
public get guild(): Guild | null;
|
||||||
|
|||||||
@@ -947,8 +947,9 @@ expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch(u
|
|||||||
expectType<Promise<GuildEmoji>>(guildEmojiManager.fetch('0'));
|
expectType<Promise<GuildEmoji>>(guildEmojiManager.fetch('0'));
|
||||||
|
|
||||||
declare const typing: Typing;
|
declare const typing: Typing;
|
||||||
expectType<PartialUser>(typing.user);
|
expectType<User | PartialUser>(typing.user);
|
||||||
if (typing.user.partial) expectType<null>(typing.user.username);
|
if (typing.user.partial) expectType<null>(typing.user.username);
|
||||||
|
if (!typing.user.partial) expectType<string>(typing.user.tag);
|
||||||
|
|
||||||
expectType<TextBasedChannel>(typing.channel);
|
expectType<TextBasedChannel>(typing.channel);
|
||||||
if (typing.channel.partial) expectType<undefined>(typing.channel.lastMessageId);
|
if (typing.channel.partial) expectType<undefined>(typing.channel.lastMessageId);
|
||||||
|
|||||||
Reference in New Issue
Block a user