go: Unify react implementation

pull/13/head
Luther Wen Xu 2019-10-14 18:01:25 +07:00
parent 9f2f3e8e3b
commit e180dec331
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 88 additions and 54 deletions

@ -17,10 +17,8 @@ func StartBot(token string, kill chan bool) {
} }
dg.AddHandler(ProcessCommand) dg.AddHandler(ProcessCommand)
dg.AddHandler(modules.CheckForReact)
dg.AddHandler(modules.CheckForVote) dg.AddHandler(modules.CheckForVote)
dg.AddHandler(modules.CheckForTrustUpdate)
dg.AddHandler(modules.CheckForInvite)
dg.AddHandler(modules.ConfirmMinecraftUsername)
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
panic(err) panic(err)

@ -12,8 +12,6 @@ import (
"TerraOceanBot/server" "TerraOceanBot/server"
) )
var pendingInviteConfirmation = make(map[string]invite)
type invite struct { type invite struct {
User string User string
Reason string Reason string
@ -106,10 +104,15 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
message.AuditErrorPM(s, m.ChannelID, err) message.AuditErrorPM(s, m.ChannelID, err)
return return
} }
pendingInviteConfirmation[msg.ID] = invite{ queueForReact(
User: m.Author.ID, msg.ID,
Reason: command[2], []string{emojiCheck, emojiX},
} invite{
User: m.Author.ID,
Reason: command[2],
},
inviteCallback,
)
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck) s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
s.MessageReactionAdd(channel.ID, msg.ID, emojiX) s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour). message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
@ -117,19 +120,9 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
}, },
))) )))
func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) { func inviteCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
if r.UserID == s.State.User.ID { invite := info.(invite)
return if r.Emoji.Name == emojiX {
}
invite, ok := pendingInviteConfirmation[r.MessageID]
if !ok {
return
}
delete(pendingInviteConfirmation, r.MessageID)
switch r.Emoji.Name {
case emojiX:
message.InitNewEmbed( message.InitNewEmbed(
config.ValidateConfirmationRejectCreatorTitle, config.ValidateConfirmationRejectCreatorTitle,
config.ValidateConfirmationRejectCreatorDescription, config.ValidateConfirmationRejectCreatorDescription,
@ -141,9 +134,6 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
config.ErrorColour, config.ErrorColour,
).SendPM(s, invite.User) ).SendPM(s, invite.User)
return return
case emojiCheck:
default:
return
} }
message.InitNewEmbed( message.InitNewEmbed(
@ -157,7 +147,11 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
config.ErrorColour, config.ErrorColour,
).SendPM(s, invite.User) ).SendPM(s, invite.User)
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...") msg, err := message.InitNewEmbed(
config.VoteSuggestionUpcomingTitle,
config.VoteSuggestionUpcomingDescription,
config.VoteSuggestionUpcomingColour,
).Send(s, config.VoteChannel)
if err != nil { if err != nil {
message.AuditErrorPM(s, r.UserID, err) message.AuditErrorPM(s, r.UserID, err)
return return

@ -10,8 +10,6 @@ import (
"TerraOceanBot/server" "TerraOceanBot/server"
) )
var minecraftConfirmReact = make(map[string]string)
var updateMinecraftUsername = memberFilter(true, enforceArgumentCount( var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
config.SetMCUsernameUsage, 2, config.SetMCUsernameUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
@ -39,23 +37,19 @@ var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
if err != nil { if err != nil {
message.AuditError(s, m.ChannelID, err) message.AuditError(s, m.ChannelID, err)
} }
minecraftConfirmReact[msg.ID] = command[1] queueForReact(
msg.ID,
[]string{emojiCheck, emojiX},
command[1],
confirmMinecraftUsername,
)
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX)
}, },
)) ))
func ConfirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd) { func confirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
if r.UserID == s.State.User.ID { newUsername := info.(string)
return
}
newUsername, ok := minecraftConfirmReact[r.MessageID]
if !ok {
return
}
delete(minecraftConfirmReact, r.MessageID)
switch r.Emoji.Name { switch r.Emoji.Name {
case emojiCheck: case emojiCheck:
@ -86,6 +80,7 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna
backend.UpdateVoiceChannelState(s) backend.UpdateVoiceChannelState(s)
if oldErr == nil { if oldErr == nil {
server.WhitelistRemove(oldUsername) server.WhitelistRemove(oldUsername)
server.DisconnectUser(oldUsername)
} }
message.InitNewEmbed( message.InitNewEmbed(

@ -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)
}

@ -9,8 +9,6 @@ import (
"TerraOceanBot/discord/message" "TerraOceanBot/discord/message"
) )
var trustMessage = make(map[string]string)
var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount( var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
config.ChangeTrustUsage, 2, config.ChangeTrustUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
@ -44,7 +42,12 @@ var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
return return
} }
trustMessage[msg.ID] = member.User.ID queueForReact(
msg.ID,
[]string{emojiOne, emojiTwo, emojiThree, emojiFour, emojiFive, emojiCheck},
member.User.ID,
trustCallback,
)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiOne) s.MessageReactionAdd(m.ChannelID, msg.ID, emojiOne)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiTwo) s.MessageReactionAdd(m.ChannelID, msg.ID, emojiTwo)
@ -55,17 +58,8 @@ var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
}, },
))) )))
func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd) { func trustCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
if r.UserID == s.State.User.ID { target := info.(string)
return
}
target, ok := trustMessage[r.MessageID]
if !ok {
return
}
delete(trustMessage, r.MessageID)
var value int var value int
switch r.Emoji.Name { switch r.Emoji.Name {