diff --git a/lib/Client.js b/lib/Client.js index e454d100b..baccf2e13 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -1,6 +1,6 @@ "use strict"; -const CategoryChannel = require("./structures/CategoryChannel"); +const Channel = require("./structures/Channel"); const Collection = require("./util/Collection"); const Constants = require("./Constants"); const Endpoints = require("./rest/Endpoints"); @@ -17,11 +17,8 @@ const Relationship = require("./structures/Relationship"); const RequestHandler = require("./rest/RequestHandler"); const Role = require("./structures/Role"); const ShardManager = require("./gateway/ShardManager"); -const TextChannel = require("./structures/TextChannel"); -const NewsChannel = require("./structures/NewsChannel"); const UnavailableGuild = require("./structures/UnavailableGuild"); const User = require("./structures/User"); -const VoiceChannel = require("./structures/VoiceChannel"); const VoiceConnectionManager = require("./voice/VoiceConnectionManager"); let EventEmitter; @@ -391,7 +388,6 @@ class Client extends EventEmitter { * @returns {Promise} */ createChannel(guildID, name, type, reason, options = {}) { - const guild = this.guilds.get(guildID); if(typeof options === "string") { // This used to be parentID, back-compat this.emit("warn", "[DEPRECATED] createChannel() was called with a string `options` argument"); options = { @@ -417,19 +413,7 @@ class Client extends EventEmitter { rate_limit_per_user: options.rateLimitPerUser, parent_id: options.parentID, permission_overwrites: options.permissionOverwrites - }).then((channel) => { - if(channel.type === 2) { - return new VoiceChannel(channel, guild); - } else if(channel.type === 4) { - return new CategoryChannel(channel, guild); - } else if(channel.type === 5) { - return new NewsChannel(channel, guild); - } else if(channel.type === 0 || channel.last_message_id !== undefined) { - return new TextChannel(channel, guild); - } else { - return channel; - } - }); + }).then((channel) => Channel.from(channel, this)); } /** @@ -460,27 +444,7 @@ class Client extends EventEmitter { rate_limit_per_user: options.rateLimitPerUser, parent_id: options.parentID, reason: reason - }).then((data) => { - if(data.guild_id) { - let guild = data.guild_id || this.channelGuildMap[channelID]; - if(guild) { - guild = this.guilds.get(guild); - } - if(data.type === 2) { - return new VoiceChannel(data, guild); - } else if(data.type === 4) { - return new CategoryChannel(data, guild); - } else if(data.type === 5) { - return new NewsChannel(data, guild); - } else if(data.type === 0 || (data.guild_id && data.last_message_id !== undefined)) { - return new TextChannel(data, guild); - } else { - return data; - } - } else { - return new GroupChannel(data, this); - } - }); + }).then((channel) => Channel.from(channel, this)); } /** @@ -1855,23 +1819,8 @@ class Client extends EventEmitter { if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true).then((channel) => { - if(channel.type === 1) { - return new PrivateChannel(channel, this); - } else if(channel.type === 2) { - return new VoiceChannel(channel, null); - } else if(channel.type === 3) { - return new GroupChannel(channel, this); - } else if(channel.type === 4) { - return new CategoryChannel(channel, null); - } else if(channel.type === 5) { - return new NewsChannel(channel, null, this.options.messageLimit); - } else if(channel.type === 0 || (channel.guild_id && channel.last_message_id !== undefined)) { - return new TextChannel(channel, null, this.options.messageLimit); - } else { - return channel; - } - }); + return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true) + .then((channel) => Channel.from(channel, this)); } /** @@ -1913,19 +1862,8 @@ class Client extends EventEmitter { if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true).then((channels) => channels.map((channel) => { - if(channel.type === 2) { - return new VoiceChannel(channel, null); - } else if(channel.type === 4) { - return new CategoryChannel(channel, null); - } else if(channel.type === 5) { - return new NewsChannel(channel, null, this.options.messageLimit); - } else if(channel.type === 0 || channel.last_message_id !== undefined) { - return new TextChannel(channel, null, this.options.messageLimit); - } else { - return channel; - } - })); + return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true) + .then((channels) => channels.map((channel) => Channel.from(channel, this))); } /** diff --git a/lib/structures/Channel.js b/lib/structures/Channel.js index d858306af..21e3ce214 100644 --- a/lib/structures/Channel.js +++ b/lib/structures/Channel.js @@ -3,7 +3,7 @@ const Base = require("./Base"); /** -* Represents a channel. You also probably want to look at CategoryChannel, GroupChannel, PrivateChannel, TextChannel, NewsChannel, and VoiceChannel. +* Represents a channel. You also probably want to look at CategoryChannel, GroupChannel, NewsChannel, PrivateChannel, TextChannel, and VoiceChannel. * @prop {String} id The ID of the channel * @prop {String} mention A string that mentions the channel * @prop {Number} type The type of the channel @@ -19,6 +19,33 @@ class Channel extends Base { return `<#${this.id}>`; } + static from(data, client) { + switch(data.type) { + case 0: { + return new TextChannel(data, client); + } + case 1: { + return new PrivateChannel(data, client); + } + case 2: { + return new VoiceChannel(data, client); + } + case 3: { + return new GroupChannel(data, client); + } + case 4: { + return new CategoryChannel(data, client); + } + case 5: { + return new NewsChannel(data, client); + } + case 6: { + return new StoreChannel(data, client); + } + } + return new Channel(data); + } + toJSON() { const base = super.toJSON(true); for(const prop of ["type"]) { @@ -31,3 +58,12 @@ class Channel extends Base { } module.exports = Channel; + +// Circular import +const CategoryChannel = require("./CategoryChannel"); +const GroupChannel = require("./GroupChannel"); +const NewsChannel = require("./NewsChannel"); +const PrivateChannel = require("./PrivateChannel"); +const StoreChannel = require("./StoreChannel"); +const TextChannel = require("./TextChannel"); +const VoiceChannel = require("./VoiceChannel"); diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 0d065ded2..b92386015 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -1,16 +1,13 @@ "use strict"; const Base = require("./Base"); -const CategoryChannel = require("./CategoryChannel"); +const Channel = require("./Channel"); const {CDN_URL} = require("../rest/Endpoints"); const Constants = require("../Constants"); const Collection = require("../util/Collection"); const GuildChannel = require("./GuildChannel"); const Member = require("./Member"); const Role = require("./Role"); -const TextChannel = require("./TextChannel"); -const NewsChannel = require("./NewsChannel"); -const VoiceChannel = require("./VoiceChannel"); const VoiceState = require("./VoiceState"); /** @@ -69,18 +66,8 @@ class Guild extends Base { } if(data.channels) { - for(let channel of data.channels) { - if(channel.type === 2) { - channel = this.channels.add(new VoiceChannel(channel, this), this); - } else if(channel.type === 4) { - channel = this.channels.add(new CategoryChannel(channel, this), this); - } else if(channel.type === 5) { - channel = this.channels.add(new NewsChannel(channel, this), this); - } else if(channel.type === 0 || channel.last_message_id !== undefined) { - channel = this.channels.add(new TextChannel(channel, this), this); - } else { - channel = this.channels.add(channel, this); - } + for(const channel of data.channels) { + this.channels.add(Channel.from(channel, client)); client.channelGuildMap[channel.id] = this.id; } } diff --git a/lib/structures/GuildChannel.js b/lib/structures/GuildChannel.js index 39ae80c8a..1ecbcf4a3 100644 --- a/lib/structures/GuildChannel.js +++ b/lib/structures/GuildChannel.js @@ -7,7 +7,7 @@ const {Permissions} = require("../Constants"); const PermissionOverwrite = require("./PermissionOverwrite"); /** -* Represents a guild channel. You also probably want to look at CategoryChannel, TextChannel, NewsChannel, and VoiceChannel. +* Represents a guild channel. You also probably want to look at CategoryChannel, NewsChannel, StoreChannel, TextChannel, and VoiceChannel. * @extends Channel * @prop {String} id The ID of the channel * @prop {String} mention A string that mentions the channel @@ -20,15 +20,11 @@ const PermissionOverwrite = require("./PermissionOverwrite"); * @prop {Collection} permissionOverwrites Collection of PermissionOverwrites in this channel */ class GuildChannel extends Channel { - constructor(data, guild) { + constructor(data, client) { super(data); - if(!guild && data.guild_id) { - this.guild = { - id: data.guild_id - }; - } else { - this.guild = guild; - } + this.guild = client.guilds.get(data.guild_id) || { + id: data.guild_id + }; this.update(data); } diff --git a/lib/structures/NewsChannel.js b/lib/structures/NewsChannel.js index 670d5c9bc..5b1f79368 100644 --- a/lib/structures/NewsChannel.js +++ b/lib/structures/NewsChannel.js @@ -26,10 +26,6 @@ class NewsChannel extends TextChannel { this.rateLimitPerUser = 0; this.update(data); } - - update(data) { - super.update(data); - } } module.exports = NewsChannel; diff --git a/lib/structures/StoreChannel.js b/lib/structures/StoreChannel.js new file mode 100644 index 000000000..bb719be2b --- /dev/null +++ b/lib/structures/StoreChannel.js @@ -0,0 +1,21 @@ +"use strict"; + +const GuildChannel = require("./GuildChannel"); + +/** +* Represents a store channel +* @extends GuildChannel +* @prop {String} id The ID of the channel +* @prop {String} mention A string that mentions the channel +* @prop {Number} type The type of the channel +* @prop {Guild} guild The guild that owns the channel +* @prop {String?} parentID The ID of the category this channel belongs to +* @prop {String} name The name of the channel +* @prop {Number} position The position of the channel +* @prop {Boolean} nsfw Whether the channel is an NSFW channel or not +* @prop {Collection} permissionOverwrites Collection of PermissionOverwrites in this channel +*/ +class StoreChannel extends GuildChannel { +} + +module.exports = StoreChannel; diff --git a/lib/structures/TextChannel.js b/lib/structures/TextChannel.js index fdcc242d2..1b939a63c 100644 --- a/lib/structures/TextChannel.js +++ b/lib/structures/TextChannel.js @@ -23,14 +23,11 @@ const Message = require("./Message"); * @prop {Number} rateLimitPerUser The ratelimit of the channel, in seconds. 0 means no ratelimit is enabled */ class TextChannel extends GuildChannel { - constructor(data, guild, messageLimit) { - super(data, guild); - if(messageLimit == null && guild) { - messageLimit = guild.shard.client.options.messageLimit; - } - this.messages = new Collection(Message, messageLimit); + constructor(data, client, messageLimit) { + super(data, client); + this.messages = new Collection(Message, messageLimit == null ? client.options.messageLimit : messageLimit); this.lastMessageID = data.last_message_id || null; - this.rateLimitPerUser = data.rate_limit_per_user == undefined ? null : data.rate_limit_per_user; + this.rateLimitPerUser = data.rate_limit_per_user == null ? null : data.rate_limit_per_user; this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null; this.update(data); }