TerraOceanPlugin/GoBot/discord/modules/invite.go

179 lines
5.2 KiB
Go

package modules
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
)
var pendingInviteConfirmation = make(map[string]invite)
type invite struct {
User string
Reason string
}
var createInvite = enforceDM(memberFilter(true, enforceArgumentCount(
config.InviteUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if strings.ContainsRune(command[1], ' ') {
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
Send(s, m.ChannelID)
return
}
err := db.SetInviteOwner(command[1], m.Author.ID)
switch err {
case db.ErrAlreadyExists:
owner, _ := db.GetInviteOwner(command[1])
if owner == m.Author.ID {
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
Send(s, m.ChannelID)
return
}
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
Send(s, m.ChannelID)
case db.ErrInviteUsed:
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
Send(s, m.ChannelID)
case nil:
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
Send(s, m.ChannelID)
default:
auditErrorPM(s, m.Author.ID, err)
}
},
)))
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
}
db.UseInvite(command[1])
member, err := s.GuildMember(config.GuildID, inviter)
if err != nil {
auditErrorPM(s, m.ChannelID, err)
return
}
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
}
channel, err := s.UserChannelCreate(inviter)
if err != nil {
initNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
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
}
pendingInviteConfirmation[msg.ID] = invite{
User: m.Author.ID,
Reason: command[2],
}
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)
},
)))
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:
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
}
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 {
auditErrorPM(s, r.UserID, err)
return
}
id, err := db.CreateInviteVote(msg.ID, invite.User, invite.Reason)
if err != nil {
auditErrorPM(s, r.UserID, err)
return
}
auditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, r.UserID))
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)
}