minecraft-server-invites/internal/minecraft/minecraft.go

48 lines
982 B
Go
Raw Normal View History

package minecraft
import (
"errors"
"fmt"
"whitelistmanager/internal/store"
2022-07-16 02:44:32 +01:00
mcrcon "github.com/Kelwing/mc-rcon"
)
const (
RconConnectionError = "rcon connection failed"
RconAuthError = "failed to authenticate with rcon server"
RconCommandError = "command failed"
)
2022-07-16 02:16:14 +01:00
type MinecraftCom struct {
}
type Minecraft interface {
Whitelist(user string, server store.Server) (string, error)
}
func NewCom() *MinecraftCom {
return &MinecraftCom{}
}
func (m *MinecraftCom) Whitelist(user string, server store.Server) (string, error) {
conn := new(mcrcon.MCConn)
err := conn.Open(server.Rcon.Address, server.Rcon.Password)
if err != nil {
return "", errors.New(RconConnectionError)
}
defer conn.Close()
err = conn.Authenticate()
if err != nil {
return "", errors.New(RconAuthError)
}
command := fmt.Sprintf("whitelist add %s", user)
resp, err := conn.SendCommand(command)
if err != nil {
return "", errors.New(RconCommandError)
}
return resp, nil
}