discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.
https://discordjs.guide
MIT License
1.57k stars 2.31k forks source link

Make cooldown guide more verbose #564

Closed almostSouji closed 3 years ago

almostSouji commented 3 years ago

Improve level of detail in the cooldown section of the guide, as the procedure currently is not explained in much detail. This came up from the original post below:


Sorry to add to this, but on the same vein of timestamp questions, what exactly do timestamps and cooldown have inside?

Trying to print timestamp using spread syntax and entries() like so [...timestamps.entries()] gives my message.author.id (as expected, since it compares it on the if) and some random value (note, I put the console.log() after the timestamps.set(...) line as seen here, otherwise it'd be empty):

timestamps inside:
        182923807449808914,1610093587419

While trying to print cooldowns Collection gives me the name of the command, and an [object Map] (also using spread syntax and entries), as seen here (I put this console.log() next to the timestamps console.log()):

cooldowns inside:        
        ping,[object Map]

In cooldowns case, there is a .set() on line 54 that fills it with the command name and a new, empty Collection. But that collection is never filled. Why is that?

I know I might sound oblivious to of the guide explanations, but apart from the quick enumeration after the 3rd snippet on Cooldown section (which cites timestamps as "A variable with the current timestamp."), there is not much explanation of what is going on inside.

If this type of questions must be asked on the Discord of the module, please let me know.

Originally posted by @lihuelworks in https://github.com/discordjs/guide/issues/197#issuecomment-756626383

almostSouji commented 3 years ago

@lihuelworks This could indeed be better explained, hence I made it a new issue for better tracking. Thanks for bringing it up!

I'll still try to quickly explain what's happening here:

You save cooldowns in a Collection<string, Collection<string, number> format. You do this to associate the timestamp of when a command is used with a user and a specific command. An annotated version of the signature would be Collection<command-id, Collection<user-id, timestamp>>.

and some random value

Said "random value" is the timestamp we are after.

But that collection is never filled. Why is that?

This is a misunderstanding on your end and here is why:

Line 54 sets a new (empty) Collection<userid, timestamp> if there is none yet (for the command that has just been executed) Line 58 retrieves this collection (maybe filled if there was one before, empty if it was just set) Line 70 sets the user-id and timestamp combination to the underlying timestamps collection.

Note: You assign a reference to the nested Collection<userid, timestamp> here, meaning that if you later (L.70) set to it this will modify the timestamps, this is where the collection is filled.

https://github.com/discordjs/guide/blob/3d5e43db53230644f47feb21a85c4ea05b5314d8/code-samples/command-handling/adding-features/12/index.js#L70-L71

We also set a time after which this timestamp entry (user and timestamp of the point when they last used the command) is deleted - which is precisely what we specify as command cooldown in the command file, transformed into milliseconds (L.59)


Line references for this commit

lihuelworks commented 3 years ago

Thank you so much Souji, this really fixed some questions I had about Collections use on DiscordJS.

Also, a rookie concept that went over my head at first (and probably would on other people): The timestamp is UNIX formated, so what I thought as a "random" number is actually the number of seconds since epoch (or jan 1st of 1970), which seems like an obvious concept, but someone who's not familiar with date formats migh also miss.