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

refactor: new node features #5132

Merged
merged 17 commits into from Jun 30, 2021
Merged

refactor: new node features #5132

merged 17 commits into from Jun 30, 2021

Conversation

NotSugden
Copy link
Contributor

@NotSugden NotSugden commented Dec 18, 2020

Please describe the changes this PR makes and why it should be merged:
This PR makes use of optional chaining and the nullish coalescing operator in various places, added in node v14,

aswell as a couple logic changes to use && instead of a tenary like x ? y : null where x can be null

Status

  • Code changes have been tested against the Discord API, or there are no code changes
    • untested as of yet
    • i might need some help fully testing this just because changing || to ?? may cause some unintended behaviour
  • I know how to update typings and have done so, or typings don't need updating

Semantic versioning classification:

  • This PR changes the library's interface (methods or parameters added)
    • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
  • This PR only includes non-code changes, like changes to documentation, README, etc.

@BannerBomb
Copy link
Contributor

BannerBomb commented Dec 18, 2020

Sorry for posting all the links. It just made it easier and quicker to do that than to comment on each line.

Shouldn't this be if (cache) existing._patch?.(data);. The code is already checking that the variable "existing" isn't undefined and on this line before you made the change was if (existing._patch &&
https://github.com/discordjs/discord.js/pull/5132/files#diff-19338f04c86e704e9e4cb898fc9d21411ca4774ecae906a4e3179a306f55974dR25

Shouldn't this be if (cache) existing?._patch?.(data);. The code before you made the change was if (existing && existing._patch &&.
https://github.com/discordjs/discord.js/pull/5132/files#diff-649365ccd46d128b24504a4bcbb566995be7338be1ca43868ad43ae9eaf802e7R46
Should be

This is missing a .delete() at the end of the api.
https://github.com/discordjs/discord.js/pull/5132/files#diff-0362e882b074f7f48e846fdc34e56a307b11eb067d140ddd0ec4b095cf86ce74R60-R63

All of the below can be ignored if they were intentionally done that way. I just brought them up because the other changes I saw were being made.

This isn't a error, but just a suggestion. Couldn't you use a for loop instead of reduce on these since all that's being done here is setting data to a collection, and for loops are significantly faster at iterations?
It would also make them consistent with fetchInvites, fetchWebhooks, fetchVoiceRegions.
https://github.com/discordjs/discord.js/pull/5132/files#diff-9c03fb4d8c1b03db876641d3900a9c08f5a3a30be8d8a3b9840018507243c284R668-R674
https://github.com/discordjs/discord.js/pull/5132/files#diff-9c03fb4d8c1b03db876641d3900a9c08f5a3a30be8d8a3b9840018507243c284R708
https://github.com/discordjs/discord.js/pull/5132/files#diff-9c03fb4d8c1b03db876641d3900a9c08f5a3a30be8d8a3b9840018507243c284R695-R698

Did you mean to leave these as Promise.reject instead of changing them to throw?
https://github.com/discordjs/discord.js/pull/5132/files#diff-9c03fb4d8c1b03db876641d3900a9c08f5a3a30be8d8a3b9840018507243c284R1383
https://github.com/discordjs/discord.js/pull/5132/files#diff-6931a2b8bde677910786bd21afe3bc29740277d6510de785acfebca7e616ad89R257

Can't you just return the this._patch method here since that method already returns "this" itself?
https://github.com/discordjs/discord.js/pull/5132/files#diff-771d176d28e8d7cd4ba65413e0562da9908ee989169a6d1b1aac819ed472cc40R147-R148
https://github.com/discordjs/discord.js/pull/5132/files#diff-771d176d28e8d7cd4ba65413e0562da9908ee989169a6d1b1aac819ed472cc40R166-R167

@NotSugden
Copy link
Contributor Author

Can't you just return the this._patch method here since that method already returns "this" itself?

actually, these methods should return void to be consistent with all of the other _patch methods

Did you mean to leave these as Promise.reject instead of changing them to throw?

fixed

This isn't a error, but just a suggestion. Couldn't you use a for loop instead of reduce on these since all that's being done here is setting data to a collection, and for loops are significantly faster at iterations?
It would also make them consistent with fetchInvites, fetchWebhooks, fetchVoiceRegions.
[...]

can you leave review comments on these lines? because the links you provided don't take me to them

Shouldn't this be if (cache) existing?._patch?.(data);. The code before you made the change was if (existing && existing._patch &&.

i think every structure has _patch anyway, but ill add that just to make sure

src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
iCrawl
iCrawl previously requested changes Dec 22, 2020
Copy link
Member

@iCrawl iCrawl left a comment

Choose a reason for hiding this comment

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

This looks mostly fine, only nit I would have is that you switched some for loops where you set into a collection to .reduce but left others intact. Should decide on one pattern over the other, but not mix-match so much.

@NotSugden NotSugden marked this pull request as draft December 22, 2020 21:10
@NotSugden
Copy link
Contributor Author

ill switch for loops to reduce, map, etc where possible

src/structures/Guild.js Outdated Show resolved Hide resolved
Copy link
Member

@kyranet kyranet left a comment

Choose a reason for hiding this comment

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

You can also replace all EventEmitter#removeListener with the newer EventEmitter#off, since we use EventEmitter#on over EventEmitter#addListener.

src/client/Client.js Outdated Show resolved Hide resolved
src/client/actions/GuildEmojisUpdate.js Show resolved Hide resolved
src/client/voice/networking/VoiceUDPClient.js Outdated Show resolved Hide resolved
src/errors/DJSError.js Outdated Show resolved Hide resolved
src/managers/GuildEmojiManager.js Outdated Show resolved Hide resolved
Copy link
Member

@kyranet kyranet left a comment

Choose a reason for hiding this comment

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

You can also replace all EventEmitter#removeListener with the newer EventEmitter#off, since we use EventEmitter#on over EventEmitter#addListener.

Copy link
Member

@kyranet kyranet left a comment

Choose a reason for hiding this comment

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

There are many cases where Array#reduce was used to fill an array of the same size, Array#map should be used instead.

I also found a lot of inconsistencies from existing code (not from this PR) regarding permissions:

In most cases, we assume permissionsFor(this.client.user) will always return a non-null value, which it indeed will, more than 99.99% of cases (when the GUILD intent is used (which discord.js needs to operate), Discord always sends the client's member, as such, guild.me is always available, even without the members or the presence intents).

The only case at which it can be null, is when a user sweeps discord.js's caches to save memory, (a practice that was discouraged, but was quite common regardless) but did not account for re-adding guild.me back, which lack breaks most of the library.

We need to consolidate this and decide what should we do in this regard, since in regular conditions this code will always work, it's the user's interference in discord.js's cache what makes it break.

Returning (not null, but undefined) in the getters is something I don't really agree with, and should be discussed.

src/rest/DiscordAPIError.js Outdated Show resolved Hide resolved
src/sharding/ShardingManager.js Outdated Show resolved Hide resolved
src/sharding/ShardingManager.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Message.js Show resolved Hide resolved
src/structures/Webhook.js Outdated Show resolved Hide resolved
src/util/BitField.js Outdated Show resolved Hide resolved
src/util/Constants.js Outdated Show resolved Hide resolved
src/util/Util.js Outdated Show resolved Hide resolved
@NotSugden
Copy link
Contributor Author

In https://github.com/discordjs/discord.js/blob/stable/src/util/DataResolver.js#L42 you can use optional chaining

its already being used there

src/rest/APIRequest.js Outdated Show resolved Hide resolved
src/structures/Message.js Outdated Show resolved Hide resolved
@NotSugden NotSugden force-pushed the node-features branch 2 times, most recently from 8c3085f to 57a340d Compare February 14, 2021 01:01
@NotSugden NotSugden marked this pull request as ready for review February 14, 2021 06:46
@NotSugden NotSugden marked this pull request as draft February 14, 2021 06:59
src/managers/ChannelManager.js Show resolved Hide resolved
src/structures/ClientPresence.js Outdated Show resolved Hide resolved
src/structures/GuildAuditLogs.js Outdated Show resolved Hide resolved
src/structures/Presence.js Show resolved Hide resolved
@vladfrangu vladfrangu self-requested a review June 29, 2021 19:38
@iCrawl
Copy link
Member

iCrawl commented Jun 29, 2021

This needs a rebase.

Copy link
Member

@vladfrangu vladfrangu left a comment

Choose a reason for hiding this comment

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

#5132 (comment) and #5132 (comment), not to mention that, and you won't believe it, this needs another rebase

@iCrawl iCrawl requested review from SpaceEEC and kyranet June 29, 2021 21:42
@NotSugden
Copy link
Contributor Author

reeeebased

Copy link
Member

@vladfrangu vladfrangu left a comment

Choose a reason for hiding this comment

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

Small thing, otherwise LGTM

src/structures/GuildAuditLogs.js Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/Guild.js Outdated Show resolved Hide resolved
src/structures/GuildTemplate.js Outdated Show resolved Hide resolved
src/structures/Message.js Outdated Show resolved Hide resolved
src/structures/Presence.js Outdated Show resolved Hide resolved
src/structures/Presence.js Outdated Show resolved Hide resolved
src/structures/interfaces/TextBasedChannel.js Outdated Show resolved Hide resolved
Copy link
Member

@SpaceEEC SpaceEEC left a comment

Choose a reason for hiding this comment

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

Three tiny nits left, otherwise LGTM.

src/structures/GuildMember.js Show resolved Hide resolved
src/structures/Message.js Show resolved Hide resolved
src/structures/User.js Show resolved Hide resolved
@iCrawl iCrawl merged commit 1e8f012 into discordjs:master Jun 30, 2021
@NotSugden NotSugden deleted the node-features branch July 28, 2021 22:09
@NotSugden NotSugden restored the node-features branch July 28, 2021 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet