github.com/starshine-sys/bcr@v0.21.0/cooldown.go (about) 1 package bcr 2 3 import ( 4 "time" 5 6 "github.com/ReneKroon/ttlcache/v2" 7 "github.com/diamondburned/arikawa/v3/discord" 8 ) 9 10 // CooldownCache holds cooldowns for commands 11 type CooldownCache struct { 12 c *ttlcache.Cache 13 } 14 15 func newCooldownCache() *CooldownCache { 16 cache := ttlcache.NewCache() 17 cache.SkipTTLExtensionOnHit(true) 18 19 return &CooldownCache{c: cache} 20 } 21 22 // Set sets a cooldown for a command 23 func (c *CooldownCache) Set(cmdName string, userID discord.UserID, channelID discord.ChannelID, cooldown time.Duration) { 24 // if the command's cooldown is 0, return 25 if cooldown == 0 { 26 return 27 } 28 29 c.c.SetWithTTL(cmdName+userID.String()+channelID.String(), true, cooldown) 30 return 31 } 32 33 // Get returns true if the command is on cooldown 34 func (c *CooldownCache) Get(cmdName string, userID discord.UserID, channelID discord.ChannelID) bool { 35 if _, e := c.c.Get(cmdName + userID.String() + channelID.String()); e == nil { 36 return true 37 } 38 39 return false 40 }