From 4619ad1c23e546a37cf3039ee5573e8af4df6dd3 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Sat, 12 Oct 2019 22:44:05 +0800 Subject: [PATCH] go: Refactor invite.go --- GoBot/discord/config/translate.go | 22 +++++++++ GoBot/discord/modules/embed.go | 4 +- GoBot/discord/modules/invite.go | 76 ++++++++++++++++++------------- 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/GoBot/discord/config/translate.go b/GoBot/discord/config/translate.go index 84ead57..c6bccdf 100644 --- a/GoBot/discord/config/translate.go +++ b/GoBot/discord/config/translate.go @@ -49,7 +49,29 @@ const VTLSingleUserPercentage = "百分比 Percentage" const VTLGlobalTitle = "伺服器信誉分报告 Server Trust Score Report" const VTLGlobalFieldFormat = "%.1f/%.1f (%.2f%%)" +const InviteUsage = "!invite <要制造的验证码>\n!invite " +const InviteSuccessTitle = "制造验证码成功Create Validation Code Success" +const InviteSuccessNew = "这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite." +const InviteSuccessAlreadyOwn = "该验证码原本就已经属于你。请直接交给你要邀请的人吧。\nThe validation code already belongs to you. Please just pass it to the person you want to invite." +const ErrorInviteTitle = "制造验证码错误Create Validation Code Error" +const ErrorInviteCodeContainsSpace = "所制造的验证码不得有任何空格。\nThe validation code to create cannot contain any spaces." +const ErrorInviteOwnedByOthers = "这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one." +const ErrorInviteExistAndUsed = "这个验证码已被使用过。请使用别的验证码。\nThis validation code has already been used before. Please try another one." + const ValidateUsage = "!validate <验证码> <为自己拉票的字>\n!validate " +const ValidateConfirmationColour = 0xE0E000 +const ValidateConfirmationTitle = "验证码使用确认Validation Code Usage Confirmation" +const ValidateConfirmationDescription = "有人使用了你创造的验证码。请确认他是否是你要邀请的人。\nSomeone has used a validation code you created. Please confirm that he/she is the person you want to invite." +const ValidateConfirmationUser = "Discord帐号 // Discord Account" +const ValidateConfirmationInviteCode = "使用的验证码Validation Code Used" +const ValidateConfirmationRejectCreatorTitle = "已拒绝验证码使用 Validation Code Usage Denied" +const ValidateConfirmationRejectCreatorDescription = "你已成功拒绝该验证码的使用。为了避免该验证码被对方重复使用,该验证码已被无效化。\nYou have successfully denied the usage of the validation code. To prevent the validation code user from reusing this code, this validation code has been revoked." +const ValidateConfirmationRejectRejecteeTitle = "验证码使用已被拒绝 Validation Code Usage Denied" +const ValidateConfirmationRejectRejecteeDescription = "验证码制造者已拒绝你对该验证码的使用。验证码也已经被无效化了。\nThe validation code creator has prevented you from using the validation code. The code has also been revoked." +const ValidateConfirmationAcceptCreatorTitle = "已同意验证码死用 Validation Code Usage Approved" +const ValidateConfirmationAcceptCreatorDescription = "你已成功同意验证码的使用,主伺服器已开始了加入投票。该验证码也在这次使用后无效化了。\nYou have successfully approved the usage of the validation code and started a vote in the main server. With the one-time usage property of the code, it has been revoked." +const ValidateConfirmationAcceptAccepteeTitle = "验证码使用已被同意 Validation Code Usage Approved" +const ValidateConfirmationAcceptAccepteeDescription = "你验证码的使用已被同意,主伺服器中开始了对于你的加入的投票,请等候投票完毕。\nYour usage of validation code was approved and a vote about your entry request has been started in the main server. Please wait patiently for the vote to finish." const ValidateSuccess = "已向验证码制造者发送了请求。\nSent a request to use the validation code to its creator." const ErrorValidateTitle = "验证错误Validation Error" const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one from its creator." diff --git a/GoBot/discord/modules/embed.go b/GoBot/discord/modules/embed.go index 9fb84b8..4a52bbf 100644 --- a/GoBot/discord/modules/embed.go +++ b/GoBot/discord/modules/embed.go @@ -58,8 +58,8 @@ func (e *embed) Build() *discordgo.MessageEmbed { return e.MessageEmbed } -func (e *embed) Send(s *discordgo.Session, channelID string) { - s.ChannelMessageSendEmbed(channelID, e.Build()) +func (e *embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) { + return s.ChannelMessageSendEmbed(channelID, e.Build()) } func (e *embed) SendPM(s *discordgo.Session, userID string) { diff --git a/GoBot/discord/modules/invite.go b/GoBot/discord/modules/invite.go index 03e4348..0b96ebf 100644 --- a/GoBot/discord/modules/invite.go +++ b/GoBot/discord/modules/invite.go @@ -2,6 +2,7 @@ package modules import ( "fmt" + "strings" "github.com/bwmarrin/discordgo" @@ -16,14 +17,12 @@ type invite struct { Reason string } -var createInvite = enforceDM(memberFilter(true, +var createInvite = enforceDM(memberFilter(true, enforceArgumentCount( + config.InviteUsage, 2, func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { - if len(command) < 2 { - s.ChannelMessageSend(m.ChannelID, "指令的使用方法是`!invite <你所要设置的验证码>`.\nUsage: `!invite `") - return - } - if len(command) > 2 { - s.ChannelMessageSend(m.ChannelID, "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces.") + if strings.ContainsRune(command[1], ' ') { + initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour). + Send(s, m.ChannelID) return } @@ -32,34 +31,23 @@ var createInvite = enforceDM(memberFilter(true, case db.ErrAlreadyExists: owner, _ := db.GetInviteOwner(command[1]) if owner == m.Author.ID { - s.ChannelMessageSend( - m.ChannelID, - "这个验证码归你所有,它仍然未被使用。请把它告诉你要邀请的人。\nThis validation code belongs to you. Please give it to the person you want to invite.", - ) + initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour). + Send(s, m.ChannelID) return } - s.ChannelMessageSend( - m.ChannelID, - "这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one.", - ) + initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour). + Send(s, m.ChannelID) case db.ErrInviteUsed: - s.ChannelMessageSend( - m.ChannelID, - "这个验证码已被使用。请使用别的验证码。\nThis validation code has already been used. Please try another one.", - ) + initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour). + Send(s, m.ChannelID) case nil: - s.ChannelMessageSend( - m.ChannelID, - "这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite.", - ) + initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour). + Send(s, m.ChannelID) default: - s.ChannelMessageSend( - m.ChannelID, - "不明错误已发生,请把这个错误信息发在群里。\nAn unknown error has occurred. Please pass this error message on.\n"+err.Error(), - ) + auditErrorPM(s, m.Author.ID, err) } }, -)) +))) var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount( config.ValidateUsage, 3, @@ -105,7 +93,13 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount( SendPM(s, m.ChannelID) return } - msg, err := s.ChannelMessageSend(channel.ID, fmt.Sprintf("<@%s>正在尝试使用验证码%s。请问是否同意该使用?", m.Author.ID, command[1])) + msg, err := initNewEmbed( + config.ValidateConfirmationTitle, + config.ValidateConfirmationDescription, + config.ValidateConfirmationColour, + ).AddField(config.ValidateConfirmationUser, m.Author.Mention()). + AddField(config.ValidateConfirmationInviteCode, command[1]). + Send(s, channel.ID) if err != nil { auditErrorPM(s, m.ChannelID, err) return @@ -114,8 +108,8 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount( User: m.Author.ID, Reason: command[2], } - s.MessageReactionAdd(channel.ID, msg.ID, emojiX) s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck) + s.MessageReactionAdd(channel.ID, msg.ID, emojiX) initNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour). SendPM(s, m.ChannelID) }, @@ -134,14 +128,32 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) { switch r.Emoji.Name { case emojiX: - s.ChannelMessageSend(r.ChannelID, "已成功拒绝该验证码的使用。为了保护你,该验证码已被无效化。\nThe use of this validation code has been rejected. This validation code has been invalidated to protect you.") + initNewEmbed( + config.ValidateConfirmationRejectCreatorTitle, + config.ValidateConfirmationRejectCreatorDescription, + config.ErrorColour, + ).SendPM(s, r.UserID) + initNewEmbed( + config.ValidateConfirmationRejectRejecteeTitle, + config.ValidateConfirmationRejectRejecteeDescription, + config.ErrorColour, + ).SendPM(s, invite.User) return case emojiCheck: default: return } - s.ChannelMessageSend(r.ChannelID, "已同意验证码的使用。该验证码已被无效化。\nUse of validation code accepted. This validation code has been invalidated.") + initNewEmbed( + config.ValidateConfirmationAcceptCreatorTitle, + config.ValidateConfirmationAcceptCreatorDescription, + config.ErrorColour, + ).SendPM(s, r.UserID) + initNewEmbed( + config.ValidateConfirmationAcceptAccepteeTitle, + config.ValidateConfirmationAcceptAccepteeDescription, + config.ErrorColour, + ).SendPM(s, invite.User) msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...") if err != nil {