fix: Handle partial data for Typing#user (#7542)

This commit is contained in:
Jiralite
2022-03-02 09:30:25 +00:00
committed by GitHub
parent a8321d8026
commit c6cb5e9ebb
4 changed files with 28 additions and 9 deletions

View File

@@ -39,7 +39,7 @@ class UserManager extends CachedManager {
* @private
*/
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;
}
/**

View File

@@ -1,9 +1,11 @@
'use strict';
const { userMention } = require('@discordjs/builders');
const { ChannelType } = require('discord-api-types/v9');
const { Channel } = require('./Channel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const MessageManager = require('../managers/MessageManager');
const Partials = require('../util/Partials');
/**
* Represents a direct message channel between two users.
@@ -28,11 +30,17 @@ class DMChannel extends Channel {
super._patch(data);
if (data.recipients) {
const recipient = data.recipients[0];
/**
* The recipient on the other end of the DM
* @type {User}
* The recipient's id
* @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) {
@@ -63,13 +71,22 @@ class DMChannel extends Channel {
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.
* @param {boolean} [force=true] Whether to skip the cache check and request the API
* @returns {Promise<DMChannel>}
*/
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}!`);
*/
toString() {
return this.recipient.toString();
return userMention(this.recipientId);
}
// These are here only for documentation purposes - they are implemented by TextBasedChannel

View File

@@ -883,7 +883,8 @@ export class EnumResolvers extends null {
export class DMChannel extends TextBasedChannelMixin(Channel, ['bulkDelete']) {
private constructor(client: Client, data?: RawDMChannelData);
public messages: MessageManager;
public recipient: User;
public recipientId: Snowflake;
public get recipient(): User | null;
public type: ChannelType.DM;
public fetch(force?: boolean): Promise<this>;
}
@@ -2256,7 +2257,7 @@ export class ThreadMemberFlagsBitField extends BitField<ThreadMemberFlagsString>
export class Typing extends Base {
private constructor(channel: TextBasedChannel, user: PartialUser, data?: RawTypingData);
public channel: TextBasedChannel;
public user: PartialUser;
public user: User | PartialUser;
public startedTimestamp: number;
public get startedAt(): Date;
public get guild(): Guild | null;

View File

@@ -947,8 +947,9 @@ expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch(u
expectType<Promise<GuildEmoji>>(guildEmojiManager.fetch('0'));
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<string>(typing.user.tag);
expectType<TextBasedChannel>(typing.channel);
if (typing.channel.partial) expectType<undefined>(typing.channel.lastMessageId);