package modules import ( "fmt" "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, func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { if len(command) < 2 { s.ChannelMessageSend(m.ChannelID, "指令的使用方法是`!invite <你所要设置的验证码>`.\nUsage: `!invite `") return } if len(command) > 2 { s.ChannelMessageSend(m.ChannelID, "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces.") return } err := db.SetInviteOwner(command[1], m.Author.ID) switch err { 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.", ) 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(), ) } }, )) 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 := s.ChannelMessageSend(channel.ID, fmt.Sprintf("<@%s>正在尝试使用验证码%s。请问是否同意该使用?", m.Author.ID, command[1])) 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, emojiX) s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck) 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: 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.") 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) }