Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GuildChannelManager): add 'fetch' method #4966

Merged
merged 13 commits into from Jun 9, 2021
31 changes: 31 additions & 0 deletions src/managers/GuildChannelManager.js
Expand Up @@ -3,6 +3,7 @@
const BaseManager = require('./BaseManager');
const GuildChannel = require('../structures/GuildChannel');
const PermissionOverwrites = require('../structures/PermissionOverwrites');
const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants');

/**
Expand Down Expand Up @@ -126,6 +127,36 @@ class GuildChannelManager extends BaseManager {
});
return this.client.actions.ChannelCreate.handle(data).channel;
}

/**
* Obtains one or more guild channels from Discord, or the channel cache if they're already available.
* @param {Snowflake} [id] ID of the channel
* @param {boolean} [cache=true] Whether to cache the new channel objects if it weren't already
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<GuildChannel|Collection<Snowflake, GuildChannel>>}
Extroonie marked this conversation as resolved.
Show resolved Hide resolved
* @example
* // Fetch all channels from the guild
* message.guild.channels.fetch()
* .then(channels => console.log(`There are ${channels.size} channels.`))
* .catch(console.error);
* @example
* // Fetch a single channel
* message.guild.channels.fetch('222197033908436994')
* .then(channel => console.log(`The channel name is: ${channel.name}`))
* .catch(console.error);
*/
async fetch(id, cache = true, force = false) {
if (id && !force) {
const existing = this.cache.get(id);
if (existing) return existing;
}

// We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404
Extroonie marked this conversation as resolved.
Show resolved Hide resolved
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.add(channel, cache));
Extroonie marked this conversation as resolved.
Show resolved Hide resolved
return id ? channels.get(id) || null : channels;
}
}

module.exports = GuildChannelManager;
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -1913,6 +1913,8 @@ declare module 'discord.js' {
name: string,
options: GuildCreateChannelOptions,
): Promise<TextChannel | VoiceChannel | CategoryChannel>;
public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise<GuildChannel | null>;
public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise<Collection<Snowflake, GuildChannel>>;
Extroonie marked this conversation as resolved.
Show resolved Hide resolved
}

export class GuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
Expand Down