2019-10-12 09:45:43 +07:00
package modules
2019-10-09 14:13:59 +07:00
2019-10-12 06:40:46 +07:00
import (
"fmt"
"github.com/bwmarrin/discordgo"
2019-10-12 08:07:10 +07:00
"TerraOceanBot/db"
2019-10-12 09:45:43 +07:00
"TerraOceanBot/discord/config"
2019-10-12 06:40:46 +07:00
)
var pendingInviteConfirmation = make ( map [ string ] invite )
type invite struct {
User string
Reason string
}
2019-10-09 14:13:59 +07:00
2019-10-12 13:49:41 +07:00
var createInvite = enforceDM ( memberFilter ( true ,
func ( s * discordgo . Session , m * discordgo . MessageCreate , command [ ] string ) {
if len ( command ) < 2 {
s . ChannelMessageSend ( m . ChannelID , "指令的使用方法是`!invite <你所要设置的验证码>`.\nUsage: `!invite <validation code>`" )
return
}
if len ( command ) > 2 {
s . ChannelMessageSend ( m . ChannelID , "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces." )
return
}
2019-10-09 14:13:59 +07:00
2019-10-12 13:49:41 +07:00
err := db . SetInviteOwner ( command [ 1 ] , m . Author . ID )
switch err {
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." ,
)
return
}
s . ChannelMessageSend (
m . ChannelID ,
"这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one." ,
)
case db . ErrInviteUsed :
s . ChannelMessageSend (
m . ChannelID ,
"这个验证码已被使用。请使用别的验证码。\nThis validation code has already been used. Please try another one." ,
)
case nil :
2019-10-09 14:13:59 +07:00
s . ChannelMessageSend (
m . ChannelID ,
2019-10-12 13:49:41 +07:00
"这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite." ,
2019-10-09 14:13:59 +07:00
)
2019-10-12 13:49:41 +07:00
default :
s . ChannelMessageSend (
m . ChannelID ,
"不明错误已发生,请把这个错误信息发在群里。\nAn unknown error has occurred. Please pass this error message on.\n" + err . Error ( ) ,
)
}
} ,
) )
var checkUseInvite = enforceDM ( memberFilter ( false , enforceArgumentCount (
config . ValidateUsage , 3 ,
func ( s * discordgo . Session , m * discordgo . MessageCreate , command [ ] string ) {
inviter , err := db . GetInviteOwner ( command [ 1 ] )
if err == db . ErrInviteUsed {
initNewEmbed ( config . ErrorValidateTitle , config . ErrorValidationCodeReuse , config . ErrorColour ) .
Send ( s , m . ChannelID )
return
}
if err == db . ErrNotFound {
initNewEmbed ( config . ErrorValidateTitle , config . ErrorValidationCodeNotExist , config . ErrorColour ) .
SendPM ( s , m . ChannelID )
return
}
if err != nil {
auditError ( s , m . ChannelID , err )
2019-10-09 14:13:59 +07:00
return
}
2019-10-12 06:40:46 +07:00
2019-10-12 13:49:41 +07:00
db . UseInvite ( command [ 1 ] )
member , err := s . GuildMember ( config . GuildID , inviter )
if err != nil {
auditErrorPM ( s , m . ChannelID , err )
return
}
2019-10-12 06:40:46 +07:00
2019-10-12 13:49:41 +07:00
isMember := false
for _ , v := range member . Roles {
if v == config . MemberRoleID {
isMember = true
}
}
if ! isMember {
initNewEmbed ( config . ErrorValidateTitle , config . ErrorValidationCodeNotOwnedByMember , config . ErrorColour ) .
SendPM ( s , m . ChannelID )
return
2019-10-12 06:40:46 +07:00
}
2019-10-12 13:49:41 +07:00
channel , err := s . UserChannelCreate ( inviter )
if err != nil {
initNewEmbed ( config . ErrorValidateTitle , config . ErrorValidationCodeCreatorDisabledPM , config . ErrorColour ) .
SendPM ( s , m . ChannelID )
return
}
msg , err := s . ChannelMessageSend ( channel . ID , fmt . Sprintf ( "<@%s>正在尝试使用验证码%s。请问是否同意该使用? " , m . Author . ID , command [ 1 ] ) )
if err != nil {
auditErrorPM ( s , m . ChannelID , err )
return
}
pendingInviteConfirmation [ msg . ID ] = invite {
User : m . Author . ID ,
Reason : command [ 2 ] ,
}
s . MessageReactionAdd ( channel . ID , msg . ID , emojiX )
s . MessageReactionAdd ( channel . ID , msg . ID , emojiCheck )
initNewEmbed ( config . SuccessTitle , config . ValidateSuccess , config . SuccessColour ) .
SendPM ( s , m . ChannelID )
} ,
) ) )
2019-10-12 06:40:46 +07:00
2019-10-12 09:45:43 +07:00
func CheckForInvite ( s * discordgo . Session , r * discordgo . MessageReactionAdd ) {
2019-10-12 06:40:46 +07:00
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." )
2019-10-12 09:45:43 +07:00
msg , err := s . ChannelMessageSend ( config . VoteChannel , "正在准备新的一个投票…… Preparing for the next vote..." )
2019-10-12 06:40:46 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditErrorPM ( s , r . UserID , err )
2019-10-12 06:40:46 +07:00
return
}
2019-10-12 08:07:10 +07:00
id , err := db . CreateInviteVote ( msg . ID , invite . User , invite . Reason )
2019-10-12 06:40:46 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditErrorPM ( s , r . UserID , err )
2019-10-12 06:40:46 +07:00
return
}
2019-10-12 13:49:41 +07:00
auditInfo ( s , fmt . Sprintf ( "Vote ID %d has been created by <@%s>." , id , r . UserID ) )
2019-10-12 09:45:43 +07:00
s . ChannelMessageEdit ( config . VoteChannel , msg . ID , "" )
s . ChannelMessageEditEmbed ( config . VoteChannel , msg . ID , createInviteEmbed ( id , invite . User + ":" + invite . Reason ) )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiX )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiOne )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiTwo )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiThree )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiFour )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiFive )
s . MessageReactionAdd ( config . VoteChannel , msg . ID , emojiCheck )
2019-10-12 06:40:46 +07:00
}