pub mod commands;
pub mod settings;
use rcon::Connection;
use commands::{Execute, SaveAll, Say};
use settings::Settings;
pub struct Client {
pub url: String,
pub port: u16,
pub connection: rcon::Connection,
}
impl Client {
pub async fn new(props: Settings) -> Result<Client, rcon::Error> {
let conn = Connection::builder()
.enable_minecraft_quirks(true)
.connect(format!("{}:{}", &props.url, props.port), &props.password)
.await?;
Ok(Client {
url: props.url,
port: props.port,
connection: conn,
})
}
async fn send(&mut self, command: &str) -> std::result::Result<String, rcon::Error> {
Ok(self.connection.cmd(command).await?)
}
pub async fn execute(&mut self, ex: Execute) -> Result<String, rcon::Error> {
Ok(self.send(&ex.command).await?)
}
pub async fn say(&mut self, s: Say) -> Result<String, rcon::Error> {
let command = format!("say {}", s.message);
Ok(self.send(&command).await?)
}
pub async fn save_all(&mut self, s: SaveAll) -> Result<String, rcon::Error> {
let command = format!("save-all{}", if s.flush { " flush" } else { "" });
Ok(self.send(&command).await?)
}
}