From 06e69d419a6b2432987c6dd2f71cff6cfc22c17e Mon Sep 17 00:00:00 2001 From: imnaiyar <137700126+imnaiyar@users.noreply.github.com> Date: Fri, 13 Sep 2024 07:25:46 -0500 Subject: [PATCH 1/5] fix(PartialGroupDMChannel): add missing ownerId property --- .../src/structures/PartialGroupDMChannel.js | 15 +++++++++++++++ packages/discord.js/typings/index.d.ts | 2 ++ 2 files changed, 17 insertions(+) diff --git a/packages/discord.js/src/structures/PartialGroupDMChannel.js b/packages/discord.js/src/structures/PartialGroupDMChannel.js index 704c8655753c..0ac2e6cd9ffe 100644 --- a/packages/discord.js/src/structures/PartialGroupDMChannel.js +++ b/packages/discord.js/src/structures/PartialGroupDMChannel.js @@ -44,6 +44,12 @@ class PartialGroupDMChannel extends BaseChannel { * @type {PartialGroupDMMessageManager} */ this.messages = new PartialGroupDMMessageManager(this); + + /** + * The user id of the owner of this Group DM Channel + * @type {Snowflake} + */ + this.ownerId = data.owner_id; } /** @@ -55,6 +61,15 @@ class PartialGroupDMChannel extends BaseChannel { return this.icon && this.client.rest.cdn.channelIcon(this.id, this.icon, options); } + /** + * Fetches the owner of this Group DM Channel. + * @param {BaseFetchOptions} [options] The options for fetching the user + * @returns {Promise} + */ + async fetchOwner(options) { + return this.client.users.fetch(this.ownerId, options); + } + delete() { return Promise.reject(new DiscordjsError(ErrorCodes.DeleteGroupDMChannel)); } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index bd3cb855c498..003ad7bf6620 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2547,7 +2547,9 @@ export class PartialGroupDMChannel extends BaseChannel { public icon: string | null; public recipients: PartialRecipient[]; public messages: PartialGroupDMMessageManager; + public ownerId: Snowflake; public iconURL(options?: ImageURLOptions): string | null; + public fetchOwner(options?: BaseFetchOptions): Promise; public toString(): ChannelMention; } From 19cfe9b4d8e98eb40d9fd9cf49050dc7b108fd99 Mon Sep 17 00:00:00 2001 From: imnaiyar <137700126+imnaiyar@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:26:27 -0500 Subject: [PATCH 2/5] refactor: make ownerID nullable --- packages/discord.js/src/errors/Messages.js | 3 ++- .../src/structures/PartialGroupDMChannel.js | 18 +++++++++++++----- packages/discord.js/typings/index.d.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 2ee1ab3405e4..1093f5de015a 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -70,7 +70,8 @@ const Messages = { [DjsErrorCodes.ChannelNotCached]: 'Could not find the channel where this message came from in the cache!', [DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.', [DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.', - [DjsErrorCodes.FetchOwnerId]: type => `Couldn't resolve the ${type} ownerId to fetch the ${type} member.`, + [DjsErrorCodes.FetchOwnerId]: type => + `Couldn't resolve the ${type} ownerId to fetch the ${type} ${type === 'group DM' ? 'owner' : 'member'}.`, [DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`, [DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`, diff --git a/packages/discord.js/src/structures/PartialGroupDMChannel.js b/packages/discord.js/src/structures/PartialGroupDMChannel.js index 0ac2e6cd9ffe..7c79f1eec439 100644 --- a/packages/discord.js/src/structures/PartialGroupDMChannel.js +++ b/packages/discord.js/src/structures/PartialGroupDMChannel.js @@ -45,11 +45,15 @@ class PartialGroupDMChannel extends BaseChannel { */ this.messages = new PartialGroupDMMessageManager(this); - /** - * The user id of the owner of this Group DM Channel - * @type {Snowflake} - */ - this.ownerId = data.owner_id; + if ('owner_id' in data) { + /** + * The user id of the owner of this Group DM Channel + * @type {?Snowflake} + */ + this.ownerId = data.owner_id; + } else { + this.ownerId ??= null; + } } /** @@ -67,6 +71,10 @@ class PartialGroupDMChannel extends BaseChannel { * @returns {Promise} */ async fetchOwner(options) { + if (!this.ownerId) { + throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'group DM'); + } + return this.client.users.fetch(this.ownerId, options); } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 003ad7bf6620..cdcb0d67b74f 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2547,7 +2547,7 @@ export class PartialGroupDMChannel extends BaseChannel { public icon: string | null; public recipients: PartialRecipient[]; public messages: PartialGroupDMMessageManager; - public ownerId: Snowflake; + public ownerId: Snowflake | null; public iconURL(options?: ImageURLOptions): string | null; public fetchOwner(options?: BaseFetchOptions): Promise; public toString(): ChannelMention; From 5df526002adbb0547f19cefb9f37890afc9508d1 Mon Sep 17 00:00:00 2001 From: imnaiyar <137700126+imnaiyar@users.noreply.github.com> Date: Mon, 23 Sep 2024 01:55:01 -0500 Subject: [PATCH 3/5] feat: add last_message_id & last_pin_timestamp prop --- .../src/structures/PartialGroupDMChannel.js | 29 +++++++++++++++++++ packages/discord.js/typings/index.d.ts | 3 ++ 2 files changed, 32 insertions(+) diff --git a/packages/discord.js/src/structures/PartialGroupDMChannel.js b/packages/discord.js/src/structures/PartialGroupDMChannel.js index 7c79f1eec439..935ca5f5c65c 100644 --- a/packages/discord.js/src/structures/PartialGroupDMChannel.js +++ b/packages/discord.js/src/structures/PartialGroupDMChannel.js @@ -54,6 +54,35 @@ class PartialGroupDMChannel extends BaseChannel { } else { this.ownerId ??= null; } + + if ('last_message_id' in data) { + /** + * The channel's last message id, if one was sent + * @type {?Snowflake} + */ + this.lastMessageId = data.last_message_id; + } else { + this.lastMessageId ??= null; + } + + if ('last_pin_timestamp' in data) { + /** + * The timestamp when the last pinned message was pinned, if there was one + * @type {?number} + */ + this.lastPinTimestamp = data.last_pin_timestamp; + } else { + this.lastPinTimestamp ??= null; + } + } + + /** + * The date when the last pinned message was pinned, if there was one + * @type {?Date} + * @readonly + */ + get lastPinAt() { + return this.lastPinTimestamp && new Date(this.lastPinTimestamp); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index cdcb0d67b74f..cd80f8b265a8 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2548,6 +2548,9 @@ export class PartialGroupDMChannel extends BaseChannel { public recipients: PartialRecipient[]; public messages: PartialGroupDMMessageManager; public ownerId: Snowflake | null; + public lastMessageId: Snowflake | null; + public lastPinTimestamp: number | null; + get lastPinAt(): Date | null; public iconURL(options?: ImageURLOptions): string | null; public fetchOwner(options?: BaseFetchOptions): Promise; public toString(): ChannelMention; From 0a5322cb5660e5371cd550a7a6675ddec6965397 Mon Sep 17 00:00:00 2001 From: imnaiyar <137700126+imnaiyar@users.noreply.github.com> Date: Fri, 27 Sep 2024 01:52:01 -0500 Subject: [PATCH 4/5] feat: add component collector methods --- .../src/structures/PartialGroupDMChannel.js | 32 +++++++++++++------ packages/discord.js/typings/index.d.ts | 23 +++++++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/packages/discord.js/src/structures/PartialGroupDMChannel.js b/packages/discord.js/src/structures/PartialGroupDMChannel.js index 935ca5f5c65c..0e2bff1244f6 100644 --- a/packages/discord.js/src/structures/PartialGroupDMChannel.js +++ b/packages/discord.js/src/structures/PartialGroupDMChannel.js @@ -1,12 +1,14 @@ 'use strict'; const { BaseChannel } = require('./BaseChannel'); +const TextBasedChannel = require('./interfaces/TextBasedChannel'); const { DiscordjsError, ErrorCodes } = require('../errors'); const PartialGroupDMMessageManager = require('../managers/PartialGroupDMMessageManager'); /** * Represents a Partial Group DM Channel on Discord. * @extends {BaseChannel} + * @implements {TextBasedChannel} */ class PartialGroupDMChannel extends BaseChannel { constructor(client, data) { @@ -70,21 +72,12 @@ class PartialGroupDMChannel extends BaseChannel { * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ - this.lastPinTimestamp = data.last_pin_timestamp; + this.lastPinTimestamp = Date.parse(data.last_pin_timestamp); } else { this.lastPinTimestamp ??= null; } } - /** - * The date when the last pinned message was pinned, if there was one - * @type {?Date} - * @readonly - */ - get lastPinAt() { - return this.lastPinTimestamp && new Date(this.lastPinTimestamp); - } - /** * The URL to this channel's icon. * @param {ImageURLOptions} [options={}] Options for the image URL @@ -114,6 +107,25 @@ class PartialGroupDMChannel extends BaseChannel { fetch() { return Promise.reject(new DiscordjsError(ErrorCodes.FetchGroupDMChannel)); } + + // These are here only for documentation purposes - they are implemented by TextBasedChannel + /* eslint-disable no-empty-function */ + get lastMessage() {} + get lastPinAt() {} + createMessageComponentCollector() {} + awaitMessageComponent() {} } +TextBasedChannel.applyToClass(PartialGroupDMChannel, true, [ + 'bulkDelete', + 'send', + 'sendTyping', + 'createMessageCollector', + 'awaitMessages', + 'fetchWebhooks', + 'createWebhook', + 'setRateLimitPerUser', + 'setNSFW', +]); + module.exports = PartialGroupDMChannel; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index cd80f8b265a8..4299432f6771 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1305,7 +1305,7 @@ export interface ResolvedFile { // tslint:disable-next-line no-empty-interface export interface DMChannel extends Omit< - TextBasedChannelFields, + TextBasedChannelFields, 'bulkDelete' | 'fetchWebhooks' | 'createWebhook' | 'setRateLimitPerUser' | 'setNSFW' > {} export class DMChannel extends BaseChannel { @@ -2539,6 +2539,19 @@ export class OAuth2Guild extends BaseGuild { public permissions: Readonly; } +export interface PartialGroupDMChannel + extends Omit< + TextBasedChannelFields, + | 'bulkDelete' + | 'send' + | 'sendTyping' + | 'createMessageCollector' + | 'awaitMessages' + | 'fetchWebhooks' + | 'createWebhook' + | 'setRateLimitPerUser' + | 'setNSFW' + > {} export class PartialGroupDMChannel extends BaseChannel { private constructor(client: Client, data: RawPartialGroupDMChannelData); public type: ChannelType.GroupDM; @@ -2546,11 +2559,7 @@ export class PartialGroupDMChannel extends BaseChannel { public name: string | null; public icon: string | null; public recipients: PartialRecipient[]; - public messages: PartialGroupDMMessageManager; public ownerId: Snowflake | null; - public lastMessageId: Snowflake | null; - public lastPinTimestamp: number | null; - get lastPinAt(): Date | null; public iconURL(options?: ImageURLOptions): string | null; public fetchOwner(options?: BaseFetchOptions): Promise; public toString(): ChannelMention; @@ -4492,13 +4501,13 @@ export interface PartialTextBasedChannelFields>; } -export interface TextBasedChannelFields +export interface TextBasedChannelFields extends PartialTextBasedChannelFields { lastMessageId: Snowflake | null; get lastMessage(): Message | null; lastPinTimestamp: number | null; get lastPinAt(): Date | null; - messages: If; + messages: If>; awaitMessageComponent( options?: AwaitMessageCollectorOptionsParams, ): Promise; From 21dfa0e8f4bd6e470c8efcf64a1177f5badba314 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:12:17 +0600 Subject: [PATCH 5/5] fix: handle null case Co-authored-by: Vlad Frangu --- packages/discord.js/src/structures/PartialGroupDMChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/PartialGroupDMChannel.js b/packages/discord.js/src/structures/PartialGroupDMChannel.js index 0e2bff1244f6..144c2091bbd2 100644 --- a/packages/discord.js/src/structures/PartialGroupDMChannel.js +++ b/packages/discord.js/src/structures/PartialGroupDMChannel.js @@ -72,7 +72,7 @@ class PartialGroupDMChannel extends BaseChannel { * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ - this.lastPinTimestamp = Date.parse(data.last_pin_timestamp); + this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null; } else { this.lastPinTimestamp ??= null; }