@ -1,6 +1,18 @@
package main
import "github.com/bwmarrin/discordgo"
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
var pendingInviteConfirmation = make ( map [ string ] invite )
type invite struct {
User string
Reason string
}
func createInvite ( s * discordgo . Session , m * discordgo . MessageCreate , command [ ] string ) {
if ! enforceDM ( s , m ) {
@ -50,3 +62,119 @@ func createInvite(s *discordgo.Session, m *discordgo.MessageCreate, command []st
)
}
}
func checkUseInvite ( s * discordgo . Session , m * discordgo . MessageCreate , command [ ] string ) {
if ! enforceDM ( s , m ) {
return
}
if ! nonMembersOnly ( s , m ) {
return
}
s . ChannelMessageDelete ( m . ChannelID , m . ID )
if len ( command ) < 3 {
sendPrivateMessage ( s , m . Author . ID , "指令的使用方法是`!validate <验证码> <原因>`.\nUsage: `!validate <validation code> <reason>`" )
return
}
messageSplit := strings . SplitN ( m . Content , " " , 3 )
inviter , err := getInviteOwner ( messageSplit [ 1 ] )
if err == errInviteUsed {
sendPrivateMessage ( s , m . Author . ID , "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one." )
return
}
if err == errNotFound {
sendPrivateMessage ( s , m . Author . ID , "该验证码不存在。请使用存在的验证码。\nThis validation code doesn't exist. Please use an existing one." )
return
}
if err != nil {
sendPrivateMessage ( s , m . Author . ID , "错误已发生。请稍候尝试。\nAn error has occurred. Please try again later." )
auditLog ( s , err . Error ( ) )
return
}
member , err := s . GuildMember ( guildID , inviter )
if err != nil {
sendPrivateMessage ( s , m . Author . ID , "验证码制造者不是会员。\nValidation code creator is no longer a member." )
return
}
isMember := false
for _ , v := range member . Roles {
if v == memberRoleID {
isMember = true
}
}
if ! isMember {
sendPrivateMessage ( s , m . Author . ID , "验证码制造者不是会员。\nValidation code creator is no longer a member." )
return
}
useInvite ( messageSplit [ 1 ] )
channel , err := s . UserChannelCreate ( inviter )
if err != nil {
sendPrivateMessage ( s , m . Author . ID , "验证码制造者关闭了私信。\nValidation code creator did not enable DMs." )
return
}
msg , err := s . ChannelMessageSend ( channel . ID , fmt . Sprintf ( "<@%s>正在尝试使用验证码%s。请问是否同意该使用? " , m . Author . ID , messageSplit [ 1 ] ) )
if err != nil {
sendPrivateMessage ( s , m . Author . ID , "验证码制造者关闭了私信。\nValidation code creator did not enable DMs." )
return
}
pendingInviteConfirmation [ msg . ID ] = invite {
User : m . Author . ID ,
Reason : messageSplit [ 2 ] ,
}
s . MessageReactionAdd ( channel . ID , msg . ID , emojiX )
s . MessageReactionAdd ( channel . ID , msg . ID , emojiCheck )
}
func checkForInvite ( s * discordgo . Session , r * discordgo . MessageReactionAdd ) {
if r . UserID == s . State . User . ID {
return
}
invite , ok := pendingInviteConfirmation [ r . MessageID ]
if ! ok {
return
}
delete ( pendingInviteConfirmation , r . MessageID )
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." )
return
case emojiCheck :
default :
return
}
s . ChannelMessageSend ( r . ChannelID , "已同意验证码的使用。该验证码已被无效化。\nUse of validation code accepted. This validation code has been invalidated." )
msg , err := s . ChannelMessageSend ( voteChannel , "正在准备新的一个投票…… Preparing for the next vote..." )
if err != nil {
sendPrivateMessage ( s , r . UserID , "创造投票失败。Failed to create vote." )
auditLog ( s ,
fmt . Sprintf ( "Error occurred while creating vote for <@%s>.\n%v\nError: %s" , r . UserID , * r , err . Error ( ) ) ,
)
return
}
id , err := createInviteVote ( msg . ID , invite . User , invite . Reason )
if err != nil {
sendPrivateMessage ( s , r . UserID , "创造投票失败。Failed to create vote." )
auditLog ( s ,
fmt . Sprintf ( "Error occurred while creating vote for <@%s>.\n%v\nError: %s" , r . UserID , * r , err . Error ( ) ) ,
)
return
}
auditLog ( s , fmt . Sprintf ( "Vote ID %d has been created by <@%s>." , id , r . UserID ) )
s . ChannelMessageEdit ( voteChannel , msg . ID , "" )
s . ChannelMessageEditEmbed ( voteChannel , msg . ID , createInviteEmbed ( id , invite . User + ":" + invite . Reason ) )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiX )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiOne )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiTwo )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiThree )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiFour )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiFive )
s . MessageReactionAdd ( voteChannel , msg . ID , emojiCheck )
}