2019-10-13 09:59:35 +07:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
|
|
|
"TerraOceanBot/db"
|
|
|
|
"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-13 09:59:35 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-10-13 12:38:00 +07:00
|
|
|
message.AuditError(s, m.ChannelID, err)
|
2019-10-13 09:59:35 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if oldUsername == command[1] {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour).
|
2019-10-13 09:59:35 +07:00
|
|
|
Send(s, m.ChannelID)
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 12:38:00 +07:00
|
|
|
msg, err := message.InitNewEmbed(
|
2019-10-13 09:59:35 +07:00
|
|
|
config.SetMCUsernameConfirmTitle,
|
|
|
|
config.SetMCUsernameConfirmDescription,
|
|
|
|
config.SetMCUsernameConfirmColour,
|
|
|
|
).AddField(config.SetMCUsernameConfirmOldUsername, oldUsername).
|
|
|
|
AddField(config.SetMCUsernameConfirmNewUsername, command[1]).
|
|
|
|
SendPM(s, m.Author.ID)
|
|
|
|
if err != nil {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.AuditError(s, m.ChannelID, err)
|
2019-10-13 09:59:35 +07:00
|
|
|
}
|
2019-10-14 10:01:25 +07:00
|
|
|
queueForReact(
|
|
|
|
msg.ID,
|
|
|
|
[]string{emojiCheck, emojiX},
|
|
|
|
command[1],
|
|
|
|
confirmMinecraftUsername,
|
|
|
|
)
|
2019-10-13 09:59:35 +07:00
|
|
|
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
|
|
|
|
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
|
2019-10-14 10:01:25 +07:00
|
|
|
func confirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
|
|
|
|
newUsername := info.(string)
|
2019-10-13 09:59:35 +07:00
|
|
|
|
|
|
|
switch r.Emoji.Name {
|
|
|
|
case emojiCheck:
|
|
|
|
case emojiX:
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
|
|
|
config.SetMCUsernameConfirmDeniedTitle,
|
|
|
|
config.SetMCUsernameConfirmDeniedDescription,
|
|
|
|
config.SuccessColour,
|
|
|
|
).Send(s, r.ChannelID)
|
2019-10-13 09:59:35 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
processUsernameUpdate(s, r.UserID, r.ChannelID, newUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUsername string) {
|
2019-10-13 11:48:24 +07:00
|
|
|
oldUsername, oldErr := db.GetMinecraftUsername(discordID)
|
2019-10-13 09:59:35 +07:00
|
|
|
err := db.SetMinecraftUsername(discordID, newUsername)
|
|
|
|
if err == db.ErrAlreadyExists {
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-13 09:59:35 +07:00
|
|
|
config.SetMCUsernameErrorTitle,
|
|
|
|
config.ErrorSetMCUsernameAlreadyOwned,
|
|
|
|
config.ErrorColour,
|
|
|
|
).Send(s, channelID)
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 11:48:24 +07:00
|
|
|
|
|
|
|
if oldErr == nil {
|
|
|
|
server.WhitelistRemove(oldUsername)
|
2019-10-14 10:01:25 +07:00
|
|
|
server.DisconnectUser(oldUsername)
|
2019-10-13 11:48:24 +07:00
|
|
|
}
|
2019-10-18 04:20:11 +07:00
|
|
|
if err == nil {
|
|
|
|
server.WhitelistAdd(newUsername)
|
|
|
|
server.ConnectUser(newUsername)
|
|
|
|
}
|
2019-10-13 11:48:24 +07:00
|
|
|
|
2019-10-13 12:38:00 +07:00
|
|
|
message.InitNewEmbed(
|
2019-10-13 09:59:35 +07:00
|
|
|
config.SetMCUsernameSuccessTitle,
|
|
|
|
config.SetMCUsernameSuccessDescription,
|
|
|
|
config.SuccessColour,
|
|
|
|
).Send(s, channelID)
|
|
|
|
}
|