Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/discord.js/src/client/actions/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ class Action {
const payloadData = {};
const id = data.channel_id ?? data.id;

if (!('recipients' in data)) {
// Try to resolve the recipient, but do not add the client user.
if ('recipients' in data) {
// Try to resolve the recipient, but do not add if already existing in recipients.
const recipient = data.author ?? data.user ?? { id: data.user_id };
if (recipient.id !== this.client.user.id) payloadData.recipients = [recipient];
if (data.recipients.every(existingRecipient => recipient.id !== existingRecipient.id))
payloadData.recipients = [...data.recipients, recipient];
} else {
// Try to resolve the recipient.
const recipient = data.author ?? data.user ?? { id: data.user_id };
payloadData.recipients = [recipient];
}

if (id !== undefined) payloadData.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class InteractionCreateAction extends Action {
const client = this.client;

// Resolve and cache partial channels for Interaction#channel getter
const channel = data.channel && this.getChannel(data.channel);
const channel =
data.channel &&
this.getChannel({ ...data.channel, ...('recipients' in data.channel ? { user: data.user } : undefined) });

// Do not emit this for interactions that cache messages that are non-text-based.
let InteractionClass;
Expand Down
34 changes: 25 additions & 9 deletions packages/discord.js/src/structures/DMChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ class DMChannel extends BaseChannel {
super._patch(data);

if (data.recipients) {
const recipient = data.recipients[0];

/**
* The recipient's id
* The recipients' ids
*
* @type {Snowflake}
* @type {Snowflake[]}
*/
this.recipientId = recipient.id;

if ('username' in recipient || this.client.options.partials.includes(Partials.User)) {
this.client.users._add(recipient);
this.recipientIds ??= [
...new Set([...(this.recipientIds ?? []), ...data.recipients.map(recipient => recipient.id)]),
];

for (const recipient of data.recipients) {
if ('username' in recipient || this.client.options.partials.includes(Partials.User)) {
this.client.users._add(recipient);
}
}
}

Expand Down Expand Up @@ -78,7 +80,21 @@ class DMChannel extends BaseChannel {
}

/**
* The recipient on the other end of the DM
* The recipient's id, if this is a DMChannel with the client user.
*
* @type {?Snowflake}
* @readonly
*/
get recipientId() {
if (this.recipientIds.includes(this.client.user.id)) {
return this.recipientIds.find(recipientId => recipientId !== this.client.user.id) ?? null;
}

return null;
}

/**
* The recipient on the other end of the DM, if this is a DMChannel with the client user.
*
* @type {?User}
* @readonly
Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,8 @@ export interface DMChannel
export class DMChannel extends BaseChannel {
private constructor(client: Client<true>, data?: RawDMChannelData);
public flags: Readonly<ChannelFlagsBitField>;
public recipientId: Snowflake;
public get recipientId(): Snowflake | null;
public recipientIds: Snowflake[];
public get recipient(): User | null;
public type: ChannelType.DM;
public fetch(force?: boolean): Promise<this>;
Expand Down