mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
perf: linear speed position getters (#9528)
* perf(Channel): linear speed position getter (#9497) Co-authored-by: kyra <kyradiscord@gmail.com> * perf(Role): linear speed position getter --------- Co-authored-by: kyra <kyradiscord@gmail.com> Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@ const { Error } = require('../errors');
|
|||||||
const PermissionOverwriteManager = require('../managers/PermissionOverwriteManager');
|
const PermissionOverwriteManager = require('../managers/PermissionOverwriteManager');
|
||||||
const { VoiceBasedChannelTypes } = require('../util/Constants');
|
const { VoiceBasedChannelTypes } = require('../util/Constants');
|
||||||
const Permissions = require('../util/Permissions');
|
const Permissions = require('../util/Permissions');
|
||||||
|
const Util = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a guild channel from any of the following:
|
* Represents a guild channel from any of the following:
|
||||||
@@ -145,8 +146,21 @@ class GuildChannel extends Channel {
|
|||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get position() {
|
get position() {
|
||||||
const sorted = this.guild._sortedChannels(this);
|
const selfIsCategory = this.type === 'GUILD_CATEGORY';
|
||||||
return [...sorted.values()].indexOf(sorted.get(this.id));
|
const types = Util.getSortableGroupTypes(this.type);
|
||||||
|
|
||||||
|
let count = 0;
|
||||||
|
for (const channel of this.guild.channels.cache.values()) {
|
||||||
|
if (!types.includes(channel.type)) continue;
|
||||||
|
if (!selfIsCategory && channel.parentId !== this.parentId) continue;
|
||||||
|
if (this.rawPosition === channel.rawPosition) {
|
||||||
|
if (BigInt(channel.id) < BigInt(this.id)) count++;
|
||||||
|
} else if (this.rawPosition > channel.rawPosition) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -228,8 +228,13 @@ class Role extends Base {
|
|||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get position() {
|
get position() {
|
||||||
const sorted = this.guild._sortedRoles();
|
let count = 0;
|
||||||
return [...sorted.values()].indexOf(sorted.get(this.id));
|
for (const role of this.guild.roles.cache.values()) {
|
||||||
|
if (this.rawPosition > role.rawPosition) count++;
|
||||||
|
else if (this.rawPosition === role.rawPosition && BigInt(this.id) > BigInt(role.id)) count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ let deprecationEmittedForSplitMessage = false;
|
|||||||
let deprecationEmittedForRemoveMentions = false;
|
let deprecationEmittedForRemoveMentions = false;
|
||||||
let deprecationEmittedForResolveAutoArchiveMaxLimit = false;
|
let deprecationEmittedForResolveAutoArchiveMaxLimit = false;
|
||||||
|
|
||||||
|
const TextSortableGroupTypes = ['GUILD_TEXT', 'GUILD_ANNOUCMENT', 'GUILD_FORUM'];
|
||||||
|
const VoiceSortableGroupTypes = ['GUILD_VOICE', 'GUILD_STAGE_VOICE'];
|
||||||
|
const CategorySortableGroupTypes = ['GUILD_CATEGORY'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains various general-purpose utility methods.
|
* Contains various general-purpose utility methods.
|
||||||
*/
|
*/
|
||||||
@@ -742,6 +746,31 @@ class Util extends null {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an array of the channel types that can be moved in the channel group. For example, a GuildText channel would
|
||||||
|
* return an array containing the types that can be ordered within the text channels (always at the top), and a voice
|
||||||
|
* channel would return an array containing the types that can be ordered within the voice channels (always at the
|
||||||
|
* bottom).
|
||||||
|
* @param {ChannelType} type The type of the channel
|
||||||
|
* @returns {ChannelType[]}
|
||||||
|
* @ignore
|
||||||
|
*/
|
||||||
|
static getSortableGroupTypes(type) {
|
||||||
|
switch (type) {
|
||||||
|
case 'GUILD_TEXT':
|
||||||
|
case 'GUILD_ANNOUNCEMENT':
|
||||||
|
case 'GUILD_FORUM':
|
||||||
|
return TextSortableGroupTypes;
|
||||||
|
case 'GUILD_VOICE':
|
||||||
|
case 'GUILD_STAGE_VOICE':
|
||||||
|
return VoiceSortableGroupTypes;
|
||||||
|
case 'GUILD_CATEGORY':
|
||||||
|
return CategorySortableGroupTypes;
|
||||||
|
default:
|
||||||
|
return [type];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the default avatar index for a given user id.
|
* Calculates the default avatar index for a given user id.
|
||||||
* @param {Snowflake} userId - The user id to calculate the default avatar index for
|
* @param {Snowflake} userId - The user id to calculate the default avatar index for
|
||||||
|
|||||||
Reference in New Issue
Block a user