Skip to content

Commit f500ad6

Browse files
refactor(ThreadChannel): use single thread member endpoint (#10136)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 6cc5fa2 commit f500ad6

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

packages/discord.js/src/errors/Messages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const Messages = {
9595
[DjsErrorCodes.ChannelNotCached]: 'Could not find the channel where this message came from in the cache!',
9696
[DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.',
9797
[DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.',
98-
[DjsErrorCodes.FetchOwnerId]: "Couldn't resolve the guild ownerId to fetch the member.",
98+
[DjsErrorCodes.FetchOwnerId]: type => `Couldn't resolve the ${type} ownerId to fetch the ${type} member.`,
9999

100100
[DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
101101
[DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,

packages/discord.js/src/structures/Guild.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class Guild extends AnonymousGuild {
495495
*/
496496
async fetchOwner(options) {
497497
if (!this.ownerId) {
498-
throw new DiscordjsError(ErrorCodes.FetchOwnerId);
498+
throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'guild');
499499
}
500500
const member = await this.members.fetch({ ...options, user: this.ownerId });
501501
return member;

packages/discord.js/src/structures/ThreadChannel.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3+
const { DiscordAPIError } = require('@discordjs/rest');
34
const { lazy } = require('@discordjs/util');
4-
const { ChannelType, PermissionFlagsBits, Routes, ChannelFlags } = require('discord-api-types/v10');
5+
const { RESTJSONErrorCodes, ChannelFlags, ChannelType, PermissionFlagsBits, Routes } = require('discord-api-types/v10');
56
const { BaseChannel } = require('./BaseChannel');
67
const getThreadOnlyChannel = lazy(() => require('./ThreadOnlyChannel'));
78
const TextBasedChannel = require('./interfaces/TextBasedChannel');
8-
const { DiscordjsRangeError, ErrorCodes } = require('../errors');
9+
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
910
const GuildMessageManager = require('../managers/GuildMessageManager');
1011
const ThreadMemberManager = require('../managers/ThreadMemberManager');
1112
const ChannelFlagsBitField = require('../util/ChannelFlagsBitField');
@@ -293,15 +294,21 @@ class ThreadChannel extends BaseChannel {
293294
* @param {BaseFetchOptions} [options] The options for fetching the member
294295
* @returns {Promise<?ThreadMember>}
295296
*/
296-
async fetchOwner({ cache = true, force = false } = {}) {
297-
if (!force) {
298-
const existing = this.members.cache.get(this.ownerId);
299-
if (existing) return existing;
297+
async fetchOwner(options) {
298+
if (!this.ownerId) {
299+
throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'thread');
300300
}
301301

302-
// We cannot fetch a single thread member, as of this commit's date, Discord API responds with 405
303-
const members = await this.members.fetch({ cache });
304-
return members.get(this.ownerId) ?? null;
302+
// TODO: Remove that catch in the next major version
303+
const member = await this.members._fetchSingle({ ...options, user: this.ownerId }).catch(error => {
304+
if (error instanceof DiscordAPIError && error.code === RESTJSONErrorCodes.UnknownMember) {
305+
return null;
306+
}
307+
308+
throw error;
309+
});
310+
311+
return member;
305312
}
306313

307314
/**

0 commit comments

Comments
 (0)