TerraOceanPlugin/GoBot/discord/modules/invite.go

179 lines
5.2 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
2019-10-12 06:40:46 +07:00
import (
"fmt"
2019-10-12 14:44:05 +07:00
"strings"
2019-10-12 06:40:46 +07:00
"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-12 14:44:05 +07:00
var createInvite = enforceDM(memberFilter(true, enforceArgumentCount(
config.InviteUsage, 2,
2019-10-12 13:49:41 +07:00
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
2019-10-12 14:44:05 +07:00
if strings.ContainsRune(command[1], ' ') {
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
return
}
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 {
2019-10-12 14:44:05 +07:00
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
return
}
2019-10-12 14:44:05 +07:00
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
case db.ErrInviteUsed:
2019-10-12 14:44:05 +07:00
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
case nil:
2019-10-12 14:44:05 +07:00
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
default:
2019-10-12 14:44:05 +07:00
auditErrorPM(s, m.Author.ID, err)
2019-10-12 13:49:41 +07:00
}
},
2019-10-12 14:44:05 +07:00
)))
2019-10-12 13:49:41 +07:00
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)
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
}
2019-10-12 14:44:05 +07:00
msg, err := initNewEmbed(
config.ValidateConfirmationTitle,
config.ValidateConfirmationDescription,
config.ValidateConfirmationColour,
).AddField(config.ValidateConfirmationUser, m.Author.Mention()).
AddField(config.ValidateConfirmationInviteCode, command[1]).
Send(s, channel.ID)
2019-10-12 13:49:41 +07:00
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, emojiCheck)
2019-10-12 14:44:05 +07:00
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
2019-10-12 13:49:41 +07:00
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:
2019-10-12 14:44:05 +07:00
initNewEmbed(
config.ValidateConfirmationRejectCreatorTitle,
config.ValidateConfirmationRejectCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
initNewEmbed(
config.ValidateConfirmationRejectRejecteeTitle,
config.ValidateConfirmationRejectRejecteeDescription,
config.ErrorColour,
).SendPM(s, invite.User)
2019-10-12 06:40:46 +07:00
return
case emojiCheck:
default:
return
}
2019-10-12 14:44:05 +07:00
initNewEmbed(
config.ValidateConfirmationAcceptCreatorTitle,
config.ValidateConfirmationAcceptCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
initNewEmbed(
config.ValidateConfirmationAcceptAccepteeTitle,
config.ValidateConfirmationAcceptAccepteeDescription,
config.ErrorColour,
).SendPM(s, invite.User)
2019-10-12 06:40:46 +07:00
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, "")
2019-10-12 15:33:06 +07:00
createInviteEmbed(id, invite.User+":"+invite.Reason).Edit(s, config.VoteChannel, msg.ID)
2019-10-12 09:45:43 +07:00
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
}