zmp-bot/src/data/hypixel.rs

46 lines
999 B
Rust

use crate::Error;
use reqwest::Client;
use serde::Deserialize;
#[derive(Deserialize)]
struct Links {
#[serde(rename = "DISCORD")]
discord: Option<String>,
}
#[derive(Deserialize)]
struct SocialMedia {
links: Option<Links>,
}
#[derive(Deserialize)]
struct HypixelPlayer {
#[serde(rename = "socialMedia")]
social_media: Option<SocialMedia>,
}
#[derive(Deserialize)]
pub struct HypixelResponse {
#[serde(rename = "player")]
player: HypixelPlayer,
}
impl HypixelResponse {
pub async fn get(uuid: &str, client: &Client) -> Result<Self, Error> {
let player = client
.get(format!("https://api.hypixel.net/v2/player?uuid={uuid}"))
.send()
.await?
.error_for_status()?
.json::<Self>()
.await?;
Ok(player)
}
pub fn discord(self) -> Option<String> {
self.player
.social_media
.and_then(|p| p.links)
.and_then(|l| l.discord)
}
}