diff --git a/src/funcs.rs b/src/funcs.rs index 152f0d8..94211a9 100644 --- a/src/funcs.rs +++ b/src/funcs.rs @@ -7,9 +7,14 @@ pub fn set_presence(ctx: &poise::serenity_prelude::Context, status: ServerRespon Some(ActivityData::custom(match status.online() { true => { format!( - "{}/{} players online", + "{}/{} players online {}", status.players().unwrap(), - status.max().unwrap() + status.max().unwrap(), + if let Some(version) = status.version() { + format!("v{}", version) + } else { + "".into() + } ) } false => "Server offline!".to_string(), diff --git a/src/main.rs b/src/main.rs index 905a6de..4e52e94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ mod scpsl; mod types; use dotenvy::{self, dotenv}; use minecraft::Minecraft; -use scpsl::Scpsl; use tokio::join; use url::{self, Url}; diff --git a/src/minecraft.rs b/src/minecraft.rs index ceaf9ca..af21798 100644 --- a/src/minecraft.rs +++ b/src/minecraft.rs @@ -3,12 +3,21 @@ use reqwest::{Client, Request}; use serenity::*; use url::Url; -use crate::{funcs, types::ServerResponse}; +use crate::{funcs, types::{self, ServerResponse}}; #[derive(serde::Deserialize, Debug)] struct ServerSummary { online: bool, players: Option, + version: Version, +} + +#[derive(serde::Deserialize, Debug)] +struct Version { + name_raw: String, + name_clean: String, + name_html: String, + protocol: u32, } #[derive(serde::Deserialize, Debug)] @@ -49,9 +58,10 @@ impl Minecraft { data.online, Some(players.online as u32), Some(players.max as u32), + Some(data.version.name_raw) )) } else { - Ok(ServerResponse::new(data.online, None, None)) + Ok(ServerResponse::new(data.online, None, None, None)) } } pub async fn run(&self) { diff --git a/src/scpsl.rs b/src/scpsl.rs index 42750c0..9ce8296 100644 --- a/src/scpsl.rs +++ b/src/scpsl.rs @@ -46,6 +46,7 @@ impl Scpsl { data.online, playercount_unwrapped.first().copied(), playercount_unwrapped.get(1).copied(), + None )) } diff --git a/src/types.rs b/src/types.rs index 98a027d..605dc00 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,15 +1,19 @@ +use std::fmt::format; + pub struct ServerResponse { online: bool, players: Option, max: Option, + version: Option } impl ServerResponse { - pub fn new(online: bool, players: Option, max: Option) -> Self { + pub fn new(online: bool, players: Option, max: Option, version: Option) -> Self { ServerResponse { online, players, max, + version } } @@ -29,11 +33,27 @@ impl ServerResponse { self.players >= self.max } + pub fn version(&self) -> &Option { + &self.version + } + pub fn to_string(&self) -> String { - if let (Some(players), Some(max)) = (self.players, self.max) { - format!("{}/{} ({})", players, max, self.online) - } else { - format!("N/A ({})", self.online) - } + format!("{} {} {}", + if let Some(players) = self.players { + if let Some(max) = self.max { + format!("{}/{}", players, max) + } else { + players.to_string() + } + } else { + "N/A".into() + }, + self.online, + if let Some(version) = &self.version { + version.to_string() + } else { + "N/A".into() + } + ) } }