使用命令的时间限制

huangapple go评论68阅读模式
英文:

Time limit for using commands

问题

我正在使用JDA编写一个机器人。

我已经编写了几个命令,我希望能够让用户只能调用一次该命令,然后他必须等待,比如说5秒钟才能再次使用它。
我尝试使用了java.util.concurrent.TimeUnit库来实现这一点。
但问题是,在这5秒钟过后,机器人开始回复我在命令未被调用的期间内发送的消息。

必须要做到的是,他只回复一次,而忽略后续相同类型的消息。

如果你需要源代码,请在评论中写明。
提前谢谢,祝好运,也祝没有错误。

英文:

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

得分: 1

你正在把它变得过于复杂,解决方案要简单得多。只需在用户执行命令时存储一个时间戳。如果用户发送另一个命令,将当前时间戳与先前的时间戳进行比较,如果它们在5秒内,就忽略该命令。您可以根据需求为每个用户、每个命令或两者都存储一个时间戳。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月30日 22:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定