go: Refactor invite.go

pull/10/head
Luther Wen Xu 2019-10-12 22:44:05 +07:00
parent 09073ac4ff
commit 4619ad1c23
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 68 additions and 34 deletions

@ -49,7 +49,29 @@ const VTLSingleUserPercentage = "百分比 Percentage"
const VTLGlobalTitle = "伺服器信誉分报告 Server Trust Score Report" const VTLGlobalTitle = "伺服器信誉分报告 Server Trust Score Report"
const VTLGlobalFieldFormat = "%.1f/%.1f (%.2f%%)" const VTLGlobalFieldFormat = "%.1f/%.1f (%.2f%%)"
const InviteUsage = "!invite <要制造的验证码>\n!invite <validation code to create>"
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 <validation code> <reason you should be accepted>" const ValidateUsage = "!validate <验证码> <为自己拉票的字>\n!validate <validation code> <reason you should be accepted>"
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 ValidateSuccess = "已向验证码制造者发送了请求。\nSent a request to use the validation code to its creator."
const ErrorValidateTitle = "验证错误Validation Error" const ErrorValidateTitle = "验证错误Validation Error"
const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one from its creator." const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one from its creator."

@ -58,8 +58,8 @@ func (e *embed) Build() *discordgo.MessageEmbed {
return e.MessageEmbed return e.MessageEmbed
} }
func (e *embed) Send(s *discordgo.Session, channelID string) { func (e *embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) {
s.ChannelMessageSendEmbed(channelID, e.Build()) return s.ChannelMessageSendEmbed(channelID, e.Build())
} }
func (e *embed) SendPM(s *discordgo.Session, userID string) { func (e *embed) SendPM(s *discordgo.Session, userID string) {

@ -2,6 +2,7 @@ package modules
import ( import (
"fmt" "fmt"
"strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -16,14 +17,12 @@ type invite struct {
Reason string 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) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) < 2 { if strings.ContainsRune(command[1], ' ') {
s.ChannelMessageSend(m.ChannelID, "指令的使用方法是`!invite <你所要设置的验证码>`.\nUsage: `!invite <validation code>`") initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
return Send(s, m.ChannelID)
}
if len(command) > 2 {
s.ChannelMessageSend(m.ChannelID, "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces.")
return return
} }
@ -32,34 +31,23 @@ var createInvite = enforceDM(memberFilter(true,
case db.ErrAlreadyExists: case db.ErrAlreadyExists:
owner, _ := db.GetInviteOwner(command[1]) owner, _ := db.GetInviteOwner(command[1])
if owner == m.Author.ID { if owner == m.Author.ID {
s.ChannelMessageSend( initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
m.ChannelID, Send(s, m.ChannelID)
"这个验证码归你所有,它仍然未被使用。请把它告诉你要邀请的人。\nThis validation code belongs to you. Please give it to the person you want to invite.",
)
return return
} }
s.ChannelMessageSend( initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
m.ChannelID, Send(s, m.ChannelID)
"这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one.",
)
case db.ErrInviteUsed: case db.ErrInviteUsed:
s.ChannelMessageSend( initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
m.ChannelID, Send(s, m.ChannelID)
"这个验证码已被使用。请使用别的验证码。\nThis validation code has already been used. Please try another one.",
)
case nil: case nil:
s.ChannelMessageSend( initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
m.ChannelID, Send(s, m.ChannelID)
"这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite.",
)
default: default:
s.ChannelMessageSend( auditErrorPM(s, m.Author.ID, err)
m.ChannelID,
"不明错误已发生,请把这个错误信息发在群里。\nAn unknown error has occurred. Please pass this error message on.\n"+err.Error(),
)
} }
}, },
)) )))
var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount( var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
config.ValidateUsage, 3, config.ValidateUsage, 3,
@ -105,7 +93,13 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
SendPM(s, m.ChannelID) SendPM(s, m.ChannelID)
return 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 { if err != nil {
auditErrorPM(s, m.ChannelID, err) auditErrorPM(s, m.ChannelID, err)
return return
@ -114,8 +108,8 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
User: m.Author.ID, User: m.Author.ID,
Reason: command[2], Reason: command[2],
} }
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck) s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
initNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour). initNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
SendPM(s, m.ChannelID) SendPM(s, m.ChannelID)
}, },
@ -134,14 +128,32 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
switch r.Emoji.Name { switch r.Emoji.Name {
case emojiX: 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 return
case emojiCheck: case emojiCheck:
default: default:
return 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...") msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
if err != nil { if err != nil {