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

feat(GuildMemberUpdate): emit on user updates #6782

Closed
wants to merge 2 commits into from
Closed

feat(GuildMemberUpdate): emit on user updates #6782

wants to merge 2 commits into from

Conversation

Bas950
Copy link
Contributor

@Bas950 Bas950 commented Oct 7, 2021

Please describe the changes this PR makes and why it should be merged:
This PR changes the guildMemberUpdate event in a (hopefully) semver: minor way in order to emit it on user updates like Discord intends us to. The USER_UPDATE event will still be emitted on both the GUILD_MEMBER_UPDATE and the PRESENCE_UPDATE gateway events, however we will not fire presenceUpdate events as presences do not exist on users like it was discussed here. There is only 1 possible issue with this which is that the events will fire when we don't have a user's flags cached and they have flags in reality. This is due to the fact that Discord does not send the public_flags property on startup and thus it doesn't get cached. The only alternative would be not to emit the event when the old flags are not present, however this would become an issue in scenarios where the first userUpdate received on a session is a flags update.

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

@@ -62,7 +62,7 @@ class GuildMember extends Base {
* The user that this guild member instance represents
* @type {?User}
*/
this.user = this.client.users._add(data.user, true);
this.user = this.client.users._add(data.user, true)._clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always cloning the user here seems like a bad idea... Now you won't be able to do member.user === client.user for example (yes I KNOW niche case), AND you'll now create clones of the user object N times (where N is the number of guilds that user is in) instead of referencing to the same 1... Why not clone the user when you process the payload instead? @discordjs/the-big-3 thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with doing that is that you’d only be able to use the cloned user to compare it to the new user, but the user attached to the member object would be updated by the time guildMemberUpdate gets emitted (since userUpdate comes first and that mutates the user object)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So change the code in guildMemberUpdate to

const old = member._clone();
old.user = member.user._clone();
member._patch(data);

or something

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounded like a hacky solution but it’s definitely better, we’ll test

Copy link
Contributor Author

@Bas950 Bas950 Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea sorry @vladfrangu that won't work.
If we apply your suggestion, old.user will already be updated by the time it gets to guildMemberUpdate because presenceUpdate always seems to fire first (at least from our tests) which then emits a userUpdate and modifies the user object.

Even if a user doesn't have the GUILD_PRESENCES intent, your suggestion would still only work for the first guild that receives the update and not the ones that come after it (when the user is in more than 1 guild).

Hence why we now ._clone() the user objects to fully detach them from eachother.

Now you won't be able to do member.user === client.user for example (yes I KNOW niche case).

yes this is true, but the .equals function can be used instead (member.user.equals(client.user)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladfrangu I added a setting for it, hopefully you could rereview your standpoint as it's better as a setting since most peeps indeed don't need this.

@Bas950
Copy link
Contributor Author

Bas950 commented Oct 10, 2021

@SpaceEEC Could you please give some details as to why you downvoted?

@iCrawl iCrawl modified the milestones: Version 13.3, Version 13.4 Oct 28, 2021
@Bas950
Copy link
Contributor Author

Bas950 commented Oct 31, 2021

I agree with y'alls concerns for memory usage going up, hence why I think it would be a good idea to make this a ClientOption.
image
image
I have currently named it emitUserUpdatesPerGuild (uncommitted as per request of Crawl), if you guys like this idea then I will commit it and add it to this PR.
Let me know if you guys have a better naming for the added option, then I can also improve that :)

@iCrawl iCrawl modified the milestones: Version 13.4, Version 13.x Nov 11, 2021
@iCrawl iCrawl removed this from the Version 13.x milestone Dec 24, 2021
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
@Bas950
Copy link
Contributor Author

Bas950 commented Jan 10, 2022

@imranbarbhuiya thanks for notifying, fixed the issues, also rebased the pr to fix all merge conflicts.

@ImRodry
Copy link
Contributor

ImRodry commented Feb 8, 2022

This needs a rebase

@iCrawl
Copy link
Member

iCrawl commented Jun 5, 2022

We decided to not move forward with this and rather have a different take on this.

@iCrawl iCrawl closed this Jun 5, 2022
@iCrawl iCrawl added this to the discord.js v14 milestone Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

6 participants