TerraOceanPlugin/GoBot/discord/modules/invite.go

181 lines
5.5 KiB
Go

package modules
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
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], ' ') {
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
Send(s, m.ChannelID)
return
}
if len(command[1]) > 512 {
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeTooLong, 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 {
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
Send(s, m.ChannelID)
return
}
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
Send(s, m.ChannelID)
case db.ErrInviteUsed:
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
Send(s, m.ChannelID)
case nil:
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
Send(s, m.ChannelID)
default:
message.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 {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeReuse, config.ErrorColour).
Send(s, m.ChannelID)
return
}
if err == db.ErrNotFound {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
db.UseInvite(command[1])
member, err := s.GuildMember(config.GuildID, inviter)
if err != nil {
message.AuditErrorPM(s, m.ChannelID, err)
return
}
isMember := false
for _, v := range member.Roles {
if v == config.MemberRoleID {
isMember = true
}
}
if !isMember {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
channel, err := s.UserChannelCreate(inviter)
if err != nil {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
msg, err := message.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 {
message.AuditErrorPM(s, m.ChannelID, err)
return
}
queueForReact(
msg.ID,
[]string{emojiCheck, emojiX},
invite{
User: m.Author.ID,
Reason: command[2],
},
inviteCallback,
)
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
Send(s, m.ChannelID)
},
)))
func inviteCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
invite := info.(invite)
if r.Emoji.Name == emojiX {
message.InitNewEmbed(
config.ValidateConfirmationRejectCreatorTitle,
config.ValidateConfirmationRejectCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
message.InitNewEmbed(
config.ValidateConfirmationRejectRejecteeTitle,
config.ValidateConfirmationRejectRejecteeDescription,
config.ErrorColour,
).SendPM(s, invite.User)
return
}
message.InitNewEmbed(
config.ValidateConfirmationAcceptCreatorTitle,
config.ValidateConfirmationAcceptCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
message.InitNewEmbed(
config.ValidateConfirmationAcceptAccepteeTitle,
config.ValidateConfirmationAcceptAccepteeDescription,
config.ErrorColour,
).SendPM(s, invite.User)
msg, err := message.InitNewEmbed(
config.VoteSuggestionUpcomingTitle,
config.VoteSuggestionUpcomingDescription,
config.VoteSuggestionUpcomingColour,
).Send(s, config.VoteChannel)
if err != nil {
message.AuditErrorPM(s, r.UserID, err)
return
}
id, err := db.CreateInviteVote(msg.ID, invite.User, invite.Reason)
if err != nil {
message.AuditErrorPM(s, r.UserID, err)
return
}
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, r.UserID))
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
createInviteEmbed(id, invite.User+":"+invite.Reason).Edit(s, config.VoteChannel, msg.ID)
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)
server.VoteNotification()
}