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

Cooldown section, where if check for timestamps.has(message.author.id) does not work #197

Closed Arxero closed 5 years ago

Arxero commented 5 years ago

So this is where I am and I can say that it does not work properly cause you never have message.author.id in the first place in timestamps collection up until this point.

And it will never enter those if therefore it won't show the message that is intended to show.

monbrey commented 5 years ago

The code for adding message.author.id to the timestamps collection is further down the page, at the bottom of the Cooldowns section

timestamps.set(message.author.id, now); setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

Arxero commented 5 years ago

hmm, I know that, but then it gets deleted immediately and it never gets in that if before it :X

monbrey commented 5 years ago

A timeout is set to delete it after the cooldown time has expired, not immediately. The default is three seconds, unless the command has a different cooldown set:

const cooldownAmount = (command.cooldown || 3) * 1000;

If this code isn't working for you, I recommend discussing it in the Discord.js server. I can't see anything at fault in the guide.

Arxero commented 5 years ago

the code definitely ain't working for me, I know what the idea behind is but don't know why in my case I never have author id in my timestamps map.

Danktuary commented 5 years ago

It'd help if posted your current code. There's nothing wrong with that piece of code from the guide. Running yarn init -y && yarn add discord.js && node . inside the code-samples/command-handling/adding-features directory (after you put your token in the config.json file in the same folder) makes it run as intended.

If you comment out the if statement you mentioned, you'll never see that cooldown message, thus allowing commands to be spammed.

I'd suggest going through that section from the beginning and trying again. If you're still having issues, you can ask in the d.js server. Thanks.

Arxero commented 5 years ago

thanks a bunch for the help I asked in the server and that guy Havoc helped me out. Screenshot_1 https://github.com/Arxero/mavbot/blob/master/index.js#L41 https://github.com/discordjs/guide/blob/master/code-samples/command-handling/adding-features/index.js#L15

I will be going there from now one, instead of spamming here.

lihuelworks commented 3 years ago

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.

almostSouji commented 3 years ago

I see a lack of detail and clarity in this guide section, #564 will be used to track this.

Additionally, I provided a short answer to your question there - This is separate and a different issue from this thread, where the guide code simply was not applied correctly which caused issues due to scope lifetimes.