mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
refactor: make use of destructuring for Constants (#1942)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Base = require('./Base');
|
||||
const Constants = require('../util/Constants');
|
||||
const { ChannelTypes } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Represents any channel on Discord.
|
||||
@@ -10,7 +10,7 @@ class Channel extends Base {
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
|
||||
const type = Object.keys(Constants.ChannelTypes)[data.type];
|
||||
const type = Object.keys(ChannelTypes)[data.type];
|
||||
/**
|
||||
* The type of the channel, either:
|
||||
* * `dm` - a DM channel
|
||||
@@ -72,23 +72,22 @@ class Channel extends Base {
|
||||
const VoiceChannel = require('./VoiceChannel');
|
||||
const CategoryChannel = require('./CategoryChannel');
|
||||
const GuildChannel = require('./GuildChannel');
|
||||
const types = Constants.ChannelTypes;
|
||||
let channel;
|
||||
if (data.type === types.DM) {
|
||||
if (data.type === ChannelTypes.DM) {
|
||||
channel = new DMChannel(client, data);
|
||||
} else if (data.type === types.GROUP) {
|
||||
} else if (data.type === ChannelTypes.GROUP) {
|
||||
channel = new GroupDMChannel(client, data);
|
||||
} else {
|
||||
guild = guild || client.guilds.get(data.guild_id);
|
||||
if (guild) {
|
||||
switch (data.type) {
|
||||
case types.TEXT:
|
||||
case ChannelTypes.TEXT:
|
||||
channel = new TextChannel(guild, data);
|
||||
break;
|
||||
case types.VOICE:
|
||||
case ChannelTypes.VOICE:
|
||||
channel = new VoiceChannel(guild, data);
|
||||
break;
|
||||
case types.CATEGORY:
|
||||
case ChannelTypes.CATEGORY:
|
||||
channel = new CategoryChannel(guild, data);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Constants = require('../util/Constants');
|
||||
const { ClientApplicationAssetTypes, Endpoints } = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const Base = require('./Base');
|
||||
|
||||
@@ -140,7 +140,7 @@ class ClientApplication extends Base {
|
||||
*/
|
||||
coverImage({ format, size } = {}) {
|
||||
if (!this.cover) return null;
|
||||
return Constants.Endpoints
|
||||
return Endpoints
|
||||
.CDN(this.client.options.http.cdn)
|
||||
.AppIcon(this.id, this.cover, { format, size });
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class ClientApplication extends Base {
|
||||
.then(assets => assets.map(a => ({
|
||||
id: a.id,
|
||||
name: a.name,
|
||||
type: Object.keys(Constants.ClientApplicationAssetTypes)[a.type - 1],
|
||||
type: Object.keys(ClientApplicationAssetTypes)[a.type - 1],
|
||||
})));
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ class ClientApplication extends Base {
|
||||
this.client.api.applications(this.id).assets.post({ data: {
|
||||
name,
|
||||
data: b64,
|
||||
type: Constants.ClientApplicationAssetTypes[type.toUpperCase()],
|
||||
type: ClientApplicationAssetTypes[type.toUpperCase()],
|
||||
} }));
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ const User = require('./User');
|
||||
const Collection = require('../util/Collection');
|
||||
const ClientUserSettings = require('./ClientUserSettings');
|
||||
const ClientUserGuildSettings = require('./ClientUserGuildSettings');
|
||||
const Constants = require('../util/Constants');
|
||||
const { Events } = require('../util/Constants');
|
||||
const Util = require('../util/Util');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const Guild = require('./Guild');
|
||||
@@ -278,15 +278,15 @@ class ClientUser extends User {
|
||||
|
||||
const handleGuild = guild => {
|
||||
if (guild.id === data.id) {
|
||||
this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild);
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.clearTimeout(timeout);
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.on(Constants.Events.GUILD_CREATE, handleGuild);
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = this.client.setTimeout(() => {
|
||||
this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild);
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
resolve(this.client.guilds.create(data));
|
||||
}, 10000);
|
||||
return undefined;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const { UserChannelOverrideMap } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* A wrapper around the ClientUser's channel overrides.
|
||||
@@ -14,7 +14,7 @@ class ClientUserChannelOverride {
|
||||
* @private
|
||||
*/
|
||||
patch(data) {
|
||||
for (const [key, value] of Object.entries(Constants.UserChannelOverrideMap)) {
|
||||
for (const [key, value] of Object.entries(UserChannelOverrideMap)) {
|
||||
if (!data.hasOwnProperty(key)) continue;
|
||||
if (typeof value === 'function') {
|
||||
this[value.name] = value(data[key]);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const { UserGuildSettingsMap } = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const ClientUserChannelOverride = require('./ClientUserChannelOverride');
|
||||
|
||||
@@ -29,7 +29,7 @@ class ClientUserGuildSettings {
|
||||
* @private
|
||||
*/
|
||||
patch(data) {
|
||||
for (const [key, value] of Object.entries(Constants.UserGuildSettingsMap)) {
|
||||
for (const [key, value] of Object.entries(UserGuildSettingsMap)) {
|
||||
if (!data.hasOwnProperty(key)) continue;
|
||||
if (key === 'channel_overrides') {
|
||||
for (const channel of data[key]) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const { UserSettingsMap } = require('../util/Constants');
|
||||
const Util = require('../util/Util');
|
||||
const { Error } = require('../errors');
|
||||
|
||||
@@ -17,7 +17,7 @@ class ClientUserSettings {
|
||||
* @private
|
||||
*/
|
||||
patch(data) {
|
||||
for (const [key, value] of Object.entries(Constants.UserSettingsMap)) {
|
||||
for (const [key, value] of Object.entries(UserSettingsMap)) {
|
||||
if (!data.hasOwnProperty(key)) continue;
|
||||
if (typeof value === 'function') {
|
||||
this[value.name] = value(data[key]);
|
||||
|
||||
@@ -3,7 +3,7 @@ const GuildAuditLogs = require('./GuildAuditLogs');
|
||||
const Webhook = require('./Webhook');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const VoiceRegion = require('./VoiceRegion');
|
||||
const Constants = require('../util/Constants');
|
||||
const { ChannelTypes, Events } = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const Util = require('../util/Util');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
@@ -923,7 +923,7 @@ class Guild extends Base {
|
||||
return this.client.api.guilds(this.id).channels.post({
|
||||
data: {
|
||||
name,
|
||||
type: Constants.ChannelTypes[type.toUpperCase()],
|
||||
type: ChannelTypes[type.toUpperCase()],
|
||||
permission_overwrites: overwrites,
|
||||
},
|
||||
reason,
|
||||
@@ -1128,7 +1128,7 @@ class Guild extends Base {
|
||||
* @param {GuildMember} member The member that started/stopped speaking
|
||||
* @param {boolean} speaking Whether or not the member is speaking
|
||||
*/
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_SPEAKING, member, speaking);
|
||||
this.client.emit(Events.GUILD_MEMBER_SPEAKING, member, speaking);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1137,7 +1137,7 @@ class Guild extends Base {
|
||||
}
|
||||
|
||||
_sortedChannels(channel) {
|
||||
const category = channel.type === Constants.ChannelTypes.CATEGORY;
|
||||
const category = channel.type === ChannelTypes.CATEGORY;
|
||||
return Util.discordSort(this.channels.filter(c =>
|
||||
c.type === channel.type && (category || c.parent === channel.parent)));
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const Util = require('../util/Util');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Collection = require('../util/Collection');
|
||||
const Constants = require('../util/Constants');
|
||||
const { MessageNotificationTypes } = require('../util/Constants');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
@@ -474,7 +474,7 @@ class GuildChannel extends Channel {
|
||||
try {
|
||||
return this.client.user.guildSettings.get(this.guild.id).channelOverrides.get(this.id).messageNotifications;
|
||||
} catch (err) {
|
||||
return Constants.MessageNotificationTypes[3];
|
||||
return MessageNotificationTypes[3];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const { Endpoints } = require('../util/Constants');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
@@ -127,7 +127,7 @@ class Invite extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get url() {
|
||||
return Constants.Endpoints.invite(this.client.options.http.invite, this.code);
|
||||
return Endpoints.invite(this.client.options.http.invite, this.code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ const ClientApplication = require('./ClientApplication');
|
||||
const Util = require('../util/Util');
|
||||
const Collection = require('../util/Collection');
|
||||
const ReactionStore = require('../stores/ReactionStore');
|
||||
const Constants = require('../util/Constants');
|
||||
const { MessageTypes } = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const Base = require('./Base');
|
||||
@@ -40,7 +40,7 @@ class Message extends Base {
|
||||
* The type of the message
|
||||
* @type {MessageType}
|
||||
*/
|
||||
this.type = Constants.MessageTypes[data.type];
|
||||
this.type = MessageTypes[data.type];
|
||||
|
||||
/**
|
||||
* The content of the message
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Collector = require('./interfaces/Collector');
|
||||
const { Events } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* @typedef {CollectorOptions} MessageCollectorOptions
|
||||
@@ -36,14 +37,14 @@ class MessageCollector extends Collector {
|
||||
for (const message of messages.values()) this.handleDispose(message);
|
||||
}).bind(this);
|
||||
|
||||
this.client.on('message', this.handleCollect);
|
||||
this.client.on('messageDelete', this.handleDispose);
|
||||
this.client.on('messageDeleteBulk', bulkDeleteListener);
|
||||
this.client.on(Events.MESSAGE_CREATE, this.handleCollect);
|
||||
this.client.on(Events.MESSAGE_DELETE, this.handleDispose);
|
||||
this.client.on(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
|
||||
|
||||
this.once('end', () => {
|
||||
this.client.removeListener('message', this.handleCollect);
|
||||
this.client.removeListener('messageDelete', this.handleDispose);
|
||||
this.client.removeListener('messageDeleteBulk', bulkDeleteListener);
|
||||
this.client.removeListener(Events.MESSAGE_CREATE, this.handleCollect);
|
||||
this.client.removeListener(Events.MESSAGE_DELETE, this.handleDispose);
|
||||
this.client.removeListener(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const { ActivityTypes } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Represents a user's presence.
|
||||
@@ -67,7 +67,7 @@ class Activity {
|
||||
* The type of the activity status
|
||||
* @type {ActivityType}
|
||||
*/
|
||||
this.type = Constants.ActivityTypes[data.type];
|
||||
this.type = ActivityTypes[data.type];
|
||||
|
||||
/**
|
||||
* If the activity is being streamed, a link to the stream
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const Collector = require('./interfaces/Collector');
|
||||
const Collection = require('../util/Collection');
|
||||
const { Events } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* @typedef {CollectorOptions} ReactionCollectorOptions
|
||||
@@ -41,14 +42,14 @@ class ReactionCollector extends Collector {
|
||||
|
||||
this.empty = this.empty.bind(this);
|
||||
|
||||
this.client.on('messageReactionAdd', this.handleCollect);
|
||||
this.client.on('messageReactionRemove', this.handleDispose);
|
||||
this.client.on('messageReactionRemoveAll', this.empty);
|
||||
this.client.on(Events.MESSAGE_REACTION_ADD, this.handleCollect);
|
||||
this.client.on(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
|
||||
this.client.on(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
|
||||
|
||||
this.once('end', () => {
|
||||
this.client.removeListener('messageReactionAdd', this.handleCollect);
|
||||
this.client.removeListener('messageReactionRemove', this.handleDispose);
|
||||
this.client.removeListener('messageReactionRemoveAll', this.empty);
|
||||
this.client.removeListener(Events.MESSAGE_REACTION_ADD, this.handleCollect);
|
||||
this.client.removeListener(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
|
||||
this.client.removeListener(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
|
||||
});
|
||||
|
||||
this.on('collect', (collected, reaction, user) => {
|
||||
|
||||
Reference in New Issue
Block a user