Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with the channelUpdate event #7916

Closed
HamoodiHajjiri opened this issue May 13, 2022 · 6 comments
Closed

Problem with the channelUpdate event #7916

HamoodiHajjiri opened this issue May 13, 2022 · 6 comments

Comments

@HamoodiHajjiri
Copy link

Which package is this bug report for?

discord.js

Issue description

I was willing to use the GuildChannel.position property for both the oldChannel and the newChannel parameters, however, I've realised that both values return the same number.

Code sample

// Checking if the channel position has changed. If it has, it will return. 
if (oldChannel.position !== newChannel.position) return;

Package version

V13.6.0

Node.js version

V18.0.0

Operating system

Windows 11 Pro

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

User, Channel, GuildMember, Message, Reaction, GuildScheduledEvent, ThreadMember

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildBans, GuildEmojisAndStickers, GuildIntegrations, GuildWebhooks, GuildInvites, GuildVoiceStates, GuildPresences, GuildMessages, GuildMessageReactions, GuildMessageTyping, DirectMessages, DirectMessageReactions, DirectMessageTyping, GuildScheduledEvents

I have tested this issue on a development release

No response

@almeidx
Copy link
Member

almeidx commented May 13, 2022

This is because position is a getter, which calls the Guild#_sortedChannels() method under the hood, which sorts all the channels (from <Client>.channels.cache)

get position() {
const sorted = this.guild._sortedChannels(this);
return [...sorted.values()].indexOf(sorted.get(this.id));
}

_sortedChannels(channel) {
const category = channel.type === ChannelTypes.GUILD_CATEGORY;
return Util.discordSort(
this.channels.cache.filter(
c =>
(['GUILD_TEXT', 'GUILD_NEWS', 'GUILD_STORE'].includes(channel.type)
? ['GUILD_TEXT', 'GUILD_NEWS', 'GUILD_STORE'].includes(c.type)
: c.type === channel.type) &&
(category || c.parent === channel.parent),
),
);
}

Because of this, both oldChannel.position and newChannel.position will always return the (same) most up-to-date position

@almeidx
Copy link
Member

almeidx commented May 13, 2022

I suppose one way of working around this issue is to manually set the position on the clone to the value of the getter, before patching the channel

_update(data) {
const clone = this._clone();
this._patch(data);
return clone;
}

Something like:

 _update(data) { 
   const clone = this._clone(); 
   Reflect.defineProperty(clone, 'position', { value: clone.position });
   this._patch(data); 
   return clone; 
 } 

this would be done on the GuildChannel class, not Base

However, this introduces some overhead on every channel update, and will still be prone to race conditions when multiple channels are updated at the same time

@HamoodiHajjiri
Copy link
Author

I suppose a way around this is to use .rawPosition to not have both return the (same) most up-to-date position, correct?

@pranshu05
Copy link

I suppose one way of working around this issue is to manually set the position on the clone to the value of the getter, before patching the channel

_update(data) {
const clone = this._clone();
this._patch(data);
return clone;
}

Something like:

 _update(data) { 
   const clone = this._clone(); 
   Reflect.defineProperty(clone, 'position', { value: clone.position });
   this._patch(data); 
   return clone; 
 } 

this would be done on the GuildChannel class, not Base

However, this introduces some overhead on every channel update, and will still be prone to race conditions when multiple channels are updated at the same time

Yes... Cloning values works

@JMTK
Copy link
Contributor

JMTK commented Nov 11, 2023

Can you confirm if this is fixed after this is now in main? #9497

@almostSouji
Copy link
Member

resolved as of 09b0382
if you can still reproduce, please open a new issue with sufficient details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants