package minecraft import ( "errors" "fmt" "whitelistmanager/internal/store" mcrcon "github.com/Kelwing/mc-rcon" ) const ( RconConnectionError = "rcon connection failed" RconAuthError = "failed to authenticate with rcon server" RconCommandError = "command failed" ) 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 }