diff --git a/app/http/endpoints/api/whitelabel/whitelabelcreateinteractions.go b/app/http/endpoints/api/whitelabel/whitelabelcreateinteractions.go index 205c70c..a22799a 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelcreateinteractions.go +++ b/app/http/endpoints/api/whitelabel/whitelabelcreateinteractions.go @@ -1,8 +1,10 @@ package api import ( + "fmt" "github.com/TicketsBot/GoPanel/botcontext" "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/messagequeue" command "github.com/TicketsBot/worker/bot/command/impl" "github.com/gin-gonic/gin" "github.com/rxdn/gdl/rest" @@ -12,8 +14,6 @@ import ( func WhitelabelCreateInteractions(ctx *gin.Context) { userId := ctx.Keys["userid"].(uint64) - //TODO: COOLDOWN - // Get bot bot, err := database.Client.Whitelabel.GetByUserId(userId) if err != nil { @@ -33,6 +33,38 @@ func WhitelabelCreateInteractions(ctx *gin.Context) { 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) if err != nil { ctx.JSON(500, gin.H{