refactor(Channel): change channel types to UPPER_CASE (#6035)

This commit is contained in:
Rodry
2021-07-08 21:32:19 +01:00
committed by GitHub
parent b170fb5ce8
commit 6301728d35
26 changed files with 190 additions and 154 deletions

View File

@@ -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
@@ -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);
}
@@ -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;

View File

@@ -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}

View File

@@ -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),
),

View File

@@ -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');
@@ -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
@@ -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),
);
@@ -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,
@@ -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;
}

View File

@@ -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 &&
@@ -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);
@@ -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 });

View File

@@ -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,
});
}

View File

@@ -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);

View File

@@ -346,7 +346,7 @@ class ThreadChannel extends Channel {
!this.archived &&
!this.joined &&
this.permissionsFor(this.client.user)?.has(
this.type === 'private_thread' ? Permissions.FLAGS.MANAGE_THREADS : Permissions.FLAGS.VIEW_CHANNEL,
this.type === 'GUILD_PRIVATE_THREAD' ? Permissions.FLAGS.MANAGE_THREADS : Permissions.FLAGS.VIEW_CHANNEL,
false,
)
);
@@ -373,7 +373,9 @@ class ThreadChannel extends Channel {
this.permissionsFor(this.client.user)?.any(
[
Permissions.FLAGS.SEND_MESSAGES,
this.type === 'private_thread' ? Permissions.FLAGS.USE_PRIVATE_THREADS : Permissions.FLAGS.USE_PUBLIC_THREADS,
this.type === 'GUILD_PRIVATE_THREAD'
? Permissions.FLAGS.USE_PRIVATE_THREADS
: Permissions.FLAGS.USE_PUBLIC_THREADS,
],
false,
)

View File

@@ -193,7 +193,7 @@ class User extends Base {
* @readonly
*/
get dmChannel() {
return this.client.channels.cache.find(c => c.type === 'dm' && c.recipient.id === this.id) ?? null;
return this.client.channels.cache.find(c => c.type === 'DM' && c.recipient.id === this.id) ?? null;
}
/**

View File

@@ -171,7 +171,7 @@ class VoiceState extends Base {
* @returns {Promise<void>}
*/
async setRequestToSpeak(request) {
if (this.channel?.type !== 'stage') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.client.user.id !== this.id) throw new Error('VOICE_STATE_NOT_OWN');
@@ -203,7 +203,7 @@ class VoiceState extends Base {
async setSuppressed(suppressed) {
if (typeof suppressed !== 'boolean') throw new TypeError('VOICE_STATE_INVALID_TYPE', 'suppressed');
if (this.channel?.type !== 'stage') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
const target = this.client.user.id === this.id ? '@me' : this.id;