refactor(ThreadChannel): use single thread member endpoint (#10136)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Danial Raza
2024-03-24 14:41:29 +00:00
committed by GitHub
parent 6cc5fa28e6
commit f500ad6e2e
3 changed files with 18 additions and 11 deletions

View File

@@ -95,7 +95,7 @@ const Messages = {
[DjsErrorCodes.ChannelNotCached]: 'Could not find the channel where this message came from in the cache!', [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.StageChannelResolve]: 'Could not resolve channel to a stage channel.',
[DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.', [DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.',
[DjsErrorCodes.FetchOwnerId]: "Couldn't resolve the guild ownerId to fetch the member.", [DjsErrorCodes.FetchOwnerId]: type => `Couldn't resolve the ${type} ownerId to fetch the ${type} member.`,
[DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`, [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}`, [DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,

View File

@@ -495,7 +495,7 @@ class Guild extends AnonymousGuild {
*/ */
async fetchOwner(options) { async fetchOwner(options) {
if (!this.ownerId) { if (!this.ownerId) {
throw new DiscordjsError(ErrorCodes.FetchOwnerId); throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'guild');
} }
const member = await this.members.fetch({ ...options, user: this.ownerId }); const member = await this.members.fetch({ ...options, user: this.ownerId });
return member; return member;

View File

@@ -1,11 +1,12 @@
'use strict'; 'use strict';
const { DiscordAPIError } = require('@discordjs/rest');
const { lazy } = require('@discordjs/util'); const { lazy } = require('@discordjs/util');
const { ChannelType, PermissionFlagsBits, Routes, ChannelFlags } = require('discord-api-types/v10'); const { RESTJSONErrorCodes, ChannelFlags, ChannelType, PermissionFlagsBits, Routes } = require('discord-api-types/v10');
const { BaseChannel } = require('./BaseChannel'); const { BaseChannel } = require('./BaseChannel');
const getThreadOnlyChannel = lazy(() => require('./ThreadOnlyChannel')); const getThreadOnlyChannel = lazy(() => require('./ThreadOnlyChannel'));
const TextBasedChannel = require('./interfaces/TextBasedChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { DiscordjsRangeError, ErrorCodes } = require('../errors'); const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
const GuildMessageManager = require('../managers/GuildMessageManager'); const GuildMessageManager = require('../managers/GuildMessageManager');
const ThreadMemberManager = require('../managers/ThreadMemberManager'); const ThreadMemberManager = require('../managers/ThreadMemberManager');
const ChannelFlagsBitField = require('../util/ChannelFlagsBitField'); const ChannelFlagsBitField = require('../util/ChannelFlagsBitField');
@@ -293,15 +294,21 @@ class ThreadChannel extends BaseChannel {
* @param {BaseFetchOptions} [options] The options for fetching the member * @param {BaseFetchOptions} [options] The options for fetching the member
* @returns {Promise<?ThreadMember>} * @returns {Promise<?ThreadMember>}
*/ */
async fetchOwner({ cache = true, force = false } = {}) { async fetchOwner(options) {
if (!force) { if (!this.ownerId) {
const existing = this.members.cache.get(this.ownerId); throw new DiscordjsError(ErrorCodes.FetchOwnerId, 'thread');
if (existing) return existing;
} }
// We cannot fetch a single thread member, as of this commit's date, Discord API responds with 405 // TODO: Remove that catch in the next major version
const members = await this.members.fetch({ cache }); const member = await this.members._fetchSingle({ ...options, user: this.ownerId }).catch(error => {
return members.get(this.ownerId) ?? null; if (error instanceof DiscordAPIError && error.code === RESTJSONErrorCodes.UnknownMember) {
return null;
}
throw error;
});
return member;
} }
/** /**