From 1ea546edb40bd67f3056e9f17fb13812f90f9af2 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Sun, 13 Oct 2019 21:49:58 +0800 Subject: [PATCH] go: Turn vote suggestion into a command --- GoBot/discord/config/discordID.go | 1 - GoBot/discord/config/translate.go | 6 +- GoBot/discord/discord.go | 1 - GoBot/discord/listener.go | 8 --- GoBot/discord/modules/commands.go | 4 ++ GoBot/discord/modules/vote_custom.go | 3 +- GoBot/discord/modules/vote_listener.go | 92 ++++++++++++-------------- 7 files changed, 54 insertions(+), 61 deletions(-) diff --git a/GoBot/discord/config/discordID.go b/GoBot/discord/config/discordID.go index 9e81745..4856acb 100644 --- a/GoBot/discord/config/discordID.go +++ b/GoBot/discord/config/discordID.go @@ -6,5 +6,4 @@ const AuditChannel = "631789849929711627" const VoteChannel = "627164246056239104" const AnnounceCustomChannel = "627165467269922864" const AnnounceInviteChannel = "627165467269922864" -const VoteSuggestionChannel = "627164561644191744" const VoiceChannel = "627165587759693827" diff --git a/GoBot/discord/config/translate.go b/GoBot/discord/config/translate.go index a6c04d7..2eec64f 100644 --- a/GoBot/discord/config/translate.go +++ b/GoBot/discord/config/translate.go @@ -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 " +const VoteSuggestionUpcomingTitle = "准备中…… Preparing..." +const VoteSuggestionUpcomingDescription = "正在准备新的一个投票……\nPreparing for the next vote..." +const VoteSuggestionUpcomingColour = 0x000000 diff --git a/GoBot/discord/discord.go b/GoBot/discord/discord.go index 09c06ef..ad0efea 100644 --- a/GoBot/discord/discord.go +++ b/GoBot/discord/discord.go @@ -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) diff --git a/GoBot/discord/listener.go b/GoBot/discord/listener.go index 006a2d1..84d7b6d 100644 --- a/GoBot/discord/listener.go +++ b/GoBot/discord/listener.go @@ -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) } diff --git a/GoBot/discord/modules/commands.go b/GoBot/discord/modules/commands.go index d7b8263..1188116 100644 --- a/GoBot/discord/modules/commands.go +++ b/GoBot/discord/modules/commands.go @@ -37,4 +37,8 @@ var Commands = []Command{ Name: "!setmcusername", Handler: updateMinecraftUsername, }, + Command{ + Name: "!votesuggest", + Handler: voteSuggestion, + }, } diff --git a/GoBot/discord/modules/vote_custom.go b/GoBot/discord/modules/vote_custom.go index 12d0ebc..a2d55ee 100644 --- a/GoBot/discord/modules/vote_custom.go +++ b/GoBot/discord/modules/vote_custom.go @@ -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) } diff --git a/GoBot/discord/modules/vote_listener.go b/GoBot/discord/modules/vote_listener.go index b171a58..5daccbf 100644 --- a/GoBot/discord/modules/vote_listener.go +++ b/GoBot/discord/modules/vote_listener.go @@ -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) - } -} + }, +))