167 lines
4.4 KiB
Go
167 lines
4.4 KiB
Go
package modules
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"TerraOceanBot/db"
|
|
"TerraOceanBot/discord/config"
|
|
)
|
|
|
|
type voteType struct {
|
|
EmbedBuilder func(id int, name string) *discordgo.MessageEmbed
|
|
ResultHandler func(s *discordgo.Session, id int, name string, isPositive bool)
|
|
}
|
|
|
|
var voteTypes = map[string]voteType{
|
|
"custom": voteType{
|
|
EmbedBuilder: createCustomEmbed,
|
|
ResultHandler: announceCustomResult,
|
|
},
|
|
"invite": voteType{
|
|
EmbedBuilder: createInviteEmbed,
|
|
ResultHandler: handleInviteResult,
|
|
},
|
|
}
|
|
|
|
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, "")
|
|
s.ChannelMessageEditEmbed(config.VoteChannel, msg.ID, createCustomEmbed(id, args[1]))
|
|
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)
|
|
}
|
|
}
|
|
|
|
func createCustomEmbed(id int, name string) *discordgo.MessageEmbed {
|
|
return newEmbed().SetColour(0x00FFFF).SetTitle("自定义投票Custom Vote").AddField("ID", strconv.Itoa(id)).AddField("内容Content", name).Build()
|
|
}
|
|
|
|
func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) {
|
|
s.ChannelMessageSendEmbed(config.AnnounceCustomChannel, showVoteStatus(createCustomEmbed(id, name), isPositive))
|
|
}
|