Skip to content

Commit b6fda78

Browse files
sdanialrazaalmeidxQjuh
authored
refactor: remove parameter reassignment (#10715)
* refactor: remove parameter reassignment * refactor: requested changes Co-authored-by: Almeida <[email protected]> * chore: requested changes Co-authored-by: Qjuh <[email protected]> * chore: requested changes * refactor: destructure in parameters Co-authored-by: Almeida <[email protected]> * refactor: apply suggested changes --------- Co-authored-by: Almeida <[email protected]> Co-authored-by: Qjuh <[email protected]>
1 parent bb67671 commit b6fda78

21 files changed

+196
-185
lines changed

packages/discord.js/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
"rest-spread-spacing": "error",
169169
"template-curly-spacing": "error",
170170
"yield-star-spacing": "error",
171+
"no-param-reassign": "error",
171172

172173
"no-restricted-globals": [
173174
"error",

packages/discord.js/src/managers/ApplicationEmojiManager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ class ApplicationEmojiManager extends CachedManager {
4949
* .catch(console.error);
5050
*/
5151
async create({ attachment, name }) {
52-
attachment = await resolveImage(attachment);
53-
if (!attachment) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType);
52+
const image = await resolveImage(attachment);
53+
if (!image) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType);
5454

55-
const body = { image: attachment, name };
55+
const body = { image, name };
5656

5757
const emoji = await this.client.rest.post(Routes.applicationEmojis(this.application.id), { body });
5858
return this._add(emoji);

packages/discord.js/src/managers/GuildChannelManager.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class GuildChannelManager extends CachedManager {
5353
get channelCountWithoutThreads() {
5454
return this.cache.reduce((acc, channel) => {
5555
if (ThreadChannelTypes.includes(channel.type)) return acc;
56-
return ++acc;
56+
return acc + 1;
5757
}, 0);
5858
}
5959

@@ -184,9 +184,6 @@ class GuildChannelManager extends CachedManager {
184184
defaultForumLayout,
185185
reason,
186186
}) {
187-
parent &&= this.client.channels.resolveId(parent);
188-
permissionOverwrites &&= permissionOverwrites.map(overwrite => PermissionOverwrites.resolve(overwrite, this.guild));
189-
190187
const data = await this.client.rest.post(Routes.guildChannels(this.guild.id), {
191188
body: {
192189
name,
@@ -195,9 +192,11 @@ class GuildChannelManager extends CachedManager {
195192
nsfw,
196193
bitrate,
197194
user_limit: userLimit,
198-
parent_id: parent,
195+
parent_id: parent && this.client.channels.resolveId(parent),
199196
position,
200-
permission_overwrites: permissionOverwrites,
197+
permission_overwrites: permissionOverwrites?.map(overwrite =>
198+
PermissionOverwrites.resolve(overwrite, this.guild),
199+
),
201200
rate_limit_per_user: rateLimitPerUser,
202201
rtc_region: rtcRegion,
203202
video_quality_mode: videoQualityMode,
@@ -235,18 +234,19 @@ class GuildChannelManager extends CachedManager {
235234
* .catch(console.error)
236235
*/
237236
async createWebhook({ channel, name, avatar, reason }) {
238-
const id = this.resolveId(channel);
239-
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');
237+
const channelId = this.resolveId(channel);
238+
if (!channelId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');
240239

241-
const resolvedImage = await resolveImage(avatar);
240+
const resolvedAvatar = await resolveImage(avatar);
242241

243-
const data = await this.client.rest.post(Routes.channelWebhooks(id), {
242+
const data = await this.client.rest.post(Routes.channelWebhooks(channelId), {
244243
body: {
245244
name,
246-
avatar: resolvedImage,
245+
avatar: resolvedAvatar,
247246
},
248247
reason,
249248
});
249+
250250
return new Webhook(this.client, data);
251251
}
252252

@@ -361,13 +361,14 @@ class GuildChannelManager extends CachedManager {
361361
* .catch(console.error);
362362
*/
363363
async setPosition(channel, position, { relative, reason } = {}) {
364-
channel = this.resolve(channel);
365-
if (!channel) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');
364+
const resolvedChannel = this.resolve(channel);
365+
if (!resolvedChannel) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');
366+
366367
const updatedChannels = await setPosition(
367-
channel,
368+
resolvedChannel,
368369
position,
369370
relative,
370-
this.guild._sortedChannels(channel),
371+
this.guild._sortedChannels(resolvedChannel),
371372
this.client,
372373
Routes.guildChannels(this.guild.id),
373374
reason,
@@ -377,7 +378,8 @@ class GuildChannelManager extends CachedManager {
377378
guild_id: this.guild.id,
378379
channels: updatedChannels,
379380
});
380-
return channel;
381+
382+
return resolvedChannel;
381383
}
382384

383385
/**
@@ -459,17 +461,18 @@ class GuildChannelManager extends CachedManager {
459461
* .catch(console.error);
460462
*/
461463
async setPositions(channelPositions) {
462-
channelPositions = channelPositions.map(channelPosition => ({
464+
const resolvedChannelPositions = channelPositions.map(channelPosition => ({
463465
id: this.client.channels.resolveId(channelPosition.channel),
464466
position: channelPosition.position,
465467
lock_permissions: channelPosition.lockPermissions,
466468
parent_id: channelPosition.parent !== undefined ? this.resolveId(channelPosition.parent) : undefined,
467469
}));
468470

469-
await this.client.rest.patch(Routes.guildChannels(this.guild.id), { body: channelPositions });
471+
await this.client.rest.patch(Routes.guildChannels(this.guild.id), { body: resolvedChannelPositions });
472+
470473
return this.client.actions.GuildChannelsPositionUpdate.handle({
471474
guild_id: this.guild.id,
472-
channels: channelPositions,
475+
channels: resolvedChannelPositions,
473476
}).guild;
474477
}
475478

packages/discord.js/src/managers/GuildEmojiManager.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ class GuildEmojiManager extends CachedManager {
8585
if (emoji instanceof ApplicationEmoji) return emoji.identifier;
8686
if (typeof emoji === 'string') {
8787
const res = parseEmoji(emoji);
88+
let identifier = emoji;
8889
if (res?.name.length) {
89-
emoji = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`;
90+
identifier = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`;
9091
}
91-
if (!emoji.includes('%')) return encodeURIComponent(emoji);
92-
return emoji;
92+
if (!identifier.includes('%')) return encodeURIComponent(identifier);
93+
return identifier;
9394
}
9495
return null;
9596
}
@@ -119,10 +120,10 @@ class GuildEmojiManager extends CachedManager {
119120
* .catch(console.error);
120121
*/
121122
async create({ attachment, name, roles, reason }) {
122-
attachment = await resolveImage(attachment);
123-
if (!attachment) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType);
123+
const image = await resolveImage(attachment);
124+
if (!image) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType);
124125

125-
const body = { image: attachment, name };
126+
const body = { image, name };
126127
if (roles) {
127128
if (!Array.isArray(roles) && !(roles instanceof Collection)) {
128129
throw new DiscordjsTypeError(
@@ -222,9 +223,9 @@ class GuildEmojiManager extends CachedManager {
222223
* @returns {Promise<User>}
223224
*/
224225
async fetchAuthor(emoji) {
225-
emoji = this.resolve(emoji);
226-
if (!emoji) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true);
227-
if (emoji.managed) {
226+
const resolvedEmoji = this.resolve(emoji);
227+
if (!resolvedEmoji) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true);
228+
if (resolvedEmoji.managed) {
228229
throw new DiscordjsError(ErrorCodes.EmojiManaged);
229230
}
230231

@@ -234,9 +235,9 @@ class GuildEmojiManager extends CachedManager {
234235
throw new DiscordjsError(ErrorCodes.MissingManageGuildExpressionsPermission, this.guild);
235236
}
236237

237-
const data = await this.client.rest.get(Routes.guildEmoji(this.guild.id, emoji.id));
238-
emoji._patch(data);
239-
return emoji.author;
238+
const data = await this.client.rest.get(Routes.guildEmoji(this.guild.id, resolvedEmoji.id));
239+
resolvedEmoji._patch(data);
240+
return resolvedEmoji.author;
240241
}
241242
}
242243

packages/discord.js/src/managers/GuildEmojiRoleManager.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ class GuildEmojiRoleManager extends DataManager {
4040
* @returns {Promise<GuildEmoji>}
4141
*/
4242
async add(roleOrRoles) {
43-
if (!Array.isArray(roleOrRoles) && !(roleOrRoles instanceof Collection)) roleOrRoles = [roleOrRoles];
43+
const roles = Array.isArray(roleOrRoles) || roleOrRoles instanceof Collection ? roleOrRoles : [roleOrRoles];
4444

45-
const resolvedRoles = [];
46-
for (const role of roleOrRoles.values()) {
47-
const resolvedRole = this.guild.roles.resolveId(role);
48-
if (!resolvedRole) {
45+
const resolvedRoleIds = [];
46+
for (const role of roles.values()) {
47+
const roleId = this.guild.roles.resolveId(role);
48+
if (!roleId) {
4949
throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role);
5050
}
51-
resolvedRoles.push(resolvedRole);
51+
resolvedRoleIds.push(roleId);
5252
}
5353

54-
const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))];
54+
const newRoles = [...new Set(resolvedRoleIds.concat(...this.cache.keys()))];
5555
return this.set(newRoles);
5656
}
5757

@@ -61,10 +61,10 @@ class GuildEmojiRoleManager extends DataManager {
6161
* @returns {Promise<GuildEmoji>}
6262
*/
6363
async remove(roleOrRoles) {
64-
if (!Array.isArray(roleOrRoles) && !(roleOrRoles instanceof Collection)) roleOrRoles = [roleOrRoles];
64+
const roles = Array.isArray(roleOrRoles) || roleOrRoles instanceof Collection ? roleOrRoles : [roleOrRoles];
6565

6666
const resolvedRoleIds = [];
67-
for (const role of roleOrRoles.values()) {
67+
for (const role of roles.values()) {
6868
const roleId = this.guild.roles.resolveId(role);
6969
if (!roleId) {
7070
throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role);

packages/discord.js/src/managers/GuildMemberManager.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,15 @@ class GuildMemberManager extends CachedManager {
220220
limit = 0,
221221
withPresences: presences,
222222
users,
223-
query,
223+
query: initialQuery,
224224
time = 120e3,
225225
nonce = DiscordSnowflake.generate().toString(),
226-
} = {}) {
226+
}) {
227227
if (nonce.length > 32) throw new DiscordjsRangeError(ErrorCodes.MemberFetchNonceLength);
228228

229+
const query = initialQuery || (!users ? '' : undefined);
230+
229231
return new Promise((resolve, reject) => {
230-
if (!query && !users) query = '';
231232
this.guild.client.ws.send(this.guild.shardId, {
232233
op: GatewayOpcodes.RequestGuildMembers,
233234
d: {

packages/discord.js/src/managers/GuildMemberRoleManager.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ class GuildMemberRoleManager extends DataManager {
121121
const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))];
122122
return this.set(newRoles, reason);
123123
} else {
124-
roleOrRoles = this.guild.roles.resolveId(roleOrRoles);
125-
if (roleOrRoles === null) {
124+
const resolvedRoleId = this.guild.roles.resolveId(roleOrRoles);
125+
if (resolvedRoleId === null) {
126126
throw new DiscordjsTypeError(
127127
ErrorCodes.InvalidType,
128128
'roles',
129129
'Role, Snowflake or Array or Collection of Roles or Snowflakes',
130130
);
131131
}
132132

133-
await this.client.rest.put(Routes.guildMemberRole(this.guild.id, this.member.id, roleOrRoles), { reason });
133+
await this.client.rest.put(Routes.guildMemberRole(this.guild.id, this.member.id, resolvedRoleId), { reason });
134134

135135
const clone = this.member._clone();
136-
clone._roles = [...this.cache.keys(), roleOrRoles];
136+
clone._roles = [...this.cache.keys(), resolvedRoleId];
137137
return clone;
138138
}
139139
}
@@ -160,19 +160,19 @@ class GuildMemberRoleManager extends DataManager {
160160
const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id));
161161
return this.set(newRoles, reason);
162162
} else {
163-
roleOrRoles = this.guild.roles.resolveId(roleOrRoles);
164-
if (roleOrRoles === null) {
163+
const resolvedRoleId = this.guild.roles.resolveId(roleOrRoles);
164+
if (resolvedRoleId === null) {
165165
throw new DiscordjsTypeError(
166166
ErrorCodes.InvalidType,
167167
'roles',
168168
'Role, Snowflake or Array or Collection of Roles or Snowflakes',
169169
);
170170
}
171171

172-
await this.client.rest.delete(Routes.guildMemberRole(this.guild.id, this.member.id, roleOrRoles), { reason });
172+
await this.client.rest.delete(Routes.guildMemberRole(this.guild.id, this.member.id, resolvedRoleId), { reason });
173173

174174
const clone = this.member._clone();
175-
const newRoles = this.cache.filter(role => role.id !== roleOrRoles);
175+
const newRoles = this.cache.filter(role => role.id !== resolvedRoleId);
176176
clone._roles = [...newRoles.keys()];
177177
return clone;
178178
}

packages/discord.js/src/managers/GuildStickerManager.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ class GuildStickerManager extends CachedManager {
5959
*/
6060
async create({ file, name, tags, description, reason } = {}) {
6161
const resolvedFile = await MessagePayload.resolveFile(file);
62-
if (!resolvedFile) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType);
63-
file = { ...resolvedFile, key: 'file' };
62+
resolvedFile.key = 'file';
6463

6564
const body = { name, tags, description: description ?? '' };
6665

6766
const sticker = await this.client.rest.post(Routes.guildStickers(this.guild.id), {
6867
appendToFormData: true,
6968
body,
70-
files: [file],
69+
files: [resolvedFile],
7170
reason,
7271
});
7372
return this.client.actions.GuildStickerCreate.handle(this.guild, sticker).sticker;
@@ -129,10 +128,10 @@ class GuildStickerManager extends CachedManager {
129128
* @returns {Promise<void>}
130129
*/
131130
async delete(sticker, reason) {
132-
sticker = this.resolveId(sticker);
133-
if (!sticker) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sticker', 'StickerResolvable');
131+
const resolvedStickerId = this.resolveId(sticker);
132+
if (!resolvedStickerId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sticker', 'StickerResolvable');
134133

135-
await this.client.rest.delete(Routes.guildSticker(this.guild.id, sticker), { reason });
134+
await this.client.rest.delete(Routes.guildSticker(this.guild.id, resolvedStickerId), { reason });
136135
}
137136

138137
/**
@@ -171,11 +170,11 @@ class GuildStickerManager extends CachedManager {
171170
* @returns {Promise<?User>}
172171
*/
173172
async fetchUser(sticker) {
174-
sticker = this.resolve(sticker);
175-
if (!sticker) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sticker', 'StickerResolvable');
176-
const data = await this.client.rest.get(Routes.guildSticker(this.guild.id, sticker.id));
177-
sticker._patch(data);
178-
return sticker.user;
173+
const resolvedSticker = this.resolve(sticker);
174+
if (!resolvedSticker) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sticker', 'StickerResolvable');
175+
const data = await this.client.rest.get(Routes.guildSticker(this.guild.id, resolvedSticker.id));
176+
resolvedSticker._patch(data);
177+
return resolvedSticker.user;
179178
}
180179
}
181180

0 commit comments

Comments
 (0)