TerraOceanPlugin/GoBot/discord/modules/react.go

57 lines
1.1 KiB
Go

package modules
import (
"sync"
"github.com/bwmarrin/discordgo"
)
var reactLock sync.Mutex
var reactCallbacks = make(map[string]reactEntry)
type reactCallback func(s *discordgo.Session, react *discordgo.MessageReactionAdd, info interface{})
type reactEntry struct {
acceptedEmoji []string
callback reactCallback
info interface{}
}
func queueForReact(messageID string, acceptedEmoji []string, info interface{}, callback reactCallback) {
reactLock.Lock()
defer reactLock.Unlock()
reactCallbacks[messageID] = reactEntry{
acceptedEmoji: acceptedEmoji,
callback: callback,
info: info,
}
}
func CheckForReact(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
if r.UserID == s.State.User.ID {
return
}
entry, ok := reactCallbacks[r.MessageID]
if !ok {
return
}
reactLock.Lock()
foundEmoji := false
for _, v := range entry.acceptedEmoji {
if r.Emoji.Name == v {
foundEmoji = true
break
}
}
if !foundEmoji {
reactLock.Unlock()
return
}
delete(reactCallbacks, r.MessageID)
reactLock.Unlock()
entry.callback(s, r, entry.info)
}