From aceb5b366c37e1d930a849b7c5d21662008ce052 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] 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 234718c50c79e..c61ce7d3f59b2 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -95,7 +95,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 0ac2e6cd9ffeb..7c79f1eec439d 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 d8e3e0a4e1f0a..0ebc1449a4c0b 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2558,7 +2558,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;