package main import ( "fmt" "strings" "github.com/bwmarrin/discordgo" ) 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 `") return } if len(command) > 2 { s.ChannelMessageSend(m.ChannelID, "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces.") return } err := setInviteOwner(command[1], m.Author.ID) switch err { case errAlreadyExists: owner, _ := 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 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(), ) } } 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 `") return } messageSplit := strings.SplitN(m.Content, " ", 3) inviter, err := getInviteOwner(messageSplit[1]) if err == errInviteUsed { sendPrivateMessage(s, m.Author.ID, "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one.") return } if err == errNotFound { 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 } useInvite(messageSplit[1]) member, err := s.GuildMember(guildID, inviter) if err != nil { sendPrivateMessage(s, m.Author.ID, "验证码制造者不是会员。\nValidation code creator is no longer a member.") return } isMember := false for _, v := range member.Roles { if v == memberRoleID { 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.") } 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(voteChannel, "正在准备新的一个投票…… Preparing for the next vote...") 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 } id, err := createInviteVote(msg.ID, invite.User, invite.Reason) 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)) s.ChannelMessageEdit(voteChannel, msg.ID, "") s.ChannelMessageEditEmbed(voteChannel, msg.ID, createInviteEmbed(id, invite.User+":"+invite.Reason)) s.MessageReactionAdd(voteChannel, msg.ID, emojiX) s.MessageReactionAdd(voteChannel, msg.ID, emojiOne) s.MessageReactionAdd(voteChannel, msg.ID, emojiTwo) s.MessageReactionAdd(voteChannel, msg.ID, emojiThree) s.MessageReactionAdd(voteChannel, msg.ID, emojiFour) s.MessageReactionAdd(voteChannel, msg.ID, emojiFive) s.MessageReactionAdd(voteChannel, msg.ID, emojiCheck) }