Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added cooldowns for commands #12

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/bot/on_private_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/esfands/retpaladinbot/internal/bot/commands"
"github.com/esfands/retpaladinbot/internal/global"
"github.com/esfands/retpaladinbot/pkg/utils"
"github.com/gempir/go-twitch-irc/v4"
"golang.org/x/exp/slog"
)
Expand Down Expand Up @@ -37,6 +38,12 @@ func handleCommand(gctx global.Context, commandManager *commands.CommandManager,
return "", err
}

// Apply cooldown
isOnCooldown := utils.CooldownCanContinue(user, strings.ToLower(context[0]), dc.UserCooldown(), dc.GlobalCooldown())
if !isOnCooldown {
return "", nil
}

return response, nil

} else {
Expand All @@ -49,6 +56,12 @@ func handleCommand(gctx global.Context, commandManager *commands.CommandManager,
return "", err
}

// Apply cooldown
isOnCooldown := utils.CooldownCanContinue(user, strings.ToLower(context[0]), dc.UserCooldown(), dc.GlobalCooldown())
if !isOnCooldown {
return "", nil
}

return response, nil
}
}
Expand Down
80 changes: 80 additions & 0 deletions pkg/utils/cooldowns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package utils

import (
"sync"
"time"

"github.com/gempir/go-twitch-irc/v4"
)

var (
cooldowns = make(map[string]map[string]int64)
globalCD = make(map[string]int64)
cooldownMu sync.Mutex
)

func CooldownCanContinue(user twitch.User, cmdName string, cmdCooldown, globalCDTime int) bool {
/* if isUserPermitted(user, []string{"broadcaster", "moderator"}) {
return true
} */

if !globalCooldown(cmdName, globalCDTime) {
return false
}

if !addCooldown(user.Name, cmdName, cmdCooldown) {
return false
}

return true
}

func addCooldown(user string, commandName string, cdTime int) bool {
cooldownMu.Lock()
defer cooldownMu.Unlock()

if _, exists := cooldowns[commandName]; !exists {
cooldowns[commandName] = make(map[string]int64)
}

currentTime := time.Now().UnixMilli()
cooldownAmount := int64(cdTime) * 1000

if expireTime, exists := cooldowns[commandName][user]; exists {
if currentTime < expireTime {
return false
}
}

cooldowns[commandName][user] = currentTime + cooldownAmount
time.AfterFunc(time.Duration(cooldownAmount)*time.Millisecond, func() {
cooldownMu.Lock()
delete(cooldowns[commandName], user)
cooldownMu.Unlock()
})

return true
}

func globalCooldown(commandName string, cdTime int) bool {
cooldownMu.Lock()
defer cooldownMu.Unlock()

currentTime := time.Now().UnixMilli()
cooldownAmount := int64(cdTime) * 1000

if expireTime, exists := globalCD[commandName]; exists {
if currentTime < expireTime {
return false
}
}

globalCD[commandName] = currentTime + cooldownAmount
time.AfterFunc(time.Duration(cooldownAmount)*time.Millisecond, func() {
cooldownMu.Lock()
delete(globalCD, commandName)
cooldownMu.Unlock()
})

return true
}