go: Implement vote creation

master
Luther Wen Xu 2019-10-10 20:35:53 +07:00
parent 056573fc26
commit 907b6d889b
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 102 additions and 12 deletions

@ -23,7 +23,7 @@ func init() {
if err != nil {
panic(err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS vote (id INTEGER PRIMARY KEY, messageId TEXT, name TEXT, finished BOOLEAN, UNIQUE(id))")
_, err = db.Exec("CREATE TABLE IF NOT EXISTS vote (id INTEGER PRIMARY KEY, messageId TEXT, name TEXT, type TEXT, finished BOOLEAN, UNIQUE(id))")
if err != nil {
panic(err)
}

@ -13,6 +13,15 @@ const (
var errForceRejectionVoteReuse = errors.New("db: the user has used force rejection vote in the last month")
var errVoteIsOver = errors.New("db: the vote cannot be changed once it's over")
func createCustomVote(messageID, text string) (int, error) {
result, err := db.Exec("INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)", messageID, text, "custom", false)
if err != nil {
return 0, err
}
lastID, err := result.LastInsertId()
return int(lastID), nil
}
func getVoteName(id int) (string, error) {
rows, err := db.Query("SELECT name FROM vote WHERE id=?", id)
if err != nil {

@ -11,6 +11,8 @@ import (
"github.com/bwmarrin/discordgo"
)
const voteSuggestionChannel = "627164561644191744"
func main() {
token, _ := ioutil.ReadFile("token.txt")
dg, err := discordgo.New("Bot " + strings.TrimSpace(string(token)))
@ -36,6 +38,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
if m.ChannelID == voteSuggestionChannel {
voteSuggestion(s, m)
return
}
command := strings.Split(m.Content, " ")
switch command[0] {
case "!sendas":

@ -1,6 +1,10 @@
package main
import "github.com/bwmarrin/discordgo"
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
const guildID = "626424729234046987"
const memberRoleID = "626434632614805526"
@ -51,5 +55,10 @@ func sendPrivateMessage(s *discordgo.Session, recipient, message string) error {
}
func auditLog(s *discordgo.Session, message string) {
s.ChannelMessageSend(auditChannel, message)
_, err := s.ChannelMessageSend(auditChannel, message)
if err != nil {
fmt.Println("==Audit Log==")
fmt.Println(message)
fmt.Println(err)
}
}

@ -2,35 +2,57 @@ package main
import (
"fmt"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
const voteChannel = "627164246056239104"
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.ID, r.UserID)
s.MessageReactionRemove(r.ChannelID, r.MessageID, r.Emoji.Name, r.UserID)
var value int
switch r.Emoji.Name {
case "x":
case emojiX:
value = forceRejectionVote
case "one":
case emojiOne:
value = 1
case "two":
case emojiTwo:
value = 2
case "three":
case emojiThree:
value = 3
case "four":
case emojiFour:
value = 4
case "five":
case emojiFive:
value = 5
case "white_check_mark":
case emojiCheck:
value = nuclearOptionVote
default:
auditLog(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
return
}
@ -53,10 +75,53 @@ func checkForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
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()),
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+":")
}
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,
newEmbed().SetColour(0x00FFFF).SetTitle("自定义投票Custom Vote").AddField("ID", strconv.Itoa(id)).AddField("内容Content", args[1]).Build(),
)
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])
}
}