From e9c303c1da0b779b0923d24f4a44f1c510338055 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Tue, 15 Oct 2019 18:36:29 +0800 Subject: [PATCH] go: Prettify vote names before sending them --- GoBot/discord/config/translate.go | 1 + GoBot/discord/modules/vote.go | 4 +++- GoBot/discord/modules/vote_custom.go | 4 ++++ GoBot/discord/modules/vote_invite.go | 5 +++++ GoBot/discord/modules/vote_listener.go | 7 +++++-- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/GoBot/discord/config/translate.go b/GoBot/discord/config/translate.go index b89c2c6..2d93f43 100644 --- a/GoBot/discord/config/translate.go +++ b/GoBot/discord/config/translate.go @@ -46,6 +46,7 @@ const EntryVoteColour = 0x808000 const EntryVoteVoteID = "投票编号 Vote ID" const EntryVoteApplicant = "申请者 Applicant" const EntryVoteReason = "加入原因 Provided Reason for Entry" +const EntryVoteName = "%s的加入 %s's Entry Application" const VotePassTitle = "投票通过 Vote Passed" const VotePassColour = 0x00F000 diff --git a/GoBot/discord/modules/vote.go b/GoBot/discord/modules/vote.go index f792718..432dac6 100644 --- a/GoBot/discord/modules/vote.go +++ b/GoBot/discord/modules/vote.go @@ -7,18 +7,20 @@ import ( ) type voteType struct { - //TODO Add FormatName(name string) to clean up vote name EmbedBuilder func(id int, name string) *message.Embed + FormatName func(internalName string) string ResultHandler func(s *discordgo.Session, id int, name string, isPositive bool) } var voteTypes = map[string]voteType{ "custom": voteType{ EmbedBuilder: createCustomEmbed, + FormatName: formatCustomName, ResultHandler: announceCustomResult, }, "invite": voteType{ EmbedBuilder: createInviteEmbed, + FormatName: formatInviteName, ResultHandler: handleInviteResult, }, } diff --git a/GoBot/discord/modules/vote_custom.go b/GoBot/discord/modules/vote_custom.go index a2d55ee..74c2bd2 100644 --- a/GoBot/discord/modules/vote_custom.go +++ b/GoBot/discord/modules/vote_custom.go @@ -15,6 +15,10 @@ func createCustomEmbed(id int, name string) *message.Embed { AddField(config.CustomVoteContent, name) } +func formatCustomName(internalName string) string { + return internalName +} + func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) { createCustomEmbed(id, name). UpdateVoteStatus(isPositive). diff --git a/GoBot/discord/modules/vote_invite.go b/GoBot/discord/modules/vote_invite.go index de29fee..ff0462b 100644 --- a/GoBot/discord/modules/vote_invite.go +++ b/GoBot/discord/modules/vote_invite.go @@ -21,6 +21,11 @@ func createInviteEmbed(id int, name string) *message.Embed { AddField(config.EntryVoteReason, list[1]) } +func formatInviteName(internalName string) string { + list := strings.SplitN(internalName, ":", 2) + return fmt.Sprintf(config.EntryVoteName, list[0], list[0]) +} + func handleInviteResult(s *discordgo.Session, id int, name string, isPositive bool) { list := strings.SplitN(name, ":", 2) if !isPositive { diff --git a/GoBot/discord/modules/vote_listener.go b/GoBot/discord/modules/vote_listener.go index 5daccbf..b4aaf12 100644 --- a/GoBot/discord/modules/vote_listener.go +++ b/GoBot/discord/modules/vote_listener.go @@ -71,10 +71,13 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) { return } - var voteName string + var voteName, voteType string if err == nil { voteName, err = db.GetVoteName(voteID) } + if err == nil { + voteType, err = db.GetVoteType(voteID) + } if err != nil { message.AuditErrorPM(s, r.UserID, err) @@ -83,7 +86,7 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) { message.InitNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour). AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)). - AddField(config.VoteSuccessfulVoteName, voteName). + AddField(config.VoteSuccessfulVoteName, voteTypes[voteType].FormatName(voteName)). AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name). SendPM(s, r.UserID) message.AuditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))