mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
perf: use logical assignments instead of if statements (#6693)
This commit is contained in:
@@ -7,7 +7,7 @@ module.exports = (client, { d: data }, shard) => {
|
||||
if (client.user) {
|
||||
client.user._patch(data.user);
|
||||
} else {
|
||||
if (!ClientUser) ClientUser = require('../../../structures/ClientUser');
|
||||
ClientUser ??= require('../../../structures/ClientUser');
|
||||
client.user = new ClientUser(client, data.user);
|
||||
client.users.cache.set(client.user.id, client.user);
|
||||
}
|
||||
|
||||
@@ -93,9 +93,7 @@ class GuildBanManager extends CachedManager {
|
||||
if (!options) return this._fetchMany();
|
||||
const user = this.client.users.resolveId(options);
|
||||
if (user) return this._fetchSingle({ user, cache: true });
|
||||
if (options.user) {
|
||||
options.user = this.client.users.resolveId(options.user);
|
||||
}
|
||||
options.user &&= this.client.users.resolveId(options.user);
|
||||
if (!options.user) {
|
||||
if ('cache' in options) return this._fetchMany(options.cache);
|
||||
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));
|
||||
|
||||
@@ -133,10 +133,8 @@ class GuildChannelManager extends CachedManager {
|
||||
name,
|
||||
{ type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, position, rateLimitPerUser, reason } = {},
|
||||
) {
|
||||
if (parent) parent = this.client.channels.resolveId(parent);
|
||||
if (permissionOverwrites) {
|
||||
permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
|
||||
}
|
||||
parent &&= this.client.channels.resolveId(parent);
|
||||
permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
|
||||
|
||||
const data = await this.client.api.guilds(this.guild.id).channels.post({
|
||||
data: {
|
||||
|
||||
@@ -188,8 +188,8 @@ class GuildManager extends CachedManager {
|
||||
explicitContentFilter = ExplicitContentFilterLevels[explicitContentFilter];
|
||||
}
|
||||
for (const channel of channels) {
|
||||
if (channel.type) channel.type = ChannelTypes[channel.type.toUpperCase()];
|
||||
if (channel.type) channel.type = typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
|
||||
channel.type &&= ChannelTypes[channel.type.toUpperCase()];
|
||||
channel.type &&= typeof channel.type === 'number' ? channel.type : ChannelTypes[channel.type];
|
||||
channel.parent_id = channel.parentId;
|
||||
delete channel.parentId;
|
||||
channel.user_limit = channel.userLimit;
|
||||
@@ -201,17 +201,17 @@ class GuildManager extends CachedManager {
|
||||
if (typeof overwrite.type === 'string') {
|
||||
overwrite.type = OverwriteTypes[overwrite.type];
|
||||
}
|
||||
if (overwrite.allow) overwrite.allow = Permissions.resolve(overwrite.allow).toString();
|
||||
if (overwrite.deny) overwrite.deny = Permissions.resolve(overwrite.deny).toString();
|
||||
overwrite.allow &&= Permissions.resolve(overwrite.allow).toString();
|
||||
overwrite.deny &&= Permissions.resolve(overwrite.deny).toString();
|
||||
}
|
||||
channel.permission_overwrites = channel.permissionOverwrites;
|
||||
delete channel.permissionOverwrites;
|
||||
}
|
||||
for (const role of roles) {
|
||||
if (role.color) role.color = resolveColor(role.color);
|
||||
if (role.permissions) role.permissions = Permissions.resolve(role.permissions).toString();
|
||||
role.color &&= resolveColor(role.color);
|
||||
role.permissions &&= Permissions.resolve(role.permissions).toString();
|
||||
}
|
||||
if (systemChannelFlags) systemChannelFlags = SystemChannelFlags.resolve(systemChannelFlags);
|
||||
systemChannelFlags &&= SystemChannelFlags.resolve(systemChannelFlags);
|
||||
|
||||
const data = await this.client.api.guilds.post({
|
||||
data: {
|
||||
|
||||
@@ -248,7 +248,7 @@ class GuildMemberManager extends CachedManager {
|
||||
_data.channel_id = null;
|
||||
_data.channel = undefined;
|
||||
}
|
||||
if (_data.roles) _data.roles = _data.roles.map(role => (role instanceof Role ? role.id : role));
|
||||
_data.roles &&= _data.roles.map(role => (role instanceof Role ? role.id : role));
|
||||
let endpoint = this.client.api.guilds(this.guild.id);
|
||||
if (id === this.client.user.id) {
|
||||
const keys = Object.keys(_data);
|
||||
|
||||
@@ -129,7 +129,7 @@ class RoleManager extends CachedManager {
|
||||
*/
|
||||
async create(options = {}) {
|
||||
let { name, color, hoist, permissions, position, mentionable, reason } = options;
|
||||
if (color) color = resolveColor(color);
|
||||
color &&= resolveColor(color);
|
||||
if (typeof permissions !== 'undefined') permissions = new Permissions(permissions);
|
||||
|
||||
const data = await this.client.api.guilds(this.guild.id).roles.post({
|
||||
|
||||
@@ -60,7 +60,7 @@ class StageInstanceManager extends CachedManager {
|
||||
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
|
||||
let { topic, privacyLevel } = options;
|
||||
|
||||
if (privacyLevel) privacyLevel = typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
|
||||
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
|
||||
|
||||
const data = await this.client.api['stage-instances'].post({
|
||||
data: {
|
||||
@@ -122,7 +122,7 @@ class StageInstanceManager extends CachedManager {
|
||||
|
||||
let { topic, privacyLevel } = options;
|
||||
|
||||
if (privacyLevel) privacyLevel = typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
|
||||
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
|
||||
|
||||
const data = await this.client.api('stage-instances', channelId).patch({
|
||||
data: {
|
||||
|
||||
@@ -294,7 +294,7 @@ class GuildChannel extends Channel {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async edit(data, reason) {
|
||||
if (data.parent) data.parent = this.client.channels.resolveId(data.parent);
|
||||
data.parent &&= this.client.channels.resolveId(data.parent);
|
||||
|
||||
if (typeof data.position !== 'undefined') {
|
||||
const updatedChannels = await Util.setPosition(
|
||||
|
||||
@@ -245,7 +245,7 @@ class Webhook {
|
||||
if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) {
|
||||
avatar = await DataResolver.resolveImage(avatar);
|
||||
}
|
||||
if (channel) channel = channel?.id ?? channel;
|
||||
channel &&= channel.id ?? channel;
|
||||
const data = await this.client.api.webhooks(this.id, channel ? undefined : this.token).patch({
|
||||
data: { name, avatar, channel_id: channel },
|
||||
reason,
|
||||
|
||||
Reference in New Issue
Block a user