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"
|
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-13 12:38:00 +07:00
|
|
|
"TerraOceanBot/discord/message"
|
2019-10-13 11:48:24 +07:00
|
|
|
"TerraOceanBot/server"
|
2019-10-12 06:40:46 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type invite struct {
|
|
|
|
User string
|
|
|
|
Reason string
|
|
|
|
}
|
2019-10-09 14:13:59 +07:00
|
|
|
|
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], ' ') {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
|
2019-10-12 14:44:05 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
return
|
|
|
|
}
|
2019-10-16 04:40:26 +07:00
|
|
|
if len(command[1]) > 512 {
|
|
|
|
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeTooLong, config.ErrorColour).
|
|
|
|
Send(s, m.ChannelID)
|
|
|
|
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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
|
2019-10-12 14:44:05 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
return
|
|
|
|
}
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
|
2019-10-12 14:44:05 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
case db.ErrInviteUsed:
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
|
2019-10-12 14:44:05 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
case nil:
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
|
2019-10-12 14:44:05 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
default:
|
2019-10-13 12:38:00 +07:00
|
|
|
message.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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeReuse, config.ErrorColour).
|
2019-10-12 13:49:41 +07:00
|
|
|
Send(s, m.ChannelID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == db.ErrNotFound {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour).
|
2019-10-17 08:46:34 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.AuditErrorPM(s, m.ChannelID, err)
|
2019-10-12 13:49:41 +07:00
|
|
|
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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour).
|
2019-10-17 08:46:34 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
|
2019-10-17 08:46:34 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
return
|
|
|
|
}
|
2019-10-13 12:38:00 +07:00
|
|
|
msg, err := message.InitNewEmbed(
|
2019-10-12 14:44:05 +07:00
|
|
|
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 {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.AuditErrorPM(s, m.ChannelID, err)
|
2019-10-12 13:49:41 +07:00
|
|
|
return
|
|
|
|
}
|
2019-10-14 10:01:25 +07:00
|
|
|
queueForReact(
|
|
|
|
msg.ID,
|
|
|
|
[]string{emojiCheck, emojiX},
|
|
|
|
invite{
|
|
|
|
User: m.Author.ID,
|
|
|
|
Reason: command[2],
|
|
|
|
},
|
|
|
|
inviteCallback,
|
|
|
|
)
|
2019-10-12 13:49:41 +07:00
|
|
|
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
|
2019-10-12 14:44:05 +07:00
|
|
|
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
|
2019-10-16 05:42:36 +07:00
|
|
|
Send(s, m.ChannelID)
|
2019-10-12 13:49:41 +07:00
|
|
|
},
|
|
|
|
)))
|
2019-10-12 06:40:46 +07:00
|
|
|
|
2019-10-14 10:01:25 +07:00
|
|
|
func inviteCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
|
|
|
|
invite := info.(invite)
|
|
|
|
if r.Emoji.Name == emojiX {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-12 14:44:05 +07:00
|
|
|
config.ValidateConfirmationRejectCreatorTitle,
|
|
|
|
config.ValidateConfirmationRejectCreatorDescription,
|
|
|
|
config.ErrorColour,
|
|
|
|
).SendPM(s, r.UserID)
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-12 14:44:05 +07:00
|
|
|
config.ValidateConfirmationRejectRejecteeTitle,
|
|
|
|
config.ValidateConfirmationRejectRejecteeDescription,
|
|
|
|
config.ErrorColour,
|
|
|
|
).SendPM(s, invite.User)
|
2019-10-12 06:40:46 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-12 14:44:05 +07:00
|
|
|
config.ValidateConfirmationAcceptCreatorTitle,
|
|
|
|
config.ValidateConfirmationAcceptCreatorDescription,
|
2019-10-17 08:54:12 +07:00
|
|
|
config.SuccessColour,
|
2019-10-12 14:44:05 +07:00
|
|
|
).SendPM(s, r.UserID)
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-12 14:44:05 +07:00
|
|
|
config.ValidateConfirmationAcceptAccepteeTitle,
|
|
|
|
config.ValidateConfirmationAcceptAccepteeDescription,
|
2019-10-17 08:54:12 +07:00
|
|
|
config.SuccessColour,
|
2019-10-12 14:44:05 +07:00
|
|
|
).SendPM(s, invite.User)
|
2019-10-12 06:40:46 +07:00
|
|
|
|
2019-10-14 10:01:25 +07:00
|
|
|
msg, err := message.InitNewEmbed(
|
|
|
|
config.VoteSuggestionUpcomingTitle,
|
|
|
|
config.VoteSuggestionUpcomingDescription,
|
|
|
|
config.VoteSuggestionUpcomingColour,
|
|
|
|
).Send(s, config.VoteChannel)
|
2019-10-12 06:40:46 +07:00
|
|
|
if err != nil {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.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-13 12:38:00 +07:00
|
|
|
message.AuditErrorPM(s, r.UserID, err)
|
2019-10-12 06:40:46 +07:00
|
|
|
return
|
|
|
|
}
|
2019-10-13 12:38:00 +07:00
|
|
|
message.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-13 11:48:24 +07:00
|
|
|
server.VoteNotification()
|
2019-10-12 06:40:46 +07:00
|
|
|
}
|