From 331a9d3ffc6e45c068bfb454e05b863130559d42 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Sat, 31 Jul 2021 13:00:33 +0100 Subject: [PATCH] feat(ThreadChannel): add fetchOwner() method (#6207) * feat(ThreadChannel): add owner and fetchOwner() * fix(ThreadChannel): remove owner and return ThreadMember with fetchOwner * fix(ThreadChannel): apply suggestions from code review Co-authored-by: SpaceEEC * fix(ThreadChannel): correctly point to the manager * docs(FetchOwnerOptions): update desc to mention threads * fix(ThreadChannel): fetchOwner can return null * docs(ThreadChannel): owner is nullable Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: SpaceEEC Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- src/structures/Guild.js | 2 +- src/structures/ThreadChannel.js | 16 ++++++++++++++++ typings/index.d.ts | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 49989d99485d..2ceefb279972 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -458,7 +458,7 @@ class Guild extends AnonymousGuild { } /** - * Options used to fetch the owner of guild. + * Options used to fetch the owner of a guild or a thread. * @typedef {Object} FetchOwnerOptions * @property {boolean} [cache=true] Whether or not to cache the fetched member * @property {boolean} [force=false] Whether to skip the cache check and request the API diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index 8c411daed902..ac91157f5c9e 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -234,6 +234,22 @@ class ThreadChannel extends Channel { return this.parent?.permissionsFor(memberOrRole) ?? null; } + /** + * Fetches the owner of this thread + * @param {FetchOwnerOptions} [options] The options for fetching the member + * @returns {Promise} + */ + async fetchOwner({ cache = true, force = false } = {}) { + if (!force) { + const existing = this.members.cache.get(this.ownerId); + if (existing) return existing; + } + + // We cannot fetch a single thread member, as of this commit's date, Discord API responds with 405 + const members = await this.members.fetch(cache); + return members.get(this.ownerId) ?? null; + } + /** * The options used to edit a thread channel * @typedef {Object} ThreadEditData diff --git a/typings/index.d.ts b/typings/index.d.ts index 0eda9a4d2fe0..9684b1c71d80 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1703,6 +1703,7 @@ export class ThreadChannel extends TextBasedChannel(Channel) { public leave(): Promise; public permissionsFor(memberOrRole: GuildMember | Role): Readonly; public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly | null; + public fetchOwner(options?: FetchOwnerOptions): Promise; public setArchived(archived?: boolean, reason?: string): Promise; public setAutoArchiveDuration( autoArchiveDuration: ThreadAutoArchiveDuration,