buddy

node MVC discord bot
Log | Files | Refs | README

TextChannel.js (4435B)


      1 'use strict';
      2 
      3 const GuildChannel = require('./GuildChannel');
      4 const Webhook = require('./Webhook');
      5 const TextBasedChannel = require('./interfaces/TextBasedChannel');
      6 const MessageManager = require('../managers/MessageManager');
      7 const Collection = require('../util/Collection');
      8 const DataResolver = require('../util/DataResolver');
      9 
     10 /**
     11  * Represents a guild text channel on Discord.
     12  * @extends {GuildChannel}
     13  * @implements {TextBasedChannel}
     14  */
     15 class TextChannel extends GuildChannel {
     16   /**
     17    * @param {Guild} guild The guild the text channel is part of
     18    * @param {Object} data The data for the text channel
     19    */
     20   constructor(guild, data) {
     21     super(guild, data);
     22     /**
     23      * A manager of the messages sent to this channel
     24      * @type {MessageManager}
     25      */
     26     this.messages = new MessageManager(this);
     27     this._typing = new Map();
     28   }
     29 
     30   _patch(data) {
     31     super._patch(data);
     32 
     33     /**
     34      * The topic of the text channel
     35      * @type {?string}
     36      */
     37     this.topic = data.topic;
     38 
     39     /**
     40      * If the guild considers this channel NSFW
     41      * @type {boolean}
     42      * @readonly
     43      */
     44     this.nsfw = data.nsfw;
     45 
     46     /**
     47      * The ID of the last message sent in this channel, if one was sent
     48      * @type {?Snowflake}
     49      */
     50     this.lastMessageID = data.last_message_id;
     51 
     52     /**
     53      * The ratelimit per user for this channel in seconds
     54      * @type {number}
     55      */
     56     this.rateLimitPerUser = data.rate_limit_per_user || 0;
     57 
     58     /**
     59      * The timestamp when the last pinned message was pinned, if there was one
     60      * @type {?number}
     61      */
     62     this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
     63 
     64     if (data.messages) for (const message of data.messages) this.messages.add(message);
     65   }
     66 
     67   /**
     68    * Sets the rate limit per user for this channel.
     69    * @param {number} rateLimitPerUser The new ratelimit in seconds
     70    * @param {string} [reason] Reason for changing the channel's ratelimits
     71    * @returns {Promise<TextChannel>}
     72    */
     73   setRateLimitPerUser(rateLimitPerUser, reason) {
     74     return this.edit({ rateLimitPerUser }, reason);
     75   }
     76 
     77   /**
     78    * Sets whether this channel is flagged as NSFW.
     79    * @param {boolean} nsfw Whether the channel should be considered NSFW
     80    * @param {string} [reason] Reason for changing the channel's NSFW flag
     81    * @returns {Promise<TextChannel>}
     82    */
     83   setNSFW(nsfw, reason) {
     84     return this.edit({ nsfw }, reason);
     85   }
     86 
     87   /**
     88    * Fetches all webhooks for the channel.
     89    * @returns {Promise<Collection<Snowflake, Webhook>>}
     90    * @example
     91    * // Fetch webhooks
     92    * channel.fetchWebhooks()
     93    *   .then(hooks => console.log(`This channel has ${hooks.size} hooks`))
     94    *   .catch(console.error);
     95    */
     96   fetchWebhooks() {
     97     return this.client.api.channels[this.id].webhooks.get().then(data => {
     98       const hooks = new Collection();
     99       for (const hook of data) hooks.set(hook.id, new Webhook(this.client, hook));
    100       return hooks;
    101     });
    102   }
    103 
    104   /**
    105    * Creates a webhook for the channel.
    106    * @param {string} name The name of the webhook
    107    * @param {Object} [options] Options for creating the webhook
    108    * @param {BufferResolvable|Base64Resolvable} [options.avatar] Avatar for the webhook
    109    * @param {string} [options.reason] Reason for creating the webhook
    110    * @returns {Promise<Webhook>} webhook The created webhook
    111    * @example
    112    * // Create a webhook for the current channel
    113    * channel.createWebhook('Snek', {
    114    *   avatar: 'https://i.imgur.com/mI8XcpG.jpg',
    115    *   reason: 'Needed a cool new Webhook'
    116    * })
    117    *   .then(console.log)
    118    *   .catch(console.error)
    119    */
    120   async createWebhook(name, { avatar, reason } = {}) {
    121     if (typeof avatar === 'string' && !avatar.startsWith('data:')) {
    122       avatar = await DataResolver.resolveImage(avatar);
    123     }
    124     return this.client.api.channels[this.id].webhooks
    125       .post({
    126         data: {
    127           name,
    128           avatar,
    129         },
    130         reason,
    131       })
    132       .then(data => new Webhook(this.client, data));
    133   }
    134 
    135   // These are here only for documentation purposes - they are implemented by TextBasedChannel
    136   /* eslint-disable no-empty-function */
    137   get lastMessage() {}
    138   get lastPinAt() {}
    139   send() {}
    140   startTyping() {}
    141   stopTyping() {}
    142   get typing() {}
    143   get typingCount() {}
    144   createMessageCollector() {}
    145   awaitMessages() {}
    146   bulkDelete() {}
    147 }
    148 
    149 TextBasedChannel.applyToClass(TextChannel, true);
    150 
    151 module.exports = TextChannel;