package modules import ( "github.com/bwmarrin/discordgo" "TerraOceanBot/db" "TerraOceanBot/discord/backend" "TerraOceanBot/discord/config" "TerraOceanBot/discord/message" "TerraOceanBot/server" ) var updateMinecraftUsername = memberFilter(true, enforceArgumentCount( config.SetMCUsernameUsage, 2, func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { oldUsername, err := db.GetMinecraftUsername(m.Author.ID) if err != nil { if err == db.ErrNotFound { processUsernameUpdate(s, m.Author.ID, m.ChannelID, command[1]) return } message.AuditError(s, m.ChannelID, err) return } if oldUsername == command[1] { message.InitNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour). Send(s, m.ChannelID) return } msg, err := message.InitNewEmbed( config.SetMCUsernameConfirmTitle, config.SetMCUsernameConfirmDescription, config.SetMCUsernameConfirmColour, ).AddField(config.SetMCUsernameConfirmOldUsername, oldUsername). AddField(config.SetMCUsernameConfirmNewUsername, command[1]). SendPM(s, m.Author.ID) if err != nil { message.AuditError(s, m.ChannelID, err) } queueForReact( msg.ID, []string{emojiCheck, emojiX}, command[1], confirmMinecraftUsername, ) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX) }, )) func confirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) { newUsername := info.(string) switch r.Emoji.Name { case emojiCheck: case emojiX: message.InitNewEmbed( config.SetMCUsernameConfirmDeniedTitle, config.SetMCUsernameConfirmDeniedDescription, config.SuccessColour, ).Send(s, r.ChannelID) return } processUsernameUpdate(s, r.UserID, r.ChannelID, newUsername) } func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUsername string) { oldUsername, oldErr := db.GetMinecraftUsername(discordID) err := db.SetMinecraftUsername(discordID, newUsername) if err == db.ErrAlreadyExists { message.InitNewEmbed( config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameAlreadyOwned, config.ErrorColour, ).Send(s, channelID) return } backend.UpdateVoiceChannelState(s) if oldErr == nil { server.WhitelistRemove(oldUsername) server.DisconnectUser(oldUsername) } message.InitNewEmbed( config.SetMCUsernameSuccessTitle, config.SetMCUsernameSuccessDescription, config.SuccessColour, ).Send(s, channelID) }