himbot/lib/helpers.go

85 lines
2 KiB
Go
Raw Normal View History

2023-12-27 17:56:21 -07:00
package lib
import (
2023-12-29 01:31:36 -07:00
"os"
"strings"
2023-12-27 17:56:21 -07:00
"github.com/diamondburned/arikawa/v3/discord"
)
// Userish is an interface that captures the common methods you may want to call
// on either a discord.Member or discord.User, including a display name.
type Userish interface {
ID() discord.UserID
Username() string
DisplayName() string // The added method for getting the display name
}
// memberUser adapts a discord.Member to the Userish interface.
type memberUser struct {
*discord.Member
}
func (mu memberUser) ID() discord.UserID {
return mu.User.ID
}
func (mu memberUser) Username() string {
return mu.User.Username
}
func (mu memberUser) DisplayName() string {
// If Nick is set, return it as the display name, otherwise return Username
if mu.Member.Nick != "" {
return mu.Member.Nick
}
return mu.User.Username
}
// directUser adapts a discord.User to the Userish interface.
type directUser struct {
*discord.User
}
func (du directUser) ID() discord.UserID {
return du.User.ID
}
func (du directUser) Username() string {
return du.User.Username
}
func (du directUser) DisplayName() string {
// For a direct user, the display name is just the username since no nickname is available.
return du.User.Username
}
// GetUserObject takes an interaction event and returns a Userish, which may be
// either a discord.Member or a discord.User, but exposes it through a consistent interface.
func GetUserObject(event discord.InteractionEvent) Userish {
if event.Member != nil {
return memberUser{event.Member}
} else {
return directUser{event.User}
}
}
2023-12-29 01:31:36 -07:00
func CooldownHandler(event discord.InteractionEvent) bool {
user := GetUserObject(event)
allowList := strings.Split(os.Getenv("COOLDOWN_ALLOW_LIST"), ",")
// Check if the user ID is in the allowList
for _, id := range allowList {
if id == user.ID().String() {
return true
}
}
cachedVal := GetCache(user.ID().String() + ":" + "hdpic")
if cachedVal != "nil" {
return false
}
SetCache(user.ID().String()+":"+"hdpic", user.ID().String()+":"+"hdpic", 10)
return true
}