forked from chanbakjsd/TerraOceanPlugin
go: Implement '!setmcusernae' command
parent
e9d5730300
commit
f3c69655e2
@ -0,0 +1,44 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
func GetMinecraftUsername(discordID string) (string, error) {
|
||||||
|
rows, err := db.Query("SELECT minecraftUsername FROM minecraftUsername WHERE discordId=?", discordID)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
if rows.Next() {
|
||||||
|
var username string
|
||||||
|
err := rows.Scan(&username)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return username, nil
|
||||||
|
}
|
||||||
|
return "", ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDiscordID(username string) (string, error) {
|
||||||
|
rows, err := db.Query("SELECT discordId FROM minecraftUsername WHERE minecraftUsername=?", username)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
if rows.Next() {
|
||||||
|
var id string
|
||||||
|
err := rows.Scan(&id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
return "", ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMinecraftUsername(discordID, username string) error {
|
||||||
|
_, err := GetDiscordID(username)
|
||||||
|
if err != ErrNotFound {
|
||||||
|
return ErrAlreadyExists
|
||||||
|
}
|
||||||
|
_, err = db.Exec("REPLACE INTO minecraftUsername(discordId, minecraftUsername) VALUES(?,?)", discordID, username)
|
||||||
|
return err
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
package modules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
|
"TerraOceanBot/db"
|
||||||
|
"TerraOceanBot/discord/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var minecraftConfirmReact = make(map[string]string)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
auditError(s, m.ChannelID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if oldUsername == command[1] {
|
||||||
|
initNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour).
|
||||||
|
Send(s, m.ChannelID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg, err := initNewEmbed(
|
||||||
|
config.SetMCUsernameConfirmTitle,
|
||||||
|
config.SetMCUsernameConfirmDescription,
|
||||||
|
config.SetMCUsernameConfirmColour,
|
||||||
|
).AddField(config.SetMCUsernameConfirmOldUsername, oldUsername).
|
||||||
|
AddField(config.SetMCUsernameConfirmNewUsername, command[1]).
|
||||||
|
SendPM(s, m.Author.ID)
|
||||||
|
if err != nil {
|
||||||
|
auditError(s, m.ChannelID, err)
|
||||||
|
}
|
||||||
|
minecraftConfirmReact[msg.ID] = command[1]
|
||||||
|
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
|
||||||
|
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
func ConfirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
||||||
|
if r.UserID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newUsername, ok := minecraftConfirmReact[r.MessageID]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(minecraftConfirmReact, r.MessageID)
|
||||||
|
|
||||||
|
switch r.Emoji.Name {
|
||||||
|
case emojiCheck:
|
||||||
|
case emojiX:
|
||||||
|
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) {
|
||||||
|
err := db.SetMinecraftUsername(discordID, newUsername)
|
||||||
|
if err == db.ErrAlreadyExists {
|
||||||
|
initNewEmbed(
|
||||||
|
config.SetMCUsernameErrorTitle,
|
||||||
|
config.ErrorSetMCUsernameAlreadyOwned,
|
||||||
|
config.ErrorColour,
|
||||||
|
).Send(s, channelID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
initNewEmbed(
|
||||||
|
config.SetMCUsernameSuccessTitle,
|
||||||
|
config.SetMCUsernameSuccessDescription,
|
||||||
|
config.SuccessColour,
|
||||||
|
).Send(s, channelID)
|
||||||
|
}
|
Loading…
Reference in New Issue