Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove discord.js enums and use discord-api-types enums instead #7077

Merged
merged 15 commits into from
Jan 12, 2022
Merged
4 changes: 2 additions & 2 deletions packages/discord.js/src/client/actions/ChannelUpdate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const { ChannelType } = require('discord-api-types/v9');
const Action = require('./Action');
const { Channel } = require('../../structures/Channel');
const { ChannelTypes } = require('../../util/Constants');

class ChannelUpdateAction extends Action {
handle(data) {
Expand All @@ -12,7 +12,7 @@ class ChannelUpdateAction extends Action {
if (channel) {
const old = channel._update(data);

if (ChannelTypes[channel.type] !== data.type) {
if (ChannelType[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);
channel = newChannel;
Expand Down
35 changes: 18 additions & 17 deletions packages/discord.js/src/client/actions/InteractionCreate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

const { InteractionType, ComponentType, ApplicationCommandType } = require('discord-api-types/v9');
const Action = require('./Action');
const AutocompleteInteraction = require('../../structures/AutocompleteInteraction');
const ButtonInteraction = require('../../structures/ButtonInteraction');
const ChatInputCommandInteraction = require('../../structures/ChatInputCommandInteraction');
const MessageContextMenuCommandInteraction = require('../../structures/MessageContextMenuCommandInteraction');
const SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
const UserContextMenuCommandInteraction = require('../../structures/UserContextMenuCommandInteraction');
const { Events, InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../../util/Constants');
const { Events } = require('../../util/Constants');

class InteractionCreateAction extends Action {
handle(data) {
Expand All @@ -16,18 +17,18 @@ class InteractionCreateAction extends Action {
// Resolve and cache partial channels for Interaction#channel getter
this.getChannel(data);

let InteractionType;
let InteractionClass;
switch (data.type) {
case InteractionTypes.APPLICATION_COMMAND:
case InteractionType.ApplicationCommand:
switch (data.data.type) {
case ApplicationCommandTypes.CHAT_INPUT:
InteractionType = ChatInputCommandInteraction;
case ApplicationCommandType.ChatInput:
InteractionClass = ChatInputCommandInteraction;
break;
case ApplicationCommandTypes.USER:
InteractionType = UserContextMenuCommandInteraction;
case ApplicationCommandType.User:
InteractionClass = UserContextMenuCommandInteraction;
break;
case ApplicationCommandTypes.MESSAGE:
InteractionType = MessageContextMenuCommandInteraction;
case ApplicationCommandType.Message:
InteractionClass = MessageContextMenuCommandInteraction;
break;
default:
client.emit(
Expand All @@ -37,13 +38,13 @@ class InteractionCreateAction extends Action {
return;
}
break;
case InteractionTypes.MESSAGE_COMPONENT:
case InteractionType.MessageComponent:
switch (data.data.component_type) {
case MessageComponentTypes.BUTTON:
InteractionType = ButtonInteraction;
case ComponentType.Button:
InteractionClass = ButtonInteraction;
break;
case MessageComponentTypes.SELECT_MENU:
InteractionType = SelectMenuInteraction;
case ComponentType.SelectMenu:
InteractionClass = SelectMenuInteraction;
break;
default:
client.emit(
Expand All @@ -53,15 +54,15 @@ class InteractionCreateAction extends Action {
return;
}
break;
case InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE:
InteractionType = AutocompleteInteraction;
case InteractionType.ApplicationCommandAutocomplete:
InteractionClass = AutocompleteInteraction;
break;
default:
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`);
return;
}

const interaction = new InteractionType(client, data);
const interaction = new InteractionClass(client, data);

/**
* Emitted when an interaction is created.
Expand Down
25 changes: 25 additions & 0 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,28 @@ exports.WelcomeChannel = require('./structures/WelcomeChannel');
exports.WelcomeScreen = require('./structures/WelcomeScreen');

exports.WebSocket = require('./WebSocket');

// External
exports.ActivityType = require('discord-api-types/v9').ActivityType;
exports.ApplicationCommandType = require('discord-api-types/v9').ApplicationCommandOptionType;
exports.ApplicationCommandOptionType = require('discord-api-types/v9').ApplicationCommandOptionType;
exports.ApplicationCommandPermissionType = require('discord-api-types/v9').ApplicationCommandPermissionType;
exports.ButtonStyle = require('discord-api-types/v9').ButtonStyle;
exports.ChannelType = require('discord-api-types/v9').ChannelType;
exports.ComponentType = require('discord-api-types/v9').ComponentType;
exports.GuildMFALevel = require('discord-api-types/v9').GuildMFALevel;
exports.GuildNSFWLevel = require('discord-api-types/v9').GuildNSFWLevel;
exports.GuildPremiumTier = require('discord-api-types/v9').GuildPremiumTier;
exports.GuildScheduledEventEntityType = require('discord-api-types/v9').GuildScheduledEventEntityType;
exports.GuildScheduledEventPrivacyLevel = require('discord-api-types/v9').GuildScheduledEventPrivacyLevel;
exports.GuildScheduledEventStatus = require('discord-api-types/v9').GuildScheduledEventStatus;
exports.GuildVerificationLevel = require('discord-api-types/v9').GuildVerificationLevel;
exports.InteractionType = require('discord-api-types/v9').InteractionType;
exports.InteractionResponseType = require('discord-api-types/v9').InteractionResponseType;
exports.InviteTargetType = require('discord-api-types/v9').InviteTargetType;
exports.MessageType = require('discord-api-types/v9').MessageType;
exports.RESTJSONErrorCodes = require('discord-api-types/v9').RESTJSONErrorCodes;
exports.StageInstancePrivacyLevel = require('discord-api-types/v9').StageInstancePrivacyLevel;
exports.StickerType = require('discord-api-types/v9').StickerType;
exports.StickerFormatType = require('discord-api-types/v9').StickerFormatType;
exports.WebhookType = require('discord-api-types/v9').WebhookType;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const { Collection } = require('@discordjs/collection');
const { ApplicationCommandType } = require('discord-api-types/v9');
const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermissionsManager');
const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const ApplicationCommand = require('../structures/ApplicationCommand');
const { ApplicationCommandTypes } = require('../util/Constants');

/**
* Manages API methods for application commands and stores their cache.
Expand Down Expand Up @@ -207,7 +207,7 @@ class ApplicationCommandManager extends CachedManager {
return {
name: command.name,
description: command.description,
type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type],
type: typeof command.type === 'number' ? command.type : ApplicationCommandType[command.type],
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
default_permission: command.defaultPermission ?? command.default_permission,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { Collection } = require('@discordjs/collection');
const { ApplicationCommandPermissionType, RESTJSONErrorCodes } = require('discord-api-types/v9');
const BaseManager = require('./BaseManager');
const { Error, TypeError } = require('../errors');
const { ApplicationCommandPermissionTypes, APIErrors } = require('../util/Constants');

/**
* Manages API methods for permissions of Application Commands.
Expand Down Expand Up @@ -230,7 +230,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
try {
existing = await this.fetch({ guild: guildId, command: commandId });
} catch (error) {
if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error;
if (error.code !== RESTJSONErrorCodes.UnknownApplicationCommandPermissions) throw error;
}

const newPermissions = permissions.slice();
Expand Down Expand Up @@ -319,7 +319,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
try {
existing = await this.fetch({ guild: guildId, command: commandId });
} catch (error) {
if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error;
if (error.code !== RESTJSONErrorCodes.UnknownApplicationCommandPermissions) throw error;
}

const permissions = existing.filter(perm => !resolvedIds.includes(perm.id));
Expand Down Expand Up @@ -366,7 +366,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
try {
existing = await this.fetch({ guild: guildId, command: commandId });
} catch (error) {
if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error;
if (error.code !== RESTJSONErrorCodes.UnknownApplicationCommandPermissions) throw error;
}

return existing.some(perm => perm.id === resolvedId);
Expand Down Expand Up @@ -403,7 +403,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
type:
typeof permissions.type === 'number' && !received
? permissions.type
: ApplicationCommandPermissionTypes[permissions.type],
: ApplicationCommandPermissionType[permissions.type],
};
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/discord.js/src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const { ChannelType } = require('discord-api-types/v9');
const CachedManager = require('./CachedManager');
const ThreadManager = require('./ThreadManager');
const { Error } = require('../errors');
const GuildChannel = require('../structures/GuildChannel');
const PermissionOverwrites = require('../structures/PermissionOverwrites');
const ThreadChannel = require('../structures/ThreadChannel');
const { ChannelTypes, ThreadChannelTypes } = require('../util/Constants');
const { ThreadChannelTypes } = require('../util/Constants');

let cacheWarningEmitted = false;
let storeChannelDeprecationEmitted = false;
Expand Down Expand Up @@ -139,9 +140,9 @@ class GuildChannelManager extends CachedManager {
) {
parent &&= this.client.channels.resolveId(parent);
permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
const intType = typeof type === 'number' ? type : ChannelTypes[type] ?? ChannelTypes.GUILD_TEXT;
const intType = typeof type === 'number' ? type : ChannelType[type] ?? ChannelType.GuildText;

if (intType === ChannelTypes.GUILD_STORE && !storeChannelDeprecationEmitted) {
if (intType === ChannelType.GuildStore && !storeChannelDeprecationEmitted) {
storeChannelDeprecationEmitted = true;
process.emitWarning(
// eslint-disable-next-line max-len
Expand Down
26 changes: 13 additions & 13 deletions packages/discord.js/src/managers/GuildManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
const process = require('node:process');
const { setTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection');
const {
GuildVerificationLevel,
GuildDefaultMessageNotifications,
GuildExplicitContentFilter,
ChannelType,
OverwriteType,
} = require('discord-api-types/v9');
const CachedManager = require('./CachedManager');
const { Guild } = require('../structures/Guild');
const GuildChannel = require('../structures/GuildChannel');
Expand All @@ -11,14 +18,7 @@ const { GuildMember } = require('../structures/GuildMember');
const Invite = require('../structures/Invite');
const OAuth2Guild = require('../structures/OAuth2Guild');
const { Role } = require('../structures/Role');
const {
ChannelTypes,
Events,
OverwriteTypes,
VerificationLevels,
DefaultMessageNotificationLevels,
ExplicitContentFilterLevels,
} = require('../util/Constants');
const { Events } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Permissions = require('../util/Permissions');
const SystemChannelFlags = require('../util/SystemChannelFlags');
Expand Down Expand Up @@ -182,16 +182,16 @@ class GuildManager extends CachedManager {
) {
icon = await DataResolver.resolveImage(icon);
if (typeof verificationLevel === 'string') {
verificationLevel = VerificationLevels[verificationLevel];
verificationLevel = GuildVerificationLevel[verificationLevel];
}
if (typeof defaultMessageNotifications === 'string') {
defaultMessageNotifications = DefaultMessageNotificationLevels[defaultMessageNotifications];
defaultMessageNotifications = GuildDefaultMessageNotifications[defaultMessageNotifications];
}
if (typeof explicitContentFilter === 'string') {
explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter];
explicitContentFilter = GuildExplicitContentFilter[explicitContentFilter];
}
for (const channel of channels) {
channel.type &&= typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
channel.type &&= typeof channel.type === 'number' ? channel.type : ChannelType[channel.type];
channel.parent_id = channel.parentId;
delete channel.parentId;
channel.user_limit = channel.userLimit;
Expand All @@ -204,7 +204,7 @@ class GuildManager extends CachedManager {
if (!channel.permissionOverwrites) continue;
for (const overwrite of channel.permissionOverwrites) {
if (typeof overwrite.type === 'string') {
overwrite.type = OverwriteTypes[overwrite.type];
overwrite.type = OverwriteType[overwrite.type];
}
overwrite.allow &&= Permissions.resolve(overwrite.allow).toString();
overwrite.deny &&= Permissions.resolve(overwrite.deny).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const { OverwriteType } = require('discord-api-types/v9');
const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const PermissionOverwrites = require('../structures/PermissionOverwrites');
const { Role } = require('../structures/Role');
const { OverwriteTypes } = require('../util/Constants');

let cacheWarningEmitted = false;

Expand Down Expand Up @@ -94,7 +94,7 @@ class PermissionOverwriteManager extends CachedManager {
if (typeof type !== 'number') {
userOrRole = this.channel.guild.roles.resolve(userOrRole) ?? this.client.users.resolve(userOrRole);
if (!userOrRole) throw new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role');
type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member;
type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
}

const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options, existing);
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/managers/StageInstanceManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { GuildScheduledEventPrivacyLevel } = require('discord-api-types/v9');
const CachedManager = require('./CachedManager');
const { TypeError, Error } = require('../errors');
const { StageInstance } = require('../structures/StageInstance');
const { PrivacyLevels } = require('../util/Constants');

/**
* Manages API methods for {@link StageInstance} objects and holds their cache.
Expand Down Expand Up @@ -60,7 +60,7 @@ class StageInstanceManager extends CachedManager {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let { topic, privacyLevel } = options;

privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : GuildScheduledEventPrivacyLevel[privacyLevel];

const data = await this.client.api['stage-instances'].post({
data: {
Expand Down Expand Up @@ -122,7 +122,7 @@ class StageInstanceManager extends CachedManager {

let { topic, privacyLevel } = options;

privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : GuildScheduledEventPrivacyLevel[privacyLevel];

const data = await this.client.api('stage-instances', channelId).patch({
data: {
Expand Down
9 changes: 4 additions & 5 deletions packages/discord.js/src/managers/ThreadManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { Collection } = require('@discordjs/collection');
const { ChannelType } = require('discord-api-types/v9');
const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const ThreadChannel = require('../structures/ThreadChannel');
const { ChannelTypes } = require('../util/Constants');

/**
* Manages API methods for {@link ThreadChannel} objects and stores their cache.
Expand Down Expand Up @@ -111,14 +111,13 @@ 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 === 'GUILD_NEWS' ? ChannelTypes.GUILD_NEWS_THREAD : ChannelTypes.GUILD_PUBLIC_THREAD;
let resolvedType = this.channel.type === 'GUILD_NEWS' ? ChannelType.GuildNewsThread : ChannelType.GuildPublicThread;
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 !== 'GUILD_NEWS') {
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType;
resolvedType = typeof type === 'string' ? ChannelType[type] : type ?? resolvedType;
}
if (autoArchiveDuration === 'MAX') {
autoArchiveDuration = 1440;
Expand All @@ -134,7 +133,7 @@ class ThreadManager extends CachedManager {
name,
auto_archive_duration: autoArchiveDuration,
type: resolvedType,
invitable: resolvedType === ChannelTypes.GUILD_PRIVATE_THREAD ? invitable : undefined,
invitable: resolvedType === ChannelType.GuildPrivateThread ? invitable : undefined,
rate_limit_per_user: rateLimitPerUser,
},
reason,
Expand Down
Loading