|
|
package main
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
|
"TerraOceanBot/db"
|
|
|
)
|
|
|
|
|
|
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 voteChannel = "627164246056239104"
|
|
|
const announceCustomChannel = "627165467269922864"
|
|
|
|
|
|
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 {
|
|
|
auditLog(s, "Error while checking for vote: "+err.Error())
|
|
|
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:
|
|
|
auditLog(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
err = db.UpdateVote(voteID, r.UserID, value)
|
|
|
|
|
|
if err == db.ErrForceRejectionVoteReuse {
|
|
|
sendPrivateMessage(s, r.UserID, "您在这个月内已使用过:x:。请选择其他选项。\nYou have used :x: this month. Please choose another option.")
|
|
|
return
|
|
|
}
|
|
|
if err == db.ErrVoteIsOver {
|
|
|
sendPrivateMessage(s, r.UserID, "这个投票已结束。你投的票没有被记录。\nThe vote is over so your vote is not recorded.")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var voteName string
|
|
|
if err == nil {
|
|
|
voteName, err = db.GetVoteName(voteID)
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
sendPrivateMessage(s, r.UserID, "一个错误已发生,请重新尝试。\nAn error has occurred while voting. Please try again.")
|
|
|
auditLog(s,
|
|
|
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+":")
|
|
|
auditLog(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
|
|
|
|
|
|
checkForVoteResult(s, voteID)
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
if len(args[1]) > 500 {
|
|
|
sendPrivateMessage(s, m.Author.ID, "你投票的内容过长了。请使用少过500个字符。\nYour vote is too long. Please use a maximum of 500 characters.")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
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 := db.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, "")
|
|
|
s.ChannelMessageEditEmbed(voteChannel, msg.ID, createCustomEmbed(id, args[1]))
|
|
|
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])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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))
|
|
|
}
|