TerraOceanPlugin/GoBot/db/minecraft.go

45 lines
1.0 KiB
Go

2019-10-13 09:59:35 +07:00
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
}