From 57977b063ef08416c0449b6b4e2ba1d401c2ed36 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sun, 13 Aug 2017 04:57:25 -0700 Subject: [PATCH] channel stuff (#1775) * channel stuff * abstract channel creation * Update Channel.js * Update Channel.js * Update Channel.js * Update Guild.js * Update Constants.js * e --- src/client/ClientDataManager.js | 25 +++------------------- src/structures/Channel.js | 36 +++++++++++++++++++++++++++++++- src/structures/DMChannel.js | 1 - src/structures/GroupDMChannel.js | 1 - src/structures/Guild.js | 4 +++- src/structures/VoiceChannel.js | 2 -- src/util/Constants.js | 2 +- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/client/ClientDataManager.js b/src/client/ClientDataManager.js index e8c5721eb..d6cc3be20 100644 --- a/src/client/ClientDataManager.js +++ b/src/client/ClientDataManager.js @@ -2,12 +2,9 @@ const Constants = require('../util/Constants'); const Util = require('../util/Util'); const Guild = require('../structures/Guild'); const User = require('../structures/User'); -const DMChannel = require('../structures/DMChannel'); -const Emoji = require('../structures/Emoji'); -const TextChannel = require('../structures/TextChannel'); -const VoiceChannel = require('../structures/VoiceChannel'); +const Channel = require('../structures/Channel'); const GuildChannel = require('../structures/GuildChannel'); -const GroupDMChannel = require('../structures/GroupDMChannel'); +const Emoji = require('../structures/Emoji'); class ClientDataManager { constructor(client) { @@ -47,23 +44,7 @@ class ClientDataManager { newChannel(data, guild) { const already = this.client.channels.has(data.id); - let channel; - if (data.type === Constants.ChannelTypes.DM) { - channel = new DMChannel(this.client, data); - } else if (data.type === Constants.ChannelTypes.GROUP_DM) { - channel = new GroupDMChannel(this.client, data); - } else { - guild = guild || this.client.guilds.get(data.guild_id); - if (guild) { - if (data.type === Constants.ChannelTypes.TEXT) { - channel = new TextChannel(guild, data); - guild.channels.set(channel.id, channel); - } else if (data.type === Constants.ChannelTypes.VOICE) { - channel = new VoiceChannel(guild, data); - guild.channels.set(channel.id, channel); - } - } - } + const channel = Channel.create(this.client, data, guild); if (channel) { if (this.pastReady && !already) this.client.emit(Constants.Events.CHANNEL_CREATE, channel); diff --git a/src/structures/Channel.js b/src/structures/Channel.js index d8f877c9a..d82545abc 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -1,4 +1,5 @@ const Snowflake = require('../util/Snowflake'); +const Constants = require('../util/Constants'); /** * Represents any channel on Discord. @@ -13,15 +14,17 @@ class Channel { */ Object.defineProperty(this, 'client', { value: client }); + const type = Object.keys(Constants.ChannelTypes)[data.type]; /** * The type of the channel, either: * * `dm` - a DM channel * * `group` - a Group DM channel * * `text` - a guild text channel * * `voice` - a guild voice channel + * * `unknown` - a generic channel of unknown type, could be Channel or GuildChannel * @type {string} */ - this.type = null; + this.type = type ? type.toLowerCase() : 'unknown'; if (data) this.setup(data); } @@ -64,6 +67,37 @@ class Channel { delete() { return this.client.api.channels(this.id).delete().then(() => this); } + + static create(client, data, guild) { + const DMChannel = require('./DMChannel'); + const GroupDMChannel = require('./GroupDMChannel'); + const TextChannel = require('./TextChannel'); + const VoiceChannel = require('./VoiceChannel'); + const GuildChannel = require('./GuildChannel'); + const types = Constants.ChannelTypes; + let channel; + if (data.type === types.DM) { + channel = new DMChannel(client, data); + } else if (data.type === types.GROUP) { + channel = new GroupDMChannel(client, data); + } else { + guild = guild || client.guilds.get(data.guild_id); + if (guild) { + switch (data.type) { + case types.TEXT: + channel = new TextChannel(guild, data); + break; + case types.VOICE: + channel = new VoiceChannel(guild, data); + break; + default: + channel = new GuildChannel(guild, data); + } + guild.channels.set(channel.id, channel); + } + } + return channel; + } } module.exports = Channel; diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 06701d14f..91d8fca1d 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -10,7 +10,6 @@ const Collection = require('../util/Collection'); class DMChannel extends Channel { constructor(client, data) { super(client, data); - this.type = 'dm'; this.messages = new Collection(); this._typing = new Map(); } diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 0511cf050..bd2f3a7ea 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -33,7 +33,6 @@ const Constants = require('../util/Constants'); class GroupDMChannel extends Channel { constructor(client, data) { super(client, data); - this.type = 'group'; this.messages = new Collection(); this._typing = new Map(); } diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 1b36eb2f4..19b1d4c33 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -960,7 +960,9 @@ class Guild { return this.client.api.guilds(this.id).channels.post({ data: { - name, type, permission_overwrites: overwrites, + name, + type: Constants.ChannelTypes[type.toUpperCase()], + permission_overwrites: overwrites, }, reason, }).then(data => this.client.actions.ChannelCreate.handle(data).channel); diff --git a/src/structures/VoiceChannel.js b/src/structures/VoiceChannel.js index f446d945e..2a67c9379 100644 --- a/src/structures/VoiceChannel.js +++ b/src/structures/VoiceChannel.js @@ -15,8 +15,6 @@ class VoiceChannel extends GuildChannel { * @type {Collection} */ this.members = new Collection(); - - this.type = 'voice'; } setup(data) { diff --git a/src/util/Constants.js b/src/util/Constants.js index 5c31eb8aa..44ea09542 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -173,7 +173,7 @@ exports.ChannelTypes = { TEXT: 0, DM: 1, VOICE: 2, - GROUP_DM: 3, + GROUP: 3, }; exports.OPCodes = {