mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
feat(*): use enums for consistency and speed (#5843)
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
@@ -13,7 +13,7 @@ const {
|
||||
ChannelTypes,
|
||||
Events,
|
||||
VerificationLevels,
|
||||
DefaultMessageNotifications,
|
||||
DefaultMessageNotificationLevels,
|
||||
ExplicitContentFilterLevels,
|
||||
} = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
@@ -168,14 +168,14 @@ class GuildManager extends BaseManager {
|
||||
} = {},
|
||||
) {
|
||||
icon = await DataResolver.resolveImage(icon);
|
||||
if (typeof verificationLevel !== 'undefined' && typeof verificationLevel !== 'number') {
|
||||
verificationLevel = VerificationLevels.indexOf(verificationLevel);
|
||||
if (typeof verificationLevel === 'string') {
|
||||
verificationLevel = VerificationLevels[verificationLevel];
|
||||
}
|
||||
if (typeof defaultMessageNotifications !== 'undefined' && typeof defaultMessageNotifications !== 'number') {
|
||||
defaultMessageNotifications = DefaultMessageNotifications.indexOf(defaultMessageNotifications);
|
||||
if (typeof defaultMessageNotifications === 'string') {
|
||||
defaultMessageNotifications = DefaultMessageNotificationLevels[defaultMessageNotifications];
|
||||
}
|
||||
if (typeof explicitContentFilter !== 'undefined' && typeof explicitContentFilter !== 'number') {
|
||||
explicitContentFilter = ExplicitContentFilterLevels.indexOf(explicitContentFilter);
|
||||
if (typeof explicitContentFilter === 'string') {
|
||||
explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter];
|
||||
}
|
||||
for (const channel of channels) {
|
||||
if (channel.type) channel.type = ChannelTypes[channel.type.toUpperCase()];
|
||||
|
||||
@@ -41,7 +41,7 @@ class ClientPresence extends Presence {
|
||||
if (!activity.type) activity.type = 0;
|
||||
|
||||
data.activities.push({
|
||||
type: typeof activity.type === 'number' ? activity.type : ActivityTypes.indexOf(activity.type),
|
||||
type: typeof activity.type === 'number' ? activity.type : ActivityTypes[activity.type],
|
||||
name: activity.name,
|
||||
url: activity.url,
|
||||
});
|
||||
@@ -50,7 +50,7 @@ class ClientPresence extends Presence {
|
||||
data.activities.push(
|
||||
...this.activities.map(a => ({
|
||||
name: a.name,
|
||||
type: ActivityTypes.indexOf(a.type),
|
||||
type: ActivityTypes[a.type],
|
||||
url: a.url ?? undefined,
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -20,7 +20,7 @@ const VoiceStateManager = require('../managers/VoiceStateManager');
|
||||
const Collection = require('../util/Collection');
|
||||
const {
|
||||
ChannelTypes,
|
||||
DefaultMessageNotifications,
|
||||
DefaultMessageNotificationLevels,
|
||||
PartialTypes,
|
||||
VerificationLevels,
|
||||
ExplicitContentFilterLevels,
|
||||
@@ -280,11 +280,10 @@ class Guild extends BaseGuild {
|
||||
this.joinedTimestamp = data.joined_at ? new Date(data.joined_at).getTime() : this.joinedTimestamp;
|
||||
|
||||
/**
|
||||
* The value set for the guild's default message notifications
|
||||
* @type {DefaultMessageNotifications|number}
|
||||
* The default message notification level of the guild
|
||||
* @type {DefaultMessageNotificationLevel}
|
||||
*/
|
||||
this.defaultMessageNotifications =
|
||||
DefaultMessageNotifications[data.default_message_notifications] || data.default_message_notifications;
|
||||
this.defaultMessageNotifications = DefaultMessageNotificationLevels[data.default_message_notifications];
|
||||
|
||||
/**
|
||||
* The value set for the guild's system channel flags
|
||||
@@ -831,7 +830,8 @@ class Guild extends BaseGuild {
|
||||
* @property {Base64Resolvable} [splash] The invite splash image of the guild
|
||||
* @property {Base64Resolvable} [discoverySplash] The discovery splash image of the guild
|
||||
* @property {Base64Resolvable} [banner] The banner of the guild
|
||||
* @property {DefaultMessageNotifications|number} [defaultMessageNotifications] The default message notifications
|
||||
* @property {DefaultMessageNotificationLevel|number} [defaultMessageNotifications] The default message notification
|
||||
* level of the guild
|
||||
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags of the guild
|
||||
* @property {ChannelResolvable} [rulesChannel] The rules channel of the guild
|
||||
* @property {ChannelResolvable} [publicUpdatesChannel] The community updates channel of the guild
|
||||
@@ -859,8 +859,8 @@ class Guild extends BaseGuild {
|
||||
if (typeof data.verificationLevel !== 'undefined') {
|
||||
_data.verification_level =
|
||||
typeof data.verificationLevel === 'number'
|
||||
? Number(data.verificationLevel)
|
||||
: VerificationLevels.indexOf(data.verificationLevel);
|
||||
? data.verificationLevel
|
||||
: VerificationLevels[data.verificationLevel];
|
||||
}
|
||||
if (typeof data.afkChannel !== 'undefined') {
|
||||
_data.afk_channel_id = this.client.channels.resolveID(data.afkChannel);
|
||||
@@ -878,13 +878,13 @@ class Guild extends BaseGuild {
|
||||
_data.explicit_content_filter =
|
||||
typeof data.explicitContentFilter === 'number'
|
||||
? data.explicitContentFilter
|
||||
: ExplicitContentFilterLevels.indexOf(data.explicitContentFilter);
|
||||
: ExplicitContentFilterLevels[data.explicitContentFilter];
|
||||
}
|
||||
if (typeof data.defaultMessageNotifications !== 'undefined') {
|
||||
_data.default_message_notifications =
|
||||
typeof data.defaultMessageNotifications === 'string'
|
||||
? DefaultMessageNotifications.indexOf(data.defaultMessageNotifications)
|
||||
: data.defaultMessageNotifications;
|
||||
typeof data.defaultMessageNotifications === 'number'
|
||||
? data.defaultMessageNotifications
|
||||
: DefaultMessageNotificationLevels[data.defaultMessageNotifications];
|
||||
}
|
||||
if (typeof data.systemChannelFlags !== 'undefined') {
|
||||
_data.system_channel_flags = SystemChannelFlags.resolve(data.systemChannelFlags);
|
||||
@@ -921,7 +921,7 @@ class Guild extends BaseGuild {
|
||||
/* eslint-disable max-len */
|
||||
/**
|
||||
* Edits the setting of the default message notifications of the guild.
|
||||
* @param {DefaultMessageNotifications|number} defaultMessageNotifications The new setting for the default message notifications
|
||||
* @param {DefaultMessageNotificationLevel|number} defaultMessageNotifications The new default message notification level of the guild
|
||||
* @param {string} [reason] Reason for changing the setting of the default message notifications
|
||||
* @returns {Promise<Guild>}
|
||||
*/
|
||||
|
||||
@@ -169,7 +169,7 @@ class Activity {
|
||||
* The type of the activity status
|
||||
* @type {ActivityType}
|
||||
*/
|
||||
this.type = ActivityTypes[data.type] || ActivityTypes[ActivityTypes.indexOf(data.type)];
|
||||
this.type = typeof data.type === 'number' ? ActivityTypes[data.type] : data.type;
|
||||
|
||||
/**
|
||||
* If the activity is being streamed, a link to the stream
|
||||
|
||||
@@ -31,7 +31,7 @@ class Sticker extends Base {
|
||||
|
||||
/**
|
||||
* The format of the sticker
|
||||
* @type {StickerFormatTypes}
|
||||
* @type {StickerFormatType}
|
||||
*/
|
||||
this.format = StickerFormatTypes[sticker.format_type];
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ class TeamMember extends Base {
|
||||
|
||||
/**
|
||||
* The permissions this Team Member has with regard to the team
|
||||
* @type {MembershipStates}
|
||||
* @type {MembershipState}
|
||||
*/
|
||||
this.membershipState = MembershipStates[data.membership_state];
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class Webhook {
|
||||
|
||||
/**
|
||||
* The type of the webhook
|
||||
* @type {WebhookTypes}
|
||||
* @type {WebhookType}
|
||||
*/
|
||||
this.type = WebhookTypes[data.type];
|
||||
|
||||
|
||||
@@ -488,17 +488,17 @@ exports.SystemMessageTypes = exports.MessageTypes.filter(
|
||||
);
|
||||
|
||||
/**
|
||||
* <info>Bots cannot set a `CUSTOM_STATUS`, it is only for custom statuses received from users</info>
|
||||
* <info>Bots cannot set a `CUSTOM` activity type, it is only for custom statuses received from users</info>
|
||||
* The type of an activity of a users presence, e.g. `PLAYING`. Here are the available types:
|
||||
* * PLAYING
|
||||
* * STREAMING
|
||||
* * LISTENING
|
||||
* * WATCHING
|
||||
* * CUSTOM_STATUS
|
||||
* * CUSTOM
|
||||
* * COMPETING
|
||||
* @typedef {string} ActivityType
|
||||
*/
|
||||
exports.ActivityTypes = ['PLAYING', 'STREAMING', 'LISTENING', 'WATCHING', 'CUSTOM_STATUS', 'COMPETING'];
|
||||
exports.ActivityTypes = createEnum(['PLAYING', 'STREAMING', 'LISTENING', 'WATCHING', 'CUSTOM', 'COMPETING']);
|
||||
|
||||
exports.ChannelTypes = createEnum([
|
||||
'TEXT',
|
||||
@@ -559,7 +559,7 @@ exports.Colors = {
|
||||
* * ALL_MEMBERS
|
||||
* @typedef {string} ExplicitContentFilterLevel
|
||||
*/
|
||||
exports.ExplicitContentFilterLevels = ['DISABLED', 'MEMBERS_WITHOUT_ROLES', 'ALL_MEMBERS'];
|
||||
exports.ExplicitContentFilterLevels = createEnum(['DISABLED', 'MEMBERS_WITHOUT_ROLES', 'ALL_MEMBERS']);
|
||||
|
||||
/**
|
||||
* The value set for the verification levels for a guild:
|
||||
@@ -570,7 +570,7 @@ exports.ExplicitContentFilterLevels = ['DISABLED', 'MEMBERS_WITHOUT_ROLES', 'ALL
|
||||
* * VERY_HIGH
|
||||
* @typedef {string} VerificationLevel
|
||||
*/
|
||||
exports.VerificationLevels = ['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_HIGH'];
|
||||
exports.VerificationLevels = createEnum(['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_HIGH']);
|
||||
|
||||
/**
|
||||
* An error encountered while performing an API request. Here are the potential errors:
|
||||
@@ -717,45 +717,35 @@ exports.APIErrors = {
|
||||
};
|
||||
|
||||
/**
|
||||
* The value set for a guild's default message notifications, e.g. `ALL`. Here are the available types:
|
||||
* * ALL
|
||||
* * MENTIONS
|
||||
* @typedef {string} DefaultMessageNotifications
|
||||
* The value set for a guild's default message notifications, e.g. `ALL_MESSAGES`. Here are the available types:
|
||||
* * ALL_MESSAGES
|
||||
* * ONLY_MENTIONS
|
||||
* @typedef {string} DefaultMessageNotificationLevel
|
||||
*/
|
||||
exports.DefaultMessageNotifications = ['ALL', 'MENTIONS'];
|
||||
exports.DefaultMessageNotificationLevels = createEnum(['ALL_MESSAGES', 'ONLY_MENTIONS']);
|
||||
|
||||
/**
|
||||
* The value set for a team members's membership state:
|
||||
* * INVITED
|
||||
* * ACCEPTED
|
||||
* @typedef {string} MembershipStates
|
||||
* @typedef {string} MembershipState
|
||||
*/
|
||||
exports.MembershipStates = [
|
||||
// They start at 1
|
||||
null,
|
||||
'INVITED',
|
||||
'ACCEPTED',
|
||||
];
|
||||
exports.MembershipStates = createEnum([null, 'INVITED', 'ACCEPTED']);
|
||||
|
||||
/**
|
||||
* The value set for a webhook's type:
|
||||
* * Incoming
|
||||
* * Channel Follower
|
||||
* @typedef {string} WebhookTypes
|
||||
* @typedef {string} WebhookType
|
||||
*/
|
||||
exports.WebhookTypes = [
|
||||
// They start at 1
|
||||
null,
|
||||
'Incoming',
|
||||
'Channel Follower',
|
||||
];
|
||||
exports.WebhookTypes = createEnum([null, 'Incoming', 'Channel Follower']);
|
||||
|
||||
/**
|
||||
* The value set for a sticker's type:
|
||||
* * PNG
|
||||
* * APNG
|
||||
* * LOTTIE
|
||||
* @typedef {string} StickerFormatTypes
|
||||
* @typedef {string} StickerFormatType
|
||||
*/
|
||||
exports.StickerFormatTypes = createEnum([null, 'PNG', 'APNG', 'LOTTIE']);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user