TerraOceanPlugin/GoBot/discord/modules/vote.go

167 lines
4.4 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
import (
"fmt"
2019-10-10 12:35:53 +07:00
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
2019-10-12 08:07:10 +07:00
"TerraOceanBot/db"
2019-10-12 09:45:43 +07:00
"TerraOceanBot/discord/config"
)
2019-10-10 13:59:00 +07:00
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,
},
2019-10-12 06:40:46 +07:00
"invite": voteType{
EmbedBuilder: createInviteEmbed,
ResultHandler: handleInviteResult,
},
2019-10-10 13:59:00 +07:00
}
2019-10-10 12:35:53 +07:00
const (
emojiOne = "1⃣"
emojiTwo = "2⃣"
emojiThree = "3⃣"
emojiFour = "4⃣"
emojiFive = "5⃣"
emojiCheck = "✅"
emojiX = "❌"
)
2019-10-12 09:45:43 +07:00
func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
2019-10-10 12:35:53 +07:00
if r.UserID == s.State.User.ID {
return
}
2019-10-12 08:07:10 +07:00
voteID, err := db.GetVoteFromMessageID(r.MessageID)
if err == db.ErrNotFound {
2019-10-10 12:35:53 +07:00
return
}
if err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, r.ChannelID, err)
return
}
2019-10-10 12:35:53 +07:00
s.MessageReactionRemove(r.ChannelID, r.MessageID, r.Emoji.Name, r.UserID)
var value int
switch r.Emoji.Name {
2019-10-10 12:35:53 +07:00
case emojiX:
2019-10-12 08:07:10 +07:00
value = db.ForceRejectionVote
2019-10-10 12:35:53 +07:00
case emojiOne:
value = 1
2019-10-10 12:35:53 +07:00
case emojiTwo:
value = 2
2019-10-10 12:35:53 +07:00
case emojiThree:
value = 3
2019-10-10 12:35:53 +07:00
case emojiFour:
value = 4
2019-10-10 12:35:53 +07:00
case emojiFive:
value = 5
2019-10-10 12:35:53 +07:00
case emojiCheck:
2019-10-12 08:07:10 +07:00
value = db.NuclearOptionVote
default:
2019-10-12 13:49:41 +07:00
auditInfo(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
return
}
2019-10-12 08:07:10 +07:00
err = db.UpdateVote(voteID, r.UserID, value)
2019-10-12 08:07:10 +07:00
if err == db.ErrForceRejectionVoteReuse {
2019-10-12 13:49:41 +07:00
initNewEmbed(config.VoteErrorTitle, config.ErrorForceRejectionReuse, config.ErrorColour).
SendPM(s, r.UserID)
return
}
2019-10-12 08:07:10 +07:00
if err == db.ErrVoteIsOver {
2019-10-12 13:49:41 +07:00
initNewEmbed(config.VoteErrorTitle, config.ErrorVoteHasEnded, config.ErrorColour).
SendPM(s, r.UserID)
return
}
var voteName string
if err == nil {
2019-10-12 08:07:10 +07:00
voteName, err = db.GetVoteName(voteID)
}
if err != nil {
2019-10-12 13:49:41 +07:00
auditErrorPM(s, r.UserID, err)
return
}
2019-10-12 13:49:41 +07:00
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))
2019-10-10 13:59:00 +07:00
checkForVoteResult(s, voteID)
}
2019-10-10 12:35:53 +07:00
2019-10-12 13:49:41 +07:00
//TODO Replace with a command.
2019-10-12 09:45:43 +07:00
func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
2019-10-10 12:35:53 +07:00
s.ChannelMessageDelete(m.ChannelID, m.ID)
args := strings.SplitN(m.Content, " ", 2)
if len(args) == 1 {
2019-10-12 13:49:41 +07:00
initNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionNotEnoughInfo+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
2019-10-10 12:35:53 +07:00
return
}
2019-10-11 14:35:44 +07:00
if len(args[1]) > 500 {
2019-10-12 13:49:41 +07:00
initNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionTooLong+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
2019-10-11 14:35:44 +07:00
return
}
2019-10-10 12:35:53 +07:00
switch args[0] {
case "custom":
2019-10-12 09:45:43 +07:00
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
2019-10-10 12:35:53 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditErrorPM(s, m.Author.ID, err)
2019-10-10 12:35:53 +07:00
return
}
2019-10-12 08:07:10 +07:00
id, err := db.CreateCustomVote(msg.ID, args[1])
2019-10-10 12:35:53 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditErrorPM(s, m.Author.ID, err)
2019-10-10 12:35:53 +07:00
return
}
2019-10-12 13:49:41 +07:00
auditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
2019-10-12 09:45:43 +07:00
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)
2019-10-10 12:35:53 +07:00
default:
2019-10-12 13:49:41 +07:00
initNewEmbed(
config.VoteSuggestionErrorTitle,
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, args[1])+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
2019-10-10 12:35:53 +07:00
}
}
2019-10-10 13:59:00 +07:00
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) {
2019-10-12 09:45:43 +07:00
s.ChannelMessageSendEmbed(config.AnnounceCustomChannel, showVoteStatus(createCustomEmbed(id, name), isPositive))
2019-10-10 13:59:00 +07:00
}