Skip to content

Commit

Permalink
refactor(Channel): change channel types to UPPER_CASE (discordjs#6035)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImRodry authored Jul 8, 2021
1 parent b170fb5 commit 6301728
Show file tree
Hide file tree
Showing 26 changed files with 189 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/client/actions/ChannelUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ChannelUpdateAction extends Action {
if (channel) {
const old = channel._update(data);

if (ChannelTypes[channel.type.toUpperCase()] !== data.type) {
if (ChannelTypes[channel.type] !== data.type) {
const newChannel = Channel.create(this.client, data, channel.guild);
for (const [id, message] of channel.messages.cache) newChannel.messages.cache.set(id, message);
newChannel._typing = new Map(channel._typing);
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/GuildDelete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const { Events, TextBasedChannelTypes } = require('../../util/Constants');

class GuildDeleteAction extends Action {
constructor(client) {
Expand All @@ -15,7 +15,7 @@ class GuildDeleteAction extends Action {
let guild = client.guilds.cache.get(data.id);
if (guild) {
for (const channel of guild.channels.cache.values()) {
if (channel.type === 'text') channel.stopTyping(true);
if (channel.type in TextBasedChannelTypes) channel.stopTyping(true);
}

if (data.unavailable) {
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/MessageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const { Events, VoiceBasedChannelTypes } = require('../../util/Constants');
const { PartialTypes } = require('../../util/Constants');

/*
Expand All @@ -23,7 +23,7 @@ class MessageReactionAdd extends Action {

// Verify channel
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
if (!channel || channel.type in VoiceBasedChannelTypes) return false;

// Verify message
const message = this.getMessage(data, channel);
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/MessageReactionRemove.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const { Events, VoiceBasedChannelTypes } = require('../../util/Constants');

/*
{ user_id: 'id',
Expand All @@ -20,7 +20,7 @@ class MessageReactionRemove extends Action {

// Verify channel
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
if (!channel || channel.type in VoiceBasedChannelTypes) return false;

// Verify message
const message = this.getMessage(data, channel);
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/MessageReactionRemoveAll.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const { Events, VoiceBasedChannelTypes } = require('../../util/Constants');

class MessageReactionRemoveAll extends Action {
handle(data) {
// Verify channel
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
if (!channel || channel.type in VoiceBasedChannelTypes) return false;

// Verify message
const message = this.getMessage(data, channel);
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/MessageReactionRemoveEmoji.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const { Events, VoiceBasedChannelTypes } = require('../../util/Constants');

class MessageReactionRemoveEmoji extends Action {
handle(data) {
const channel = this.getChannel(data);
if (!channel || channel.type === 'voice') return false;
if (!channel || channel.type in VoiceBasedChannelTypes) return false;

const message = this.getMessage(data, channel);
if (!message) return false;
Expand Down
5 changes: 2 additions & 3 deletions src/client/actions/TypingStart.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');
const textBasedChannelTypes = ['dm', 'text', 'news', 'news_thread', 'public_thread', 'private_thread'];
const { Events, TextBasedChannelTypes } = require('../../util/Constants');

class TypingStart extends Action {
handle(data) {
const channel = this.getChannel(data);
if (!channel) {
return;
}
if (!textBasedChannelTypes.includes(channel.type)) {
if (!(channel.type in TextBasedChannelTypes)) {
this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class GuildChannelManager extends CachedManager {
/**
* Options used to create a new channel in a guild.
* @typedef {Object} GuildChannelCreateOptions
* @property {string} [type='text'] The type of the new channel, either `text`, `voice`, `category`, `news`,
* `store`, or `stage`
* @property {string|number} [type='GUILD_TEXT'] The type of the new channel, either `GUILD_TEXT`, `GUILD_VOICE`,
* `GUILD_CATEGORY`, `GUILD_NEWS`, `GUILD_STORE`, or `GUILD_STAGE_VOICE`
* @property {string} [topic] The topic for the new channel
* @property {boolean} [nsfw] Whether the new channel is nsfw
* @property {number} [bitrate] Bitrate of the new channel in bits (only voice)
Expand All @@ -107,7 +107,7 @@ class GuildChannelManager extends CachedManager {
* @example
* // Create a new channel with permission overwrites
* guild.channels.create('new-voice', {
* type: 'voice',
* type: 'GUILD_VOICE',
* permissionOverwrites: [
* {
* id: message.author.id,
Expand All @@ -129,7 +129,7 @@ class GuildChannelManager extends CachedManager {
data: {
name,
topic,
type: type ? ChannelTypes[type.toUpperCase()] : ChannelTypes.TEXT,
type: typeof type === 'number' ? type : ChannelTypes[type] ?? ChannelTypes.GUILD_TEXT,
nsfw,
bitrate,
user_limit: userLimit,
Expand Down
2 changes: 1 addition & 1 deletion src/managers/GuildManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GuildManager extends CachedManager {
* @property {Snowflake|number} [id] The channel's id, used to set its parent,
* this is a placeholder and will be replaced by the API after consumption
* @property {Snowflake|number} [parentId] The parent id for this channel
* @property {string} [type] The type of the channel
* @property {ChannelType} [type] The type of the channel
* @property {string} name The name of the channel
* @property {string} [topic] The topic of the text channel
* @property {boolean} [nsfw] Whether the channel is NSFW
Expand Down
15 changes: 8 additions & 7 deletions src/managers/ThreadManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class ThreadManager extends CachedManager {
* should automatically archive in case of no recent activity
* @property {MessageResolvable} [startMessage] The message to start a thread from. <warn>If this is defined then type
* of thread gets automatically defined and cannot be changed. The provided `type` field will be ignored</warn>
* @property {ThreadChannelType|number} [type] The type of thread to create. Defaults to `public_thread` if created in
* a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always
* `news_thread`</warn>
* @property {ThreadChannelTypes|number} [type] The type of thread to create. Defaults to `GUILD_PUBLIC_THREAD` if
* created in a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always
* `GUILD_NEWS_THREAD`</warn>
* @property {string} [reason] Reason for creating the thread
*/

Expand All @@ -103,7 +103,7 @@ class ThreadManager extends CachedManager {
* .create({
* name: 'mod-talk',
* autoArchiveDuration: 60,
* type: 'private_thread',
* type: 'GUILD_PRIVATE_THREAD',
* reason: 'Needed a separate thread for moderation',
* })
* .then(threadChannel => console.log(threadChannel))
Expand All @@ -114,13 +114,14 @@ class ThreadManager extends CachedManager {
if (type && typeof type !== 'string' && typeof type !== 'number') {
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number');
}
let resolvedType = this.channel.type === 'news' ? ChannelTypes.NEWS_THREAD : ChannelTypes.PUBLIC_THREAD;
let resolvedType =
this.channel.type === 'GUILD_NEWS' ? ChannelTypes.GUILD_NEWS_THREAD : ChannelTypes.GUILD_PUBLIC_THREAD;
if (startMessage) {
const startMessageId = this.channel.messages.resolveId(startMessage);
if (!startMessageId) throw new TypeError('INVALID_TYPE', 'startMessage', 'MessageResolvable');
path = path.messages(startMessageId);
} else if (this.channel.type !== 'news') {
resolvedType = typeof type === 'string' ? ChannelTypes[type.toUpperCase()] : type ?? resolvedType;
} else if (this.channel.type !== 'GUILD_NEWS') {
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType;
}

const data = await path.threads.post({
Expand Down
39 changes: 14 additions & 25 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ class Channel extends Base {

const type = 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
* * `news` - a guild news channel
* * `store` - a guild store channel
* * `news_thread` - a guild news channel's public thread channel
* * `public_thread` - a guild text channel's public thread channel
* * `private_thread` - a guild text channel's private thread channel
* * `stage` - a guild stage channel
* * `unknown` - a generic channel of unknown type, could be Channel or GuildChannel
* @type {string}
* The type of the channel
* @type {ChannelType}
*/
this.type = type?.toLowerCase() ?? 'unknown';
this.type = type ?? 'UNKNOWN';

/**
* Whether the channel has been deleted
Expand Down Expand Up @@ -139,9 +128,9 @@ class Channel extends Base {

let channel;
if (!data.guild_id && !guild) {
if ((data.recipients && data.type !== ChannelTypes.GROUP) || data.type === ChannelTypes.DM) {
if ((data.recipients && data.type !== ChannelTypes.GROUP_DM) || data.type === ChannelTypes.DM) {
channel = new DMChannel(client, data);
} else if (data.type === ChannelTypes.GROUP) {
} else if (data.type === ChannelTypes.GROUP_DM) {
const PartialGroupDMChannel = require('./PartialGroupDMChannel');
channel = new PartialGroupDMChannel(client, data);
}
Expand All @@ -150,33 +139,33 @@ class Channel extends Base {

if (guild || allowUnknownGuild) {
switch (data.type) {
case ChannelTypes.TEXT: {
case ChannelTypes.GUILD_TEXT: {
channel = new TextChannel(guild, data, client);
break;
}
case ChannelTypes.VOICE: {
case ChannelTypes.GUILD_VOICE: {
channel = new VoiceChannel(guild, data, client);
break;
}
case ChannelTypes.CATEGORY: {
case ChannelTypes.GUILD_CATEGORY: {
channel = new CategoryChannel(guild, data, client);
break;
}
case ChannelTypes.NEWS: {
case ChannelTypes.GUILD_NEWS: {
channel = new NewsChannel(guild, data, client);
break;
}
case ChannelTypes.STORE: {
case ChannelTypes.GUILD_STORE: {
channel = new StoreChannel(guild, data, client);
break;
}
case ChannelTypes.STAGE: {
case ChannelTypes.GUILD_STAGE_VOICE: {
channel = new StageChannel(guild, data, client);
break;
}
case ChannelTypes.NEWS_THREAD:
case ChannelTypes.PUBLIC_THREAD:
case ChannelTypes.PRIVATE_THREAD: {
case ChannelTypes.GUILD_NEWS_THREAD:
case ChannelTypes.GUILD_PUBLIC_THREAD:
case ChannelTypes.GUILD_PRIVATE_THREAD: {
channel = new ThreadChannel(guild, data, client);
if (!allowUnknownGuild) channel.parent?.threads.cache.set(channel.id, channel);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/DMChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DMChannel extends Channel {
constructor(client, data) {
super(client, data);
// Override the channel type so partials have a known type
this.type = 'dm';
this.type = 'DM';
/**
* A manager of the messages belonging to this channel
* @type {MessageManager}
Expand Down
6 changes: 3 additions & 3 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -1358,12 +1358,12 @@ class Guild extends AnonymousGuild {
* @private
*/
_sortedChannels(channel) {
const category = channel.type === ChannelTypes.CATEGORY;
const category = channel.type === ChannelTypes.GUILD_CATEGORY;
return Util.discordSort(
this.channels.cache.filter(
c =>
(['text', 'news', 'store'].includes(channel.type)
? ['text', 'news', 'store'].includes(c.type)
(['GUILD_TEXT', 'GUILD_NEWS', 'GUILD_STORE'].includes(channel.type)
? ['GUILD_TEXT', 'GUILD_NEWS', 'GUILD_STORE'].includes(c.type)
: c.type === channel.type) &&
(category || c.parent === channel.parent),
),
Expand Down
10 changes: 5 additions & 5 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PermissionOverwrites = require('./PermissionOverwrites');
const { Error } = require('../errors');
const PermissionOverwriteManager = require('../managers/PermissionOverwriteManager');
const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants');
const { ChannelTypes, VoiceBasedChannelTypes } = require('../util/Constants');
const Permissions = require('../util/Permissions');
const Util = require('../util/Util');

Expand Down Expand Up @@ -265,7 +265,7 @@ class GuildChannel extends Channel {
* The data for a guild channel.
* @typedef {Object} ChannelData
* @property {string} [name] The name of the channel
* @property {string} [type] The type of the the channel (only conversion between text and news is supported)
* @property {ChannelType} [type] The type of the the channel (only conversion between text and news is supported)
* @property {number} [position] The position of the channel
* @property {string} [topic] The topic of the text channel
* @property {boolean} [nsfw] Whether the channel is NSFW
Expand Down Expand Up @@ -319,7 +319,7 @@ class GuildChannel extends Channel {
if (data.lockPermissions) {
if (data.parentId) {
const newParent = this.guild.channels.resolve(data.parentId);
if (newParent?.type === 'category') {
if (newParent?.type === 'GUILD_CATEGORY') {
permission_overwrites = newParent.permissionOverwrites.cache.map(o =>
PermissionOverwrites.resolve(o, this.guild),
);
Expand All @@ -334,7 +334,7 @@ class GuildChannel extends Channel {
const newData = await this.client.api.channels(this.id).patch({
data: {
name: (data.name ?? this.name).trim(),
type: ChannelTypes[data.type?.toUpperCase()],
type: ChannelTypes[data.type],
topic: data.topic,
nsfw: data.nsfw,
bitrate: data.bitrate ?? this.bitrate,
Expand Down Expand Up @@ -567,7 +567,7 @@ class GuildChannel extends Channel {
*/
get manageable() {
if (this.client.user.id === this.guild.ownerId) return true;
if (this.type === 'voice' || this.type === 'stage') {
if (this.type in VoiceBasedChannelTypes) {
if (!this.permissionsFor(this.client.user).has(Permissions.FLAGS.CONNECT, false)) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class Message extends Base {
*/
get crosspostable() {
return (
this.channel.type === 'news' &&
this.channel.type === 'GUILD_NEWS' &&
!this.flags.has(MessageFlags.FLAGS.CROSSPOSTED) &&
this.type === 'DEFAULT' &&
this.channel.viewable &&
Expand Down Expand Up @@ -595,7 +595,7 @@ class Message extends Base {
* @returns {Promise<Message>}
* @example
* // Crosspost a message
* if (message.channel.type === 'news') {
* if (message.channel.type === 'GUILD_NEWS') {
* message.crosspost()
* .then(() => console.log('Crossposted message'))
* .catch(console.error);
Expand Down Expand Up @@ -713,7 +713,7 @@ class Message extends Base {
* @returns {Promise<ThreadChannel>}
*/
startThread(name, autoArchiveDuration, reason) {
if (!['text', 'news'].includes(this.channel.type)) {
if (!['GUILD_TEXT', 'GUILD_NEWS'].includes(this.channel.type)) {
return Promise.reject(new Error('MESSAGE_THREAD_PARENT'));
}
return this.channel.threads.create({ name, autoArchiveDuration, startMessage: this, reason });
Expand Down
2 changes: 1 addition & 1 deletion src/structures/MessageMentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MessageMentions {
this.crosspostedChannels.set(d.id, {
channelId: d.id,
guildId: d.guild_id,
type: type?.toLowerCase() ?? 'unknown',
type: type ?? 'UNKNOWN',
name: d.name,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/NewsChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NewsChannel extends TextChannel {
* @param {string} [reason] Reason for creating the webhook
* @returns {Promise<NewsChannel>}
* @example
* if (channel.type === 'news') {
* if (channel.type === 'GUILD_NEWS') {
* channel.addFollower('222197033908436994', 'Important announcements')
* .then(() => console.log('Added follower'))
* .catch(console.error);
Expand Down
Loading

0 comments on commit 6301728

Please sign in to comment.