GuildChannelManager.js (4125B)
1 'use strict'; 2 3 const BaseManager = require('./BaseManager'); 4 const GuildChannel = require('../structures/GuildChannel'); 5 const PermissionOverwrites = require('../structures/PermissionOverwrites'); 6 const { ChannelTypes } = require('../util/Constants'); 7 8 /** 9 * Manages API methods for GuildChannels and stores their cache. 10 * @extends {BaseManager} 11 */ 12 class GuildChannelManager extends BaseManager { 13 constructor(guild, iterable) { 14 super(guild.client, iterable, GuildChannel); 15 16 /** 17 * The guild this Manager belongs to 18 * @type {Guild} 19 */ 20 this.guild = guild; 21 } 22 23 /** 24 * The cache of this Manager 25 * @type {Collection<Snowflake, GuildChannel>} 26 * @name GuildChannelManager#cache 27 */ 28 29 add(channel) { 30 const existing = this.cache.get(channel.id); 31 if (existing) return existing; 32 this.cache.set(channel.id, channel); 33 return channel; 34 } 35 36 /** 37 * Data that can be resolved to give a Guild Channel object. This can be: 38 * * A GuildChannel object 39 * * A Snowflake 40 * @typedef {GuildChannel|Snowflake} GuildChannelResolvable 41 */ 42 43 /** 44 * Resolves a GuildChannelResolvable to a Channel object. 45 * @method resolve 46 * @memberof GuildChannelManager 47 * @instance 48 * @param {GuildChannelResolvable} channel The GuildChannel resolvable to resolve 49 * @returns {?Channel} 50 */ 51 52 /** 53 * Resolves a GuildChannelResolvable to a channel ID string. 54 * @method resolveID 55 * @memberof GuildChannelManager 56 * @instance 57 * @param {GuildChannelResolvable} channel The GuildChannel resolvable to resolve 58 * @returns {?Snowflake} 59 */ 60 61 /** 62 * Creates a new channel in the guild. 63 * @param {string} name The name of the new channel 64 * @param {Object} [options] Options 65 * @param {string} [options.type='text'] The type of the new channel, either `text`, `voice`, or `category` 66 * @param {string} [options.topic] The topic for the new channel 67 * @param {boolean} [options.nsfw] Whether the new channel is nsfw 68 * @param {number} [options.bitrate] Bitrate of the new channel in bits (only voice) 69 * @param {number} [options.userLimit] Maximum amount of users allowed in the new channel (only voice) 70 * @param {ChannelResolvable} [options.parent] Parent of the new channel 71 * @param {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [options.permissionOverwrites] 72 * Permission overwrites of the new channel 73 * @param {number} [options.position] Position of the new channel 74 * @param {number} [options.rateLimitPerUser] The ratelimit per user for the channel 75 * @param {string} [options.reason] Reason for creating the channel 76 * @returns {Promise<GuildChannel>} 77 * @example 78 * // Create a new text channel 79 * guild.channels.create('new-general', { reason: 'Needed a cool new channel' }) 80 * .then(console.log) 81 * .catch(console.error); 82 * @example 83 * // Create a new channel with permission overwrites 84 * guild.channels.create('new-voice', { 85 * type: 'voice', 86 * permissionOverwrites: [ 87 * { 88 * id: message.author.id, 89 * deny: ['VIEW_CHANNEL'], 90 * }, 91 * ], 92 * }) 93 */ 94 async create(name, options = {}) { 95 let { 96 type, 97 topic, 98 nsfw, 99 bitrate, 100 userLimit, 101 parent, 102 permissionOverwrites, 103 position, 104 rateLimitPerUser, 105 reason, 106 } = options; 107 if (parent) parent = this.client.channels.resolveID(parent); 108 if (permissionOverwrites) { 109 permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild)); 110 } 111 112 const data = await this.client.api.guilds(this.guild.id).channels.post({ 113 data: { 114 name, 115 topic, 116 type: type ? ChannelTypes[type.toUpperCase()] : ChannelTypes.TEXT, 117 nsfw, 118 bitrate, 119 user_limit: userLimit, 120 parent_id: parent, 121 position, 122 permission_overwrites: permissionOverwrites, 123 rate_limit_per_user: rateLimitPerUser, 124 }, 125 reason, 126 }); 127 return this.client.actions.ChannelCreate.handle(data).channel; 128 } 129 } 130 131 module.exports = GuildChannelManager;