forked from chanbakjsd/TerraOceanPlugin
go: Unify react implementation
parent
9f2f3e8e3b
commit
e180dec331
@ -0,0 +1,53 @@
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue