1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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?)
}
}