TerraOceanPlugin/GoBot/vote.go

148 lines
4.3 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"fmt"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
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,
},
}
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 := getVoteFromMessageID(r.MessageID)
if err == 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 = forceRejectionVote
case emojiOne:
value = 1
case emojiTwo:
value = 2
case emojiThree:
value = 3
case emojiFour:
value = 4
case emojiFive:
value = 5
case emojiCheck:
value = nuclearOptionVote
default:
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,
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+":")
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
}
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, "")
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))
}