TerraOceanPlugin/GoBot/discord/modules/invite.go

184 lines
6.4 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
2019-10-12 06:40:46 +07:00
import (
"fmt"
"strings"
"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
}
func createInvite(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if !enforceDM(s, m) {
return
}
if !membersOnly(s, m) {
return
}
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-12 08:07:10 +07:00
err := db.SetInviteOwner(command[1], m.Author.ID)
switch err {
2019-10-12 08:07:10 +07:00
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.",
)
2019-10-12 08:07:10 +07:00
case db.ErrInviteUsed:
s.ChannelMessageSend(
m.ChannelID,
"这个验证码已被使用。请使用别的验证码。\nThis validation code has already been used. Please try another one.",
)
case nil:
s.ChannelMessageSend(
m.ChannelID,
"这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite.",
)
default:
s.ChannelMessageSend(
m.ChannelID,
"不明错误已发生,请把这个错误信息发在群里。\nAn unknown error has occurred. Please pass this error message on.\n"+err.Error(),
)
}
}
2019-10-12 06:40:46 +07:00
func checkUseInvite(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if !enforceDM(s, m) {
return
}
if !nonMembersOnly(s, m) {
return
}
s.ChannelMessageDelete(m.ChannelID, m.ID)
if len(command) < 3 {
sendPrivateMessage(s, m.Author.ID, "指令的使用方法是`!validate <验证码> <原因>`.\nUsage: `!validate <validation code> <reason>`")
return
}
messageSplit := strings.SplitN(m.Content, " ", 3)
2019-10-12 08:07:10 +07:00
inviter, err := db.GetInviteOwner(messageSplit[1])
if err == db.ErrInviteUsed {
2019-10-12 06:40:46 +07:00
sendPrivateMessage(s, m.Author.ID, "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one.")
return
}
2019-10-12 08:07:10 +07:00
if err == db.ErrNotFound {
2019-10-12 06:40:46 +07:00
sendPrivateMessage(s, m.Author.ID, "该验证码不存在。请使用存在的验证码。\nThis validation code doesn't exist. Please use an existing one.")
return
}
if err != nil {
sendPrivateMessage(s, m.Author.ID, "错误已发生。请稍候尝试。\nAn error has occurred. Please try again later.")
auditLog(s, err.Error())
return
}
2019-10-12 08:07:10 +07:00
db.UseInvite(messageSplit[1])
2019-10-12 09:45:43 +07:00
member, err := s.GuildMember(config.GuildID, inviter)
2019-10-12 06:40:46 +07:00
if err != nil {
sendPrivateMessage(s, m.Author.ID, "验证码制造者不是会员。\nValidation code creator is no longer a member.")
return
}
isMember := false
for _, v := range member.Roles {
2019-10-12 09:45:43 +07:00
if v == config.MemberRoleID {
2019-10-12 06:40:46 +07:00
isMember = true
}
}
if !isMember {
sendPrivateMessage(s, m.Author.ID, "验证码制造者不是会员。\nValidation code creator is no longer a member.")
return
}
channel, err := s.UserChannelCreate(inviter)
if err != nil {
sendPrivateMessage(s, m.Author.ID, "验证码制造者关闭了私信。\nValidation code creator did not enable DMs.")
return
}
msg, err := s.ChannelMessageSend(channel.ID, fmt.Sprintf("<@%s>正在尝试使用验证码%s。请问是否同意该使用", m.Author.ID, messageSplit[1]))
if err != nil {
sendPrivateMessage(s, m.Author.ID, "验证码制造者关闭了私信。\nValidation code creator did not enable DMs.")
return
}
pendingInviteConfirmation[msg.ID] = invite{
User: m.Author.ID,
Reason: messageSplit[2],
}
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
sendPrivateMessage(s, m.Author.ID, "已向验证码制造者发送了请求。\nSent a request to use the validation code to its creator.")
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 {
sendPrivateMessage(s, r.UserID, "创造投票失败。Failed to create vote.")
auditLog(s,
fmt.Sprintf("Error occurred while creating vote for <@%s>.\n%v\nError: %s", r.UserID, *r, err.Error()),
)
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 {
sendPrivateMessage(s, r.UserID, "创造投票失败。Failed to create vote.")
auditLog(s,
fmt.Sprintf("Error occurred while creating vote for <@%s>.\n%v\nError: %s", r.UserID, *r, err.Error()),
)
return
}
auditLog(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
}