forked from chanbakjsd/TerraOceanPlugin
54 lines
1.0 KiB
Go
54 lines
1.0 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) {
|
||
|
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)
|
||
|
}
|