'use strict'; const GuildChannel = require('./GuildChannel'); const Webhook = require('./Webhook'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const MessageManager = require('../managers/MessageManager'); const ThreadManager = require('../managers/ThreadManager'); const Collection = require('../util/Collection'); const DataResolver = require('../util/DataResolver'); /** * Represents a guild text channel on Discord. * @extends {GuildChannel} * @implements {TextBasedChannel} */ class TextChannel extends GuildChannel { /** * @param {Guild} guild The guild the text channel is part of * @param {APIChannel} data The data for the text channel */ constructor(guild, data) { super(guild, data); /** * A manager of the messages sent to this channel * @type {MessageManager} */ this.messages = new MessageManager(this); /** * A manager of the threads belonging to this channel * @type {ThreadManager} */ this.threads = new ThreadManager(this); /** * If the guild considers this channel NSFW * @type {boolean} */ this.nsfw = Boolean(data.nsfw); this._typing = new Map(); } _patch(data) { super._patch(data); if ('topic' in data) { /** * The topic of the text channel * @type {?string} */ this.topic = data.topic; } if ('nsfw' in data) { this.nsfw = Boolean(data.nsfw); } if ('last_message_id' in data) { /** * The ID of the last message sent in this channel, if one was sent * @type {?Snowflake} */ this.lastMessageID = data.last_message_id; } if ('rate_limit_per_user' in data) { /** * The ratelimit per user for this channel in seconds * It is not currently possible to set a rate limit per user on a `NewsChannel`. * @type {number} */ this.rateLimitPerUser = data.rate_limit_per_user; } if ('last_pin_timestamp' in data) { /** * The timestamp when the last pinned message was pinned, if there was one * @type {?number} */ this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null; } if ('default_auto_archive_duration' in data) { /** * The default auto archive duration for newly created threads in this channel * @type {?ThreadAutoArchiveDuration} */ this.defaultAutoArchiveDuration = data.default_auto_archive_duration; } if ('messages' in data) { for (const message of data.messages) this.messages.add(message); } } /** * Sets the default auto archive duration for all newly created threads in this channel. * @param {ThreadAutoArchiveDuration} defaultAutoArchiveDuration The new default auto archive duration * @param {string} [reason] Reason for changing the channel's default auto archive duration * @returns {Promise} */ setDefaultAutoArchiveDuration(defaultAutoArchiveDuration, reason) { return this.edit({ defaultAutoArchiveDuration }, reason); } /** * Sets the rate limit per user for this channel. * It is not currently possible to set the rate limit per user on a `NewsChannel`. * @param {number} rateLimitPerUser The new ratelimit in seconds * @param {string} [reason] Reason for changing the channel's ratelimits * @returns {Promise} */ setRateLimitPerUser(rateLimitPerUser, reason) { return this.edit({ rateLimitPerUser }, reason); } /** * Sets whether this channel is flagged as NSFW. * @param {boolean} nsfw Whether the channel should be considered NSFW * @param {string} [reason] Reason for changing the channel's NSFW flag * @returns {Promise} */ setNSFW(nsfw, reason) { return this.edit({ nsfw }, reason); } /** * Sets the type of this channel (only conversion between text and news is supported) * @param {string} type The new channel type * @param {string} [reason] Reason for changing the channel's type * @returns {Promise} */ setType(type, reason) { return this.edit({ type }, reason); } /** * Fetches all webhooks for the channel. * @returns {Promise>} * @example * // Fetch webhooks * channel.fetchWebhooks() * .then(hooks => console.log(`This channel has ${hooks.size} hooks`)) * .catch(console.error); */ fetchWebhooks() { return this.client.api.channels[this.id].webhooks.get().then(data => { const hooks = new Collection(); for (const hook of data) hooks.set(hook.id, new Webhook(this.client, hook)); return hooks; }); } /** * Options used to create a {@link Webhook} for {@link TextChannel} and {@link NewsChannel}. * @typedef {Object} ChannelWebhookCreateOptions * @property {BufferResolvable|Base64Resolvable} [avatar] Avatar for the webhook * @property {string} [reason] Reason for creating the webhook */ /** * Creates a webhook for the channel. * @param {string} name The name of the webhook * @param {ChannelWebhookCreateOptions} [options] Options for creating the webhook * @returns {Promise} webhook The created webhook * @example * // Create a webhook for the current channel * channel.createWebhook('Snek', { * avatar: 'https://i.imgur.com/mI8XcpG.jpg', * reason: 'Needed a cool new Webhook' * }) * .then(console.log) * .catch(console.error) */ async createWebhook(name, { avatar, reason } = {}) { if (typeof avatar === 'string' && !avatar.startsWith('data:')) { avatar = await DataResolver.resolveImage(avatar); } return this.client.api.channels[this.id].webhooks .post({ data: { name, avatar, }, reason, }) .then(data => new Webhook(this.client, data)); } // These are here only for documentation purposes - they are implemented by TextBasedChannel /* eslint-disable no-empty-function */ get lastMessage() {} get lastPinAt() {} send() {} startTyping() {} stopTyping() {} get typing() {} get typingCount() {} createMessageCollector() {} awaitMessages() {} createMessageComponentInteractionCollector() {} awaitMessageComponentInteraction() {} bulkDelete() {} } TextBasedChannel.applyToClass(TextChannel, true); module.exports = TextChannel;