Skip to content

Commit

Permalink
feat: allow channels from uncached guilds to be returned from fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Jul 4, 2021
1 parent ab0b3b9 commit caee450
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const Messages = {
GUILD_CHANNEL_RESOLVE: 'Could not resolve channel to a guild channel.',
GUILD_VOICE_CHANNEL_RESOLVE: 'Could not resolve channel to a guild voice channel.',
GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.',
GUILD_CHANNEL_UNOWNED: 'The fetched channel does not belong to this managers guild',
GUILD_OWNED: 'Guild is owned by the client.',
GUILD_MEMBERS_TIMEOUT: "Members didn't arrive in time.",
GUILD_UNCACHED_ME: 'The client user as a member of this guild is uncached.',
Expand Down
19 changes: 13 additions & 6 deletions src/managers/ChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ChannelManager extends CachedManager {
* @name ChannelManager#cache
*/

add(data, guild, cache = true) {
add(data, guild, cache = true, allowUnknownGuild = false) {
const existing = this.cache.get(data.id);
if (existing) {
if (cache) existing._patch(data);
Expand All @@ -30,14 +30,14 @@ class ChannelManager extends CachedManager {
return existing;
}

const channel = Channel.create(this.client, data, guild);
const channel = Channel.create(this.client, data, guild, allowUnknownGuild);

if (!channel) {
this.client.emit(Events.DEBUG, `Failed to find guild, or unknown type for channel ${data.id} ${data.type}`);
return null;
}

if (cache) this.cache.set(channel.id, channel);
if (cache && !allowUnknownGuild) this.cache.set(channel.id, channel);

return channel;
}
Expand Down Expand Up @@ -74,25 +74,32 @@ class ChannelManager extends CachedManager {
* @returns {?Snowflake}
*/

/**
* Options for fetching a channel from discord
* @typedef {BaseFetchOptions} FetchChannelOptions
* @property {boolean} [allowUnknownGuild=false] Allows the channel to be returned even if the guild is not in cache,
* it will not be cached. <warn>Many of the properties and methods on the returned channel will throw errors</warn>
*/

/**
* Obtains a channel from Discord, or the channel cache if it's already available.
* @param {Snowflake} id ID of the channel
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @param {FetchChannelOptions} [options] Additional options for this fetch
* @returns {Promise<?Channel>}
* @example
* // Fetch a channel by its id
* client.channels.fetch('222109930545610754')
* .then(channel => console.log(channel.name))
* .catch(console.error);
*/
async fetch(id, { cache = true, force = false } = {}) {
async fetch(id, { allowUnknownGuild = false, cache = true, force = false } = {}) {
if (!force) {
const existing = this.cache.get(id);
if (existing && !existing.partial) return existing;
}

const data = await this.client.api.channels(id).get();
return this.add(data, null, cache);
return this.add(data, null, cache, allowUnknownGuild);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const CachedManager = require('./CachedManager');
const { Error } = require('../errors');
const GuildChannel = require('../structures/GuildChannel');
const PermissionOverwrites = require('../structures/PermissionOverwrites');
const ThreadChannel = require('../structures/ThreadChannel');
Expand Down Expand Up @@ -164,11 +165,17 @@ class GuildChannelManager extends CachedManager {
if (existing) return existing;
}

// We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404
if (id) {
const data = await this.client.api.channels(id).get();
// Since this is the guild manager, throw if on a different guild
if (this.guild.id !== data.guild_id) throw new Error('GUILD_CHANNEL_UNOWNED');
return this.client.channels.add(data, this.guild, cache);
}

const data = await this.client.api.guilds(this.guild.id).channels.get();
const channels = new Collection();
for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this.guild, cache));
return id ? channels.get(id) ?? null : channels;
return channels;
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Channel extends Base {
return ThreadChannelTypes.includes(this.type);
}

static create(client, data, guild) {
static create(client, data, guild, allowUnknownGuild) {
if (!CategoryChannel) CategoryChannel = require('./CategoryChannel');
if (!DMChannel) DMChannel = require('./DMChannel');
if (!NewsChannel) NewsChannel = require('./NewsChannel');
Expand All @@ -148,41 +148,41 @@ class Channel extends Base {
} else {
if (!guild) guild = client.guilds.cache.get(data.guild_id);

if (guild) {
if (guild || allowUnknownGuild) {
switch (data.type) {
case ChannelTypes.TEXT: {
channel = new TextChannel(guild, data);
channel = new TextChannel(guild, data, client);
break;
}
case ChannelTypes.VOICE: {
channel = new VoiceChannel(guild, data);
channel = new VoiceChannel(guild, data, client);
break;
}
case ChannelTypes.CATEGORY: {
channel = new CategoryChannel(guild, data);
channel = new CategoryChannel(guild, data, client);
break;
}
case ChannelTypes.NEWS: {
channel = new NewsChannel(guild, data);
channel = new NewsChannel(guild, data, client);
break;
}
case ChannelTypes.STORE: {
channel = new StoreChannel(guild, data);
channel = new StoreChannel(guild, data, client);
break;
}
case ChannelTypes.STAGE: {
channel = new StageChannel(guild, data);
channel = new StageChannel(guild, data, client);
break;
}
case ChannelTypes.NEWS_THREAD:
case ChannelTypes.PUBLIC_THREAD:
case ChannelTypes.PRIVATE_THREAD: {
channel = new ThreadChannel(guild, data);
channel.parent?.threads.cache.set(channel.id, channel);
channel = new ThreadChannel(guild, data, client);
if (!allowUnknownGuild) channel.parent?.threads.cache.set(channel.id, channel);
break;
}
}
if (channel) guild.channels?.cache.set(channel.id, channel);
if (channel && !allowUnknownGuild) guild.channels?.cache.set(channel.id, channel);
}
}
return channel;
Expand Down
11 changes: 9 additions & 2 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ class GuildChannel extends Channel {
/**
* @param {Guild} guild The guild the guild channel is part of
* @param {APIChannel} data The data for the guild channel
* @param {Client} [client] A safety parameter for the client that instantiated this
*/
constructor(guild, data) {
super(guild.client, data, false);
constructor(guild, data, client) {
super(guild?.client ?? client, data, false);

/**
* The guild the channel is in
* @type {Guild}
*/
this.guild = guild;

/**
* The id of the guild the channel is in
* @type {Snowflake}
*/
this.guildID = data.guild_id;

this.parentID = this.parentID ?? null;
/**
* A manager of permission overwrites that belong to this channel
Expand Down
9 changes: 5 additions & 4 deletions src/structures/StoreChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const GuildChannel = require('./GuildChannel');
*/
class StoreChannel extends GuildChannel {
/**
* @param {*} guild The guild the store channel is part of
* @param {*} data The data for the store channel
* @param {Guild} guild The guild the store channel is part of
* @param {APIChannel} data The data for the store channel
* @param {Client} [client] A safety parameter for the client that instantiated this
*/
constructor(guild, data) {
super(guild, data);
constructor(guild, data, client) {
super(guild, data, client);

/**
* If the guild considers this channel NSFW
Expand Down
5 changes: 3 additions & 2 deletions src/structures/TextChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class TextChannel extends GuildChannel {
/**
* @param {Guild} guild The guild the text channel is part of
* @param {APIChannel} data The data for the text channel
* @param {Client} [client] A safety parameter for the client that instantiated this
*/
constructor(guild, data) {
super(guild, data);
constructor(guild, data, client) {
super(guild, data, client);
/**
* A manager of the messages sent to this channel
* @type {MessageManager}
Expand Down
11 changes: 9 additions & 2 deletions src/structures/ThreadChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ class ThreadChannel extends Channel {
/**
* @param {Guild} guild The guild the thread channel is part of
* @param {APIChannel} data The data for the thread channel
* @param {Client} [client] A safety parameter for the client that instantiated this
*/
constructor(guild, data) {
super(guild.client, data, false);
constructor(guild, data, client) {
super(guild?.client ?? client, data, false);

/**
* The guild the thread is in
* @type {Guild}
*/
this.guild = guild;

/**
* The id of the guild the channel is in
* @type {Snowflake}
*/
this.guildID = data.guild_id;

/**
* A manager of the messages sent to this thread
* @type {MessageManager}
Expand Down
16 changes: 11 additions & 5 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,13 +1016,14 @@ declare module 'discord.js' {
}

export class GuildChannel extends Channel {
constructor(guild: Guild, data?: unknown);
constructor(guild: Guild, data?: unknown, client?: Client);
private memberPermissions(member: GuildMember): Readonly<Permissions>;
private rolePermissions(role: Role): Readonly<Permissions>;

public readonly calculatedPosition: number;
public readonly deletable: boolean;
public guild: Guild;
public guildID: Snowflake;
public readonly manageable: boolean;
public readonly members: Collection<Snowflake, GuildMember>;
public name: string;
Expand Down Expand Up @@ -1883,7 +1884,7 @@ declare module 'discord.js' {
}

export class StoreChannel extends GuildChannel {
constructor(guild: Guild, data?: unknown);
constructor(guild: Guild, data?: unknown, client?: Client);
public nsfw: boolean;
public type: 'store';
}
Expand Down Expand Up @@ -1922,7 +1923,7 @@ declare module 'discord.js' {
}

export class TextChannel extends TextBasedChannel(GuildChannel) {
constructor(guild: Guild, data?: unknown);
constructor(guild: Guild, data?: unknown, client?: Client);
public defaultAutoArchiveDuration?: ThreadAutoArchiveDuration;
public messages: MessageManager;
public nsfw: boolean;
Expand All @@ -1942,13 +1943,14 @@ declare module 'discord.js' {
}

export class ThreadChannel extends TextBasedChannel(Channel) {
constructor(guild: Guild, data?: object);
constructor(guild: Guild, data?: object, client?: Client);
public archived: boolean;
public readonly archivedAt: Date;
public archiveTimestamp: number;
public autoArchiveDuration: ThreadAutoArchiveDuration;
public readonly editable: boolean;
public guild: Guild;
public guildID: Snowflake;
public readonly guildMembers: Collection<Snowflake, GuildMember>;
public readonly joinable: boolean;
public readonly joined: boolean;
Expand Down Expand Up @@ -2449,7 +2451,7 @@ declare module 'discord.js' {

export class ChannelManager extends CachedManager<Snowflake, Channel, ChannelResolvable> {
constructor(client: Client, iterable: Iterable<any>);
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<Channel | null>;
public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<Channel | null>;
}

export class GuildApplicationCommandManager extends ApplicationCommandManager<ApplicationCommand, {}, Guild> {
Expand Down Expand Up @@ -3324,6 +3326,10 @@ declare module 'discord.js' {
cache: boolean;
}

interface FetchChannelOptions extends BaseFetchOptions {
allowUnknownGuild?: boolean;
}

interface FetchGuildOptions extends BaseFetchOptions {
guild: GuildResolvable;
}
Expand Down

0 comments on commit caee450

Please sign in to comment.