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() {
|
Some(ActivityData::custom(match status.online() {
|
||||||
true => {
|
true => {
|
||||||
format!(
|
format!(
|
||||||
"{}/{} players online",
|
"{}/{} players online {}",
|
||||||
status.players().unwrap(),
|
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(),
|
false => "Server offline!".to_string(),
|
||||||
|
|||||||
@ -4,7 +4,6 @@ mod scpsl;
|
|||||||
mod types;
|
mod types;
|
||||||
use dotenvy::{self, dotenv};
|
use dotenvy::{self, dotenv};
|
||||||
use minecraft::Minecraft;
|
use minecraft::Minecraft;
|
||||||
use scpsl::Scpsl;
|
|
||||||
use tokio::join;
|
use tokio::join;
|
||||||
use url::{self, Url};
|
use url::{self, Url};
|
||||||
|
|
||||||
|
|||||||
@ -3,12 +3,21 @@ use reqwest::{Client, Request};
|
|||||||
use serenity::*;
|
use serenity::*;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{funcs, types::ServerResponse};
|
use crate::{funcs, types::{self, ServerResponse}};
|
||||||
|
|
||||||
#[derive(serde::Deserialize, Debug)]
|
#[derive(serde::Deserialize, Debug)]
|
||||||
struct ServerSummary {
|
struct ServerSummary {
|
||||||
online: bool,
|
online: bool,
|
||||||
players: Option<Players>,
|
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)]
|
#[derive(serde::Deserialize, Debug)]
|
||||||
@ -49,9 +58,10 @@ impl Minecraft {
|
|||||||
data.online,
|
data.online,
|
||||||
Some(players.online as u32),
|
Some(players.online as u32),
|
||||||
Some(players.max as u32),
|
Some(players.max as u32),
|
||||||
|
Some(data.version.name_raw)
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok(ServerResponse::new(data.online, None, None))
|
Ok(ServerResponse::new(data.online, None, None, None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub async fn run(&self) {
|
pub async fn run(&self) {
|
||||||
|
|||||||
@ -46,6 +46,7 @@ impl Scpsl {
|
|||||||
data.online,
|
data.online,
|
||||||
playercount_unwrapped.first().copied(),
|
playercount_unwrapped.first().copied(),
|
||||||
playercount_unwrapped.get(1).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 {
|
pub struct ServerResponse {
|
||||||
online: bool,
|
online: bool,
|
||||||
players: Option<u32>,
|
players: Option<u32>,
|
||||||
max: Option<u32>,
|
max: Option<u32>,
|
||||||
|
version: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerResponse {
|
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 {
|
ServerResponse {
|
||||||
online,
|
online,
|
||||||
players,
|
players,
|
||||||
max,
|
max,
|
||||||
|
version
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,11 +33,27 @@ impl ServerResponse {
|
|||||||
self.players >= self.max
|
self.players >= self.max
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn version(&self) -> &Option<String> {
|
||||||
|
&self.version
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(&self) -> String {
|
||||||
if let (Some(players), Some(max)) = (self.players, self.max) {
|
format!("{} {} {}",
|
||||||
format!("{}/{} ({})", players, max, self.online)
|
if let Some(players) = self.players {
|
||||||
} else {
|
if let Some(max) = self.max {
|
||||||
format!("N/A ({})", self.online)
|
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