go: Turn vote suggestion into a command

master
Luther Wen Xu 2019-10-13 21:49:58 +07:00
parent 88087e4c81
commit 1ea546edb4
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
7 changed files with 54 additions and 61 deletions

@ -6,5 +6,4 @@ const AuditChannel = "631789849929711627"
const VoteChannel = "627164246056239104"
const AnnounceCustomChannel = "627165467269922864"
const AnnounceInviteChannel = "627165467269922864"
const VoteSuggestionChannel = "627164561644191744"
const VoiceChannel = "627165587759693827"

@ -26,7 +26,6 @@ const VoteSuccessfulVoteID = "投票编号Vote ID"
const VoteSuccessfulVoteName = "投票名称Vote Name"
const VoteSuccessfulDetectedVote = "所投的票Your Vote"
const VoteSuggestionNote = "\n备注这个系统即将被一个指令代替。\nNote: This system is going to be replaced by a command soon."
const VoteSuggestionErrorTitle = "开始新的投票错误Vote Suggestion Error"
const ErrorVoteSuggestionNotEnoughInfo = "请提供更多资料。\nPlease provide more information."
const ErrorVoteSuggestionTooLong = "你投票的内容过长了。请使用少过500个字符。\nYour vote is too long. Please use a maximum of 500 characters."
@ -126,3 +125,8 @@ const ChangeTrustSuccessDescription = "已改变你给这名玩家的分数已
const ErrorChangeTrustTitle = "调整信誉分错误 Change Trust Error"
const ErrorChangeTrustTargetNotFound = "无法寻找到改变目标。\nCannot find the target user."
const ErrorChangeTrustRecentlyChanged = "你在一个月内有设定过该名玩家的分数。你这次的更动没有被记录。\nYou have changed your score for this player within the last month. Your change was not recorded."
const VoteSuggestionUsage = "!votesuggest <种类custom> <内容>\n!votesuggest <type: custom> <content>"
const VoteSuggestionUpcomingTitle = "准备中…… Preparing..."
const VoteSuggestionUpcomingDescription = "正在准备新的一个投票……\nPreparing for the next vote..."
const VoteSuggestionUpcomingColour = 0x000000

@ -17,7 +17,6 @@ func StartBot(token string, kill chan bool) {
}
dg.AddHandler(ProcessCommand)
dg.AddHandler(ProcessVote)
dg.AddHandler(modules.CheckForVote)
dg.AddHandler(modules.CheckForTrustUpdate)
dg.AddHandler(modules.CheckForInvite)

@ -6,7 +6,6 @@ import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/modules"
)
@ -24,13 +23,6 @@ func ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}
func ProcessVote(s *discordgo.Session, m *discordgo.MessageCreate) {
//TODO Factorize this code into a command
if m.ChannelID == config.VoteSuggestionChannel {
modules.VoteSuggestion(s, m)
}
}
func ProcessVoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
backend.VoiceStateUpdate(s, vs.VoiceState)
}

@ -37,4 +37,8 @@ var Commands = []Command{
Name: "!setmcusername",
Handler: updateMinecraftUsername,
},
Command{
Name: "!votesuggest",
Handler: voteSuggestion,
},
}

@ -16,6 +16,7 @@ func createCustomEmbed(id int, name string) *message.Embed {
}
func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) {
createCustomEmbed(id, name).UpdateVoteStatus(isPositive).
createCustomEmbed(id, name).
UpdateVoteStatus(isPositive).
Send(s, config.AnnounceCustomChannel)
}

@ -3,7 +3,6 @@ package modules
import (
"fmt"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
@ -92,54 +91,49 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
checkForVoteResult(s, voteID)
}
//TODO Replace with a command.
func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageDelete(m.ChannelID, m.ID)
args := strings.SplitN(m.Content, " ", 2)
if len(args) == 1 {
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionNotEnoughInfo+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
return
}
if len(args[1]) > 500 {
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionTooLong+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
return
}
switch args[0] {
case "custom":
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
if err != nil {
message.AuditErrorPM(s, m.Author.ID, err)
var voteSuggestion = enforceDM(enforceArgumentCount(
config.VoteSuggestionUsage, 3,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command[1]) > 500 {
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionTooLong,
config.ErrorColour,
).SendPM(s, m.Author.ID)
return
}
id, err := db.CreateCustomVote(msg.ID, args[1])
if err != nil {
message.AuditErrorPM(s, m.Author.ID, err)
return
switch command[0] {
case "custom":
msg, err := message.InitNewEmbed(
config.VoteSuggestionUpcomingTitle,
config.VoteSuggestionUpcomingDescription,
config.VoteSuggestionUpcomingColour,
).Send(s, config.VoteChannel)
if err != nil {
message.AuditErrorPM(s, m.Author.ID, err)
return
}
id, err := db.CreateCustomVote(msg.ID, command[1])
if err != nil {
message.AuditErrorPM(s, m.Author.ID, err)
return
}
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
createCustomEmbed(id, command[1]).Edit(s, config.VoteChannel, msg.ID)
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)
server.VoteNotification()
default:
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, command[1]),
config.ErrorColour,
).SendPM(s, m.Author.ID)
}
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
createCustomEmbed(id, args[1]).Edit(s, config.VoteChannel, msg.ID)
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)
server.VoteNotification()
default:
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, args[1])+config.VoteSuggestionNote,
config.ErrorColour,
).SendPM(s, m.Author.ID)
}
}
},
))