Files
discord.js/src/structures/Channel.js
Vlad Frangu a59968f7de src: add news and store channels, and missing guild props (#3168)
* src: Implement store and news channels!

* src: Remove code dupe

* src: Add missing guild properties

* docs: Add a small notice that the channel type may also change

* src: Remove re-creation of the MessageStore

* lint: Unused Import

* src: Requested changes for StoreChannels

* typings: Fix typings

* src: Moar guild updates

* src: Set maximumPresence to the data prop, the already existent one, or default to 5000

* typings: afkChannel is a VC

I keep confusing them, ffs

Co-Authored-By: vladfrangu <kingdgrizzle@gmail.com>

* docs: Document that maximumMembers and maximumPresences may be inaccurate before fetching

* src Appels requested changes
2019-05-03 17:08:07 +02:00

142 lines
3.6 KiB
JavaScript

'use strict';
const Snowflake = require('../util/Snowflake');
const Base = require('./Base');
const { ChannelTypes } = require('../util/Constants');
/**
* Represents any channel on Discord.
* @extends {Base}
*/
class Channel extends Base {
constructor(client, data) {
super(client);
const type = Object.keys(ChannelTypes)[data.type];
/**
* The type of the channel, either:
* * `dm` - a DM channel
* * `text` - a guild text channel
* * `voice` - a guild voice channel
* * `category` - a guild category channel
* * `unknown` - a generic channel of unknown type, could be Channel or GuildChannel
* @type {string}
*/
this.type = type ? type.toLowerCase() : 'unknown';
/**
* Whether the channel has been deleted
* @type {boolean}
*/
this.deleted = false;
if (data) this._patch(data);
}
_patch(data) {
/**
* The unique ID of the channel
* @type {Snowflake}
*/
this.id = data.id;
}
/**
* The timestamp the channel was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return Snowflake.deconstruct(this.id).timestamp;
}
/**
* The time the channel was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* When concatenated with a string, this automatically returns the channel's mention instead of the Channel object.
* @returns {string}
* @example
* // Logs: Hello from <#123456789012345678>!
* console.log(`Hello from ${channel}!`);
*/
toString() {
return `<#${this.id}>`;
}
/**
* Deletes this channel.
* @returns {Promise<Channel>}
* @example
* // Delete the channel
* channel.delete()
* .then(console.log)
* .catch(console.error);
*/
delete() {
return this.client.api.channels(this.id).delete().then(() => this);
}
/**
* Fetches this channel.
* @returns {Promise<Channel>}
*/
fetch() {
return this.client.channels.fetch(this.id, true);
}
static create(client, data, guild) {
const Structures = require('../util/Structures');
let channel;
if (data.type === ChannelTypes.DM || (data.type !== ChannelTypes.GROUP && !data.guild_id && !guild)) {
const DMChannel = Structures.get('DMChannel');
channel = new DMChannel(client, data);
} else {
guild = guild || client.guilds.get(data.guild_id);
if (guild) {
switch (data.type) {
case ChannelTypes.TEXT: {
const TextChannel = Structures.get('TextChannel');
channel = new TextChannel(guild, data);
break;
}
case ChannelTypes.VOICE: {
const VoiceChannel = Structures.get('VoiceChannel');
channel = new VoiceChannel(guild, data);
break;
}
case ChannelTypes.CATEGORY: {
const CategoryChannel = Structures.get('CategoryChannel');
channel = new CategoryChannel(guild, data);
break;
}
case ChannelTypes.NEWS: {
const NewsChannel = Structures.get('NewsChannel');
channel = new NewsChannel(guild, data);
break;
}
case ChannelTypes.STORE: {
const StoreChannel = Structures.get('StoreChannel');
channel = new StoreChannel(guild, data);
break;
}
}
if (channel) guild.channels.set(channel.id, channel);
}
}
return channel;
}
toJSON(...props) {
return super.toJSON({ createdTimestamp: true }, ...props);
}
}
module.exports = Channel;