go: Refactor vote*.go
parent
4619ad1c23
commit
38580f8910
@ -0,0 +1,20 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/discord/config"
|
||||
)
|
||||
|
||||
func createCustomEmbed(id int, name string) *embed {
|
||||
return initNewEmbed(config.CustomVoteTitle, "", config.CustomVoteColour).
|
||||
AddField(config.CustomVoteVoteID, strconv.Itoa(id)).
|
||||
AddField(config.CustomVoteContent, name)
|
||||
}
|
||||
|
||||
func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) {
|
||||
createCustomEmbed(id, name).UpdateVoteStatus(isPositive).
|
||||
Send(s, config.AnnounceCustomChannel)
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"TerraOceanBot/db"
|
||||
"TerraOceanBot/discord/config"
|
||||
)
|
||||
|
||||
const (
|
||||
emojiOne = "1⃣"
|
||||
emojiTwo = "2⃣"
|
||||
emojiThree = "3⃣"
|
||||
emojiFour = "4⃣"
|
||||
emojiFive = "5⃣"
|
||||
emojiCheck = "✅"
|
||||
emojiX = "❌"
|
||||
)
|
||||
|
||||
func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
||||
if r.UserID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
voteID, err := db.GetVoteFromMessageID(r.MessageID)
|
||||
if err == db.ErrNotFound {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
auditError(s, r.ChannelID, err)
|
||||
return
|
||||
}
|
||||
|
||||
s.MessageReactionRemove(r.ChannelID, r.MessageID, r.Emoji.Name, r.UserID)
|
||||
|
||||
var value int
|
||||
switch r.Emoji.Name {
|
||||
case emojiX:
|
||||
value = db.ForceRejectionVote
|
||||
case emojiOne:
|
||||
value = 1
|
||||
case emojiTwo:
|
||||
value = 2
|
||||
case emojiThree:
|
||||
value = 3
|
||||
case emojiFour:
|
||||
value = 4
|
||||
case emojiFive:
|
||||
value = 5
|
||||
case emojiCheck:
|
||||
value = db.NuclearOptionVote
|
||||
default:
|
||||
auditInfo(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
|
||||
return
|
||||
}
|
||||
|
||||
err = db.UpdateVote(voteID, r.UserID, value)
|
||||
|
||||
if err == db.ErrForceRejectionVoteReuse {
|
||||
initNewEmbed(config.VoteErrorTitle, config.ErrorForceRejectionReuse, config.ErrorColour).
|
||||
SendPM(s, r.UserID)
|
||||
return
|
||||
}
|
||||
if err == db.ErrVoteIsOver {
|
||||
initNewEmbed(config.VoteErrorTitle, config.ErrorVoteHasEnded, config.ErrorColour).
|
||||
SendPM(s, r.UserID)
|
||||
return
|
||||
}
|
||||
|
||||
var voteName string
|
||||
if err == nil {
|
||||
voteName, err = db.GetVoteName(voteID)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
auditErrorPM(s, r.UserID, err)
|
||||
return
|
||||
}
|
||||
|
||||
initNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour).
|
||||
AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)).
|
||||
AddField(config.VoteSuccessfulVoteName, voteName).
|
||||
AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name).
|
||||
SendPM(s, r.UserID)
|
||||
auditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
|
||||
|
||||
checkForVoteResult(s, voteID)
|
||||
}
|
||||
|
||||
//TODO Replace with a command.
|
||||
func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
s.ChannelMessageDelete(m.ChannelID, m.ID)
|
||||
|
||||
args := strings.SplitN(m.Content, " ", 2)
|
||||
if len(args) == 1 {
|
||||
initNewEmbed(
|
||||
config.VoteSuggestionErrorTitle,
|
||||
config.ErrorVoteSuggestionNotEnoughInfo+config.VoteSuggestionNote,
|
||||
config.ErrorColour,
|
||||
).SendPM(s, m.Author.ID)
|
||||
return
|
||||
}
|
||||
if len(args[1]) > 500 {
|
||||
initNewEmbed(
|
||||
config.VoteSuggestionErrorTitle,
|
||||
config.ErrorVoteSuggestionTooLong+config.VoteSuggestionNote,
|
||||
config.ErrorColour,
|
||||
).SendPM(s, m.Author.ID)
|
||||
return
|
||||
}
|
||||
|
||||
switch args[0] {
|
||||
case "custom":
|
||||
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
|
||||
if err != nil {
|
||||
auditErrorPM(s, m.Author.ID, err)
|
||||
return
|
||||
}
|
||||
id, err := db.CreateCustomVote(msg.ID, args[1])
|
||||
if err != nil {
|
||||
auditErrorPM(s, m.Author.ID, err)
|
||||
return
|
||||
}
|
||||
auditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
|
||||
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
|
||||
createCustomEmbed(id, args[1]).Edit(s, config.VoteChannel, msg.ID)
|
||||
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiOne)
|
||||
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiTwo)
|
||||
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiThree)
|
||||
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiFour)
|
||||
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiFive)
|
||||
default:
|
||||
initNewEmbed(
|
||||
config.VoteSuggestionErrorTitle,
|
||||
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, args[1])+config.VoteSuggestionNote,
|
||||
config.ErrorColour,
|
||||
).SendPM(s, m.Author.ID)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue