Channel.js (3998B)
1 'use strict'; 2 3 const Base = require('./Base'); 4 const { ChannelTypes } = require('../util/Constants'); 5 const Snowflake = require('../util/Snowflake'); 6 7 /** 8 * Represents any channel on Discord. 9 * @extends {Base} 10 */ 11 class Channel extends Base { 12 constructor(client, data) { 13 super(client); 14 15 const type = Object.keys(ChannelTypes)[data.type]; 16 /** 17 * The type of the channel, either: 18 * * `dm` - a DM channel 19 * * `text` - a guild text channel 20 * * `voice` - a guild voice channel 21 * * `category` - a guild category channel 22 * * `news` - a guild news channel 23 * * `store` - a guild store channel 24 * * `unknown` - a generic channel of unknown type, could be Channel or GuildChannel 25 * @type {string} 26 */ 27 this.type = type ? type.toLowerCase() : 'unknown'; 28 29 /** 30 * Whether the channel has been deleted 31 * @type {boolean} 32 */ 33 this.deleted = false; 34 35 if (data) this._patch(data); 36 } 37 38 _patch(data) { 39 /** 40 * The unique ID of the channel 41 * @type {Snowflake} 42 */ 43 this.id = data.id; 44 } 45 46 /** 47 * The timestamp the channel was created at 48 * @type {number} 49 * @readonly 50 */ 51 get createdTimestamp() { 52 return Snowflake.deconstruct(this.id).timestamp; 53 } 54 55 /** 56 * The time the channel was created at 57 * @type {Date} 58 * @readonly 59 */ 60 get createdAt() { 61 return new Date(this.createdTimestamp); 62 } 63 64 /** 65 * When concatenated with a string, this automatically returns the channel's mention instead of the Channel object. 66 * @returns {string} 67 * @example 68 * // Logs: Hello from <#123456789012345678>! 69 * console.log(`Hello from ${channel}!`); 70 */ 71 toString() { 72 return `<#${this.id}>`; 73 } 74 75 /** 76 * Deletes this channel. 77 * @returns {Promise<Channel>} 78 * @example 79 * // Delete the channel 80 * channel.delete() 81 * .then(console.log) 82 * .catch(console.error); 83 */ 84 delete() { 85 return this.client.api 86 .channels(this.id) 87 .delete() 88 .then(() => this); 89 } 90 91 /** 92 * Fetches this channel. 93 * @returns {Promise<Channel>} 94 */ 95 fetch() { 96 return this.client.channels.fetch(this.id, true); 97 } 98 99 static create(client, data, guild) { 100 const Structures = require('../util/Structures'); 101 let channel; 102 if (!data.guild_id && !guild) { 103 if ((data.recipients && data.type !== ChannelTypes.GROUP) || data.type === ChannelTypes.DM) { 104 const DMChannel = Structures.get('DMChannel'); 105 channel = new DMChannel(client, data); 106 } else if (data.type === ChannelTypes.GROUP) { 107 const PartialGroupDMChannel = require('./PartialGroupDMChannel'); 108 channel = new PartialGroupDMChannel(client, data); 109 } 110 } else { 111 guild = guild || client.guilds.cache.get(data.guild_id); 112 if (guild) { 113 switch (data.type) { 114 case ChannelTypes.TEXT: { 115 const TextChannel = Structures.get('TextChannel'); 116 channel = new TextChannel(guild, data); 117 break; 118 } 119 case ChannelTypes.VOICE: { 120 const VoiceChannel = Structures.get('VoiceChannel'); 121 channel = new VoiceChannel(guild, data); 122 break; 123 } 124 case ChannelTypes.CATEGORY: { 125 const CategoryChannel = Structures.get('CategoryChannel'); 126 channel = new CategoryChannel(guild, data); 127 break; 128 } 129 case ChannelTypes.NEWS: { 130 const NewsChannel = Structures.get('NewsChannel'); 131 channel = new NewsChannel(guild, data); 132 break; 133 } 134 case ChannelTypes.STORE: { 135 const StoreChannel = Structures.get('StoreChannel'); 136 channel = new StoreChannel(guild, data); 137 break; 138 } 139 } 140 if (channel) guild.channels.cache.set(channel.id, channel); 141 } 142 } 143 return channel; 144 } 145 146 toJSON(...props) { 147 return super.toJSON({ createdTimestamp: true }, ...props); 148 } 149 } 150 151 module.exports = Channel;