From 5e4654ee07b5408a2f8dbb7f52693239dacc203e Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 11 May 2019 14:58:46 -0400 Subject: [PATCH] Backport news/store channels --- src/client/actions/ChannelUpdate.js | 45 +++++++++++++++++++++++++++-- src/index.js | 2 ++ src/structures/Channel.js | 2 ++ src/structures/NewsChannel.js | 24 +++++++++++++++ src/structures/StoreChannel.js | 25 ++++++++++++++++ src/util/Constants.js | 2 ++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/structures/NewsChannel.js create mode 100644 src/structures/StoreChannel.js diff --git a/src/client/actions/ChannelUpdate.js b/src/client/actions/ChannelUpdate.js index 43158b94c..4c94f9c02 100644 --- a/src/client/actions/ChannelUpdate.js +++ b/src/client/actions/ChannelUpdate.js @@ -1,15 +1,56 @@ const Action = require('./Action'); +const Channel = require('../../structures/Channel'); +const TextChannel = require('../../structures/TextChannel'); +const VoiceChannel = require('../../structures/VoiceChannel'); +const CategoryChannel = require('../../structures/CategoryChannel'); +const NewsChannel = require('../../structures/NewsChannel'); +const StoreChannel = require('../../structures/StoreChannel'); const Constants = require('../../util/Constants'); +const ChannelTypes = Constants.ChannelTypes; const Util = require('../../util/Util'); class ChannelUpdateAction extends Action { handle(data) { const client = this.client; - const channel = client.channels.get(data.id); + let channel = client.channels.get(data.id); if (channel) { const oldChannel = Util.cloneObject(channel); - channel.setup(data); + + // If the channel is changing types, we need to follow a different process + if (ChannelTypes[channel.type.toUpperCase()] !== data.type) { + // Determine which channel class we're changing to + let channelClass; + switch (data.type) { + case ChannelTypes.TEXT: + channelClass = TextChannel; + break; + case ChannelTypes.VOICE: + channelClass = VoiceChannel; + break; + case ChannelTypes.CATEGORY: + channelClass = CategoryChannel; + break; + case ChannelTypes.NEWS: + channelClass = NewsChannel; + break; + case ChannelTypes.STORE: + channelClass = StoreChannel; + break; + } + + // Create the new channel instance and copy over cached data + const newChannel = new channelClass(channel.guild, data); + if (channel.messages && newChannel.messages) { + for (const [id, message] of channel.messages) newChannel.messages.set(id, message); + } + + channel = newChannel; + this.client.channels.set(channel.id, channel); + } else { + channel.setup(data); + } + client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel); return { old: oldChannel, diff --git a/src/index.js b/src/index.js index da33d0dbc..4624694ad 100644 --- a/src/index.js +++ b/src/index.js @@ -47,6 +47,7 @@ module.exports = { MessageEmbed: require('./structures/MessageEmbed'), MessageMentions: require('./structures/MessageMentions'), MessageReaction: require('./structures/MessageReaction'), + NewsChannel: require('./structures/NewsChannel'), OAuth2Application: require('./structures/OAuth2Application'), ClientOAuth2Application: require('./structures/OAuth2Application'), PartialGuild: require('./structures/PartialGuild'), @@ -57,6 +58,7 @@ module.exports = { ReactionCollector: require('./structures/ReactionCollector'), RichEmbed: require('./structures/RichEmbed'), Role: require('./structures/Role'), + StoreChannel: require('./structures/StoreChannel'), TextChannel: require('./structures/TextChannel'), User: require('./structures/User'), VoiceChannel: require('./structures/VoiceChannel'), diff --git a/src/structures/Channel.js b/src/structures/Channel.js index e5263ccc7..732945d7f 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -20,6 +20,8 @@ class Channel { * * `text` - a guild text channel * * `voice` - a guild voice channel * * `category` - a guild category channel + * * `news` - a guild news channel + * * `store` - a guild store channel * @type {string} */ this.type = null; diff --git a/src/structures/NewsChannel.js b/src/structures/NewsChannel.js new file mode 100644 index 000000000..bcbfbf3a8 --- /dev/null +++ b/src/structures/NewsChannel.js @@ -0,0 +1,24 @@ +const TextChannel = require('./TextChannel'); + +/** + * Represents a guild news channel on Discord. + * @extends {TextChannel} + */ +class NewsChannel extends TextChannel { + constructor(guild, data) { + super(guild, data); + this.type = 'news'; + } + + setup(data) { + super.setup(data); + + /** + * The ratelimit per user for this channel (always 0) + * @type {number} + */ + this.rateLimitPerUser = 0; + } +} + +module.exports = NewsChannel; diff --git a/src/structures/StoreChannel.js b/src/structures/StoreChannel.js new file mode 100644 index 000000000..8985c0f78 --- /dev/null +++ b/src/structures/StoreChannel.js @@ -0,0 +1,25 @@ +const GuildChannel = require('./GuildChannel'); + +/** + * Represents a guild store channel on Discord. + * @extends {GuildChannel} + */ +class StoreChannel extends GuildChannel { + constructor(guild, data) { + super(guild, data); + this.type = 'store'; + } + + setup(data) { + super.setup(data); + + /** + * If the guild considers this channel NSFW + * @type {boolean} + * @readonly + */ + this.nsfw = data.nsfw; + } +} + +module.exports = StoreChannel; diff --git a/src/util/Constants.js b/src/util/Constants.js index 7a8e13443..9110c42dc 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -282,6 +282,8 @@ exports.ChannelTypes = { VOICE: 2, GROUP_DM: 3, CATEGORY: 4, + NEWS: 5, + STORE: 6, }; exports.OPCodes = {