go: Prettify vote names before sending them

Luther Wen Xu 2019-10-15 18:36:29 +07:00
parent 42ef07a1b4
commit e9c303c1da
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 18 additions and 3 deletions

@ -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

@ -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,
},
}

@ -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).

@ -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 {

@ -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))