Add versions
This commit is contained in:
parent
8d1bdbc055
commit
a496f70cda
@ -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(),
|
||||
|
||||
@ -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};
|
||||
|
||||
|
||||
@ -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<Players>,
|
||||
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) {
|
||||
|
||||
@ -46,6 +46,7 @@ impl Scpsl {
|
||||
data.online,
|
||||
playercount_unwrapped.first().copied(),
|
||||
playercount_unwrapped.get(1).copied(),
|
||||
None
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
32
src/types.rs
32
src/types.rs
@ -1,15 +1,19 @@
|
||||
use std::fmt::format;
|
||||
|
||||
pub struct ServerResponse {
|
||||
online: bool,
|
||||
players: Option<u32>,
|
||||
max: Option<u32>,
|
||||
version: Option<String>
|
||||
}
|
||||
|
||||
impl ServerResponse {
|
||||
pub fn new(online: bool, players: Option<u32>, max: Option<u32>) -> Self {
|
||||
pub fn new(online: bool, players: Option<u32>, max: Option<u32>, version: Option<String>) -> Self {
|
||||
ServerResponse {
|
||||
online,
|
||||
players,
|
||||
max,
|
||||
version
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,11 +33,27 @@ impl ServerResponse {
|
||||
self.players >= self.max
|
||||
}
|
||||
|
||||
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)
|
||||
pub fn version(&self) -> &Option<String> {
|
||||
&self.version
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
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()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user