Skip to content

Commit 09b0382

Browse files
kyranetvladfrangukodiakhq[bot]
authored
perf(Channel): linear speed position getter (#9497)
* perf(Channel): linear speed position getter * fix: add another set of parens * perf: lower memory and CPU usage * refactor: add shared private utility for group types * perf: improve readability and performance Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> * feat: add support for voice sortables --------- Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 719e54a commit 09b0382

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

packages/discord.js/src/structures/Guild.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const VoiceStateManager = require('../managers/VoiceStateManager');
2828
const DataResolver = require('../util/DataResolver');
2929
const Status = require('../util/Status');
3030
const SystemChannelFlagsBitField = require('../util/SystemChannelFlagsBitField');
31-
const { discordSort } = require('../util/Util');
31+
const { discordSort, getSortableGroupTypes } = require('../util/Util');
3232

3333
/**
3434
* Represents a guild (or a server) on Discord.
@@ -1351,14 +1351,10 @@ class Guild extends AnonymousGuild {
13511351
* @private
13521352
*/
13531353
_sortedChannels(channel) {
1354-
const category = channel.type === ChannelType.GuildCategory;
1355-
const channelTypes = [ChannelType.GuildText, ChannelType.GuildAnnouncement];
1354+
const channelIsCategory = channel.type === ChannelType.GuildCategory;
1355+
const types = getSortableGroupTypes(channel.type);
13561356
return discordSort(
1357-
this.channels.cache.filter(
1358-
c =>
1359-
(channelTypes.includes(channel.type) ? channelTypes.includes(c.type) : c.type === channel.type) &&
1360-
(category || c.parent === channel.parent),
1361-
),
1357+
this.channels.cache.filter(c => types.includes(c.type) && (channelIsCategory || c.parentId === channel.parentId)),
13621358
);
13631359
}
13641360
}

packages/discord.js/src/structures/GuildChannel.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

3-
const { PermissionFlagsBits } = require('discord-api-types/v10');
3+
const { Snowflake } = require('@sapphire/snowflake');
4+
const { PermissionFlagsBits, ChannelType } = require('discord-api-types/v10');
45
const { BaseChannel } = require('./BaseChannel');
56
const { DiscordjsError, ErrorCodes } = require('../errors');
67
const PermissionOverwriteManager = require('../managers/PermissionOverwriteManager');
78
const { VoiceBasedChannelTypes } = require('../util/Constants');
89
const PermissionsBitField = require('../util/PermissionsBitField');
10+
const { getSortableGroupTypes } = require('../util/Util');
911

1012
/**
1113
* Represents a guild channel from any of the following:
@@ -145,8 +147,21 @@ class GuildChannel extends BaseChannel {
145147
* @readonly
146148
*/
147149
get position() {
148-
const sorted = this.guild._sortedChannels(this);
149-
return [...sorted.values()].indexOf(sorted.get(this.id));
150+
const selfIsCategory = this.type === ChannelType.GuildCategory;
151+
const types = getSortableGroupTypes(this.type);
152+
153+
let count = 0;
154+
for (const channel of this.guild.channels.cache.values()) {
155+
if (!types.includes(channel.type)) continue;
156+
if (!selfIsCategory && channel.parentId !== this.parentId) continue;
157+
if (this.rawPosition === channel.rawPosition) {
158+
if (Snowflake.compare(channel.id, this.id) === -1) count++;
159+
} else if (this.rawPosition > channel.rawPosition) {
160+
count++;
161+
}
162+
}
163+
164+
return count;
150165
}
151166

152167
/**

packages/discord.js/src/util/Util.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,35 @@ function makePlainError(err) {
163163
};
164164
}
165165

166+
const TextSortableGroupTypes = [ChannelType.GuildText, ChannelType.GuildAnnouncement, ChannelType.GuildForum];
167+
const VoiceSortableGroupTypes = [ChannelType.GuildVoice, ChannelType.GuildStageVoice];
168+
const CategorySortableGroupTypes = [ChannelType.GuildCategory];
169+
170+
/**
171+
* Gets an array of the channel types that can be moved in the channel group. For example, a GuildText channel would
172+
* return an array containing the types that can be ordered within the text channels (always at the top), and a voice
173+
* channel would return an array containing the types that can be ordered within the voice channels (always at the
174+
* bottom).
175+
* @param {ChannelType} type The type of the channel
176+
* @returns {ChannelType[]}
177+
* @private
178+
*/
179+
function getSortableGroupTypes(type) {
180+
switch (type) {
181+
case ChannelType.GuildText:
182+
case ChannelType.GuildAnnouncement:
183+
case ChannelType.GuildForum:
184+
return TextSortableGroupTypes;
185+
case ChannelType.GuildVoice:
186+
case ChannelType.GuildStageVoice:
187+
return VoiceSortableGroupTypes;
188+
case ChannelType.GuildCategory:
189+
return CategorySortableGroupTypes;
190+
default:
191+
return [type];
192+
}
193+
}
194+
166195
/**
167196
* Moves an element in an array *in place*.
168197
* @param {Array<*>} array Array to modify
@@ -379,6 +408,7 @@ module.exports = {
379408
mergeDefault,
380409
makeError,
381410
makePlainError,
411+
getSortableGroupTypes,
382412
moveElementInArray,
383413
verifyString,
384414
resolveColor,

0 commit comments

Comments
 (0)