use crate::{commands::utils, Context, Error}; use chrono::{DateTime, Utc}; use log::*; use serde::Deserialize; mod wrc_date { use chrono::{DateTime, TimeZone, Utc}; use serde::{self, Deserialize, Deserializer}; const FORMAT: &'static str = "%Y-%m-%dT%H:%M:%S%:z"; pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; Utc.datetime_from_str(&s, FORMAT) .map_err(serde::de::Error::custom) } } #[derive(Deserialize)] struct WRC { // #[serde(rename = "earliestPlayableStart")] // #[serde(with = "es_date_time")] // earliest_playable_start: DateTime, // id: u32, name: String, #[serde(rename = "eventDays")] days: Vec, } #[derive(Deserialize)] struct WRCDays { // id: u32, #[serde(rename = "eventDay")] event_day: String, #[serde(rename = "spottChannel")] spott_channel: WRCChannel, } #[derive(Deserialize)] struct WRCChannel { // id: u32, // #[serde(rename = "displayName")] // name: String, assets: Vec, } #[derive(Deserialize)] struct WRCAssets { // id: u32, #[serde(with = "wrc_date")] start: DateTime, #[serde(with = "wrc_date")] end: DateTime, // duration: u32, content: WRCContent, } #[derive(Deserialize)] struct WRCContent { title: String, id: u32, #[serde(default)] description: Option, } async fn get_schedule() -> Result, Error> { let client = reqwest::Client::new(); let req = client .get("https://api.wrc.com/sdb/rallyevent/active/") .send() .await?; if req.status().as_u16() == 404 { info!("Error 404 on getting wrc schedule"); Ok(None) } else if req.status().as_u16() == 200 { let data: WRC = req.json().await?; Ok(Some(data)) } else { Ok(None) } } // WRC sessions #[poise::command(slash_command, ephemeral)] pub async fn wrc( ctx: Context<'_>, // #[description = "Filter sessions for when they are/were happening, defaults to future"] // timeframe: Option, // #[description = "Content to filter on"] filter: Option, ) -> Result<(), Error> { let wrc = get_schedule().await?; match wrc { None => { ctx.say("No WRC sessions found").await?; Ok(()) } Some(wrc_data) => { let mut events: Vec = vec![]; for day in wrc_data.days { for session in day.spott_channel.assets { let desc = if let Some(d) = session.content.description { d } else { "".to_string() }; events.push(format!("{}: - : [{}](https://morningstreams.com/wrcplayer.html?id={}) {} {}", day.event_day, session.start.timestamp(), session.end.timestamp(), session.content.id, session.content.id, session.content.title, desc)); } } let pages = utils::paginator(events, 2400, "\n".to_owned()); utils::paginate_string_embed(ctx, format!("WRC {}", wrc_data.name), pages).await?; Ok(()) } } }