TerraOceanPlugin/GoBot/command_vote.go

152 lines
4.5 KiB
Go

package main
import (
"fmt"
2019-10-10 12:35:53 +07:00
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
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-10 12:35:53 +07:00
const voteChannel = "627164246056239104"
2019-10-10 13:59:00 +07:00
const announceCustomChannel = "627165467269922864"
2019-10-10 12:35:53 +07:00
const (
emojiOne = "1⃣"
emojiTwo = "2⃣"
emojiThree = "3⃣"
emojiFour = "4⃣"
emojiFive = "5⃣"
emojiCheck = "✅"
emojiX = "❌"
)
func checkForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
2019-10-10 12:35:53 +07:00
if r.UserID == s.State.User.ID {
return
}
voteID, err := getVoteFromMessageID(r.MessageID)
2019-10-10 12:35:53 +07:00
if err == errNotFound {
return
}
if err != nil {
2019-10-10 12:35:53 +07:00
auditLog(s, "Error while checking for vote: "+err.Error())
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:
value = 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:
value = nuclearOptionVote
default:
2019-10-10 12:35:53 +07:00
auditLog(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
return
}
err = updateVote(voteID, r.UserID, value)
if err == errForceRejectionVoteReuse {
sendPrivateMessage(s, r.UserID, "您在这个月内已使用过:x:。请选择其他选项。\nYou have used :x: this month. Please choose another option.")
return
}
if err == errVoteIsOver {
sendPrivateMessage(s, r.UserID, "这个投票已结束。你投的票没有被记录。\nThe vote is over so your vote is not recorded.")
return
}
var voteName string
if err == nil {
voteName, err = getVoteName(voteID)
}
if err != nil {
sendPrivateMessage(s, r.UserID, "一个错误已发生,请重新尝试。\nAn error has occurred while voting. Please try again.")
auditLog(s,
2019-10-10 12:35:53 +07:00
fmt.Sprintf("Error occurred while processing vote for <@%s>.\n%v\nError: %s", r.UserID, *r, err.Error()),
)
return
}
sendPrivateMessage(s, r.UserID, "您投票已成功。\nYou have voted successfully.\n名字Vote Name: "+voteName+"\n目前投的票 :"+r.Emoji.Name+":")
2019-10-10 13:59:00 +07:00
checkForVoteResult(s, voteID)
}
2019-10-10 12:35:53 +07:00
func voteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageDelete(m.ChannelID, m.ID)
args := strings.SplitN(m.Content, " ", 2)
if len(args) == 1 {
sendPrivateMessage(s, m.Author.ID, "请提供更多资料。\nPlease provide more information.")
return
}
2019-10-11 14:35:44 +07:00
if len(args[1]) > 500 {
sendPrivateMessage(s, m.Author.ID, "你投票的内容过长了。请使用少过500个字符。\nYour vote is too long. Please use a maximum of 500 characters.")
return
}
2019-10-10 12:35:53 +07:00
switch args[0] {
case "custom":
msg, err := s.ChannelMessageSend(voteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
if err != nil {
sendPrivateMessage(s, m.Author.ID, "创造投票失败。Failed to create vote.")
auditLog(s,
fmt.Sprintf("Error occurred while creating vote for <@%s>.\n%v\nError: %s", m.Author.ID, *m, err.Error()),
)
return
}
id, err := createCustomVote(msg.ID, args[1])
if err != nil {
sendPrivateMessage(s, m.Author.ID, "创造投票失败。Failed to create vote.")
auditLog(s,
fmt.Sprintf("Error occurred while creating vote for <@%s>.\n%v\nError: %s", m.Author.ID, *m, err.Error()),
)
return
}
auditLog(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
s.ChannelMessageEdit(voteChannel, msg.ID, "")
2019-10-10 13:59:00 +07:00
s.ChannelMessageEditEmbed(voteChannel, msg.ID, createCustomEmbed(id, args[1]))
2019-10-10 12:35:53 +07:00
s.MessageReactionAdd(voteChannel, msg.ID, emojiOne)
s.MessageReactionAdd(voteChannel, msg.ID, emojiTwo)
s.MessageReactionAdd(voteChannel, msg.ID, emojiThree)
s.MessageReactionAdd(voteChannel, msg.ID, emojiFour)
s.MessageReactionAdd(voteChannel, msg.ID, emojiFive)
default:
sendPrivateMessage(s, m.Author.ID, "未知投票种类:"+args[0]+"\nUnknown vote type: "+args[0])
}
}
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) {
s.ChannelMessageSendEmbed(announceCustomChannel, showVoteStatus(createCustomEmbed(id, name), isPositive))
}