interaction creation cooldown

This commit is contained in:
rxdn 2020-12-24 23:27:30 +00:00
parent 9fb2eacc67
commit e4433d4eda

View File

@ -1,8 +1,10 @@
package api package api
import ( import (
"fmt"
"github.com/TicketsBot/GoPanel/botcontext" "github.com/TicketsBot/GoPanel/botcontext"
"github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/messagequeue"
command "github.com/TicketsBot/worker/bot/command/impl" command "github.com/TicketsBot/worker/bot/command/impl"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rxdn/gdl/rest" "github.com/rxdn/gdl/rest"
@ -12,8 +14,6 @@ import (
func WhitelabelCreateInteractions(ctx *gin.Context) { func WhitelabelCreateInteractions(ctx *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := ctx.Keys["userid"].(uint64)
//TODO: COOLDOWN
// Get bot // Get bot
bot, err := database.Client.Whitelabel.GetByUserId(userId) bot, err := database.Client.Whitelabel.GetByUserId(userId)
if err != nil { if err != nil {
@ -33,6 +33,38 @@ func WhitelabelCreateInteractions(ctx *gin.Context) {
return return
} }
// Cooldown
key := fmt.Sprintf("tickets:interaction-create-cooldown:%d", bot.BotId)
// try to set first, prevent race condition
wasSet, err := messagequeue.Client.SetNX(key, 1, time.Minute * 15).Result()
if err != nil {
ctx.JSON(500, gin.H{
"success": false,
"error": err.Error(),
})
return
}
// on cooldown, tell user how long left
if !wasSet {
expiration, err := messagequeue.Client.TTL(key).Result()
if err != nil {
ctx.JSON(500, gin.H{
"success": false,
"error": err.Error(),
})
return
}
ctx.JSON(400, gin.H{
"success": false,
"error": fmt.Sprintf("Interaction creation on cooldown, please wait another %f minutes", expiration.Minutes()),
})
return
}
botContext, err := botcontext.ContextForGuild(0) botContext, err := botcontext.ContextForGuild(0)
if err != nil { if err != nil {
ctx.JSON(500, gin.H{ ctx.JSON(500, gin.H{