Time limit for using commands

145 Views Asked by At

I am using JDA to write a bot.

I have already written several commands and I want to make it so that the user can call the command only once, then he has to wait, say, 5 seconds to use it again. I tried to do this using the java.util.concurrent.TimeUnit library. But the problem is that after these 5 seconds the bot begins to respond to messages that I sent during the time the command was not called.

And it is necessary so that he only replies to it times, and ignored subsequent messages of the same kind.

If you need the source code, please write in the comments. Thank you in advance, good luck and no bugs.

1

There are 1 best solutions below

1
On BEST ANSWER

You're making it way to complicated, the solution is much more simple. Just store a timestamp whenever a user executes a command. If the user sends another command, compare the current timestamp with the previous one, and if there within 5 seconds, ignore the command.You can store a timestamp per user, per command, or both depending on your requirements.