diff options
author | Cara Salter <cara@devcara.com> | 2022-01-16 00:02:56 -0500 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-01-16 00:02:56 -0500 |
commit | eb41fede91d072693b279f908976d2ebc2ba1ca3 (patch) | |
tree | 81154fe49330b429e66fff098653fc636747717c /src/commands/osu.rs | |
parent | 16921c23ff7186381a16ca0b03c521d56ee4eb21 (diff) | |
download | glitch-ng-eb41fede91d072693b279f908976d2ebc2ba1ca3.tar.gz glitch-ng-eb41fede91d072693b279f908976d2ebc2ba1ca3.zip |
format
Diffstat (limited to 'src/commands/osu.rs')
-rw-r--r-- | src/commands/osu.rs | 100 |
1 files changed, 73 insertions, 27 deletions
diff --git a/src/commands/osu.rs b/src/commands/osu.rs index 28d3cb3..8d91f56 100644 --- a/src/commands/osu.rs +++ b/src/commands/osu.rs @@ -29,17 +29,24 @@ async fn setup_reqwest() -> Result<reqwest::Client, Error> { scope: "public".into(), }; - let req = reqwest::Client::new().post("https://osu.ppy.sh/oauth/token") - .json(&token_req).send().await? - .json::<OsuTokenResponse>().await?; - + let req = reqwest::Client::new() + .post("https://osu.ppy.sh/oauth/token") + .json(&token_req) + .send() + .await? + .json::<OsuTokenResponse>() + .await?; let mut headers = header::HeaderMap::new(); - headers.insert("Authorization", header::HeaderValue::from_str(format!("Bearer {}", req.access_token).as_str()).unwrap()); - + headers.insert( + "Authorization", + header::HeaderValue::from_str(format!("Bearer {}", req.access_token).as_str()).unwrap(), + ); + Ok(ClientBuilder::new() .default_headers(headers) - .build().unwrap()) + .build() + .unwrap()) } #[derive(Deserialize, Serialize, Clone, Debug)] @@ -75,14 +82,21 @@ struct OsuUserStatsGrades { /// Examples: /// ~osup muirrum #[poise::command(slash_command, prefix_command)] -pub async fn osup(ctx: Context<'_>, - #[description = "The osu! username or ID to look up"] - lookup: String, - ) -> Result<(), Error> { +pub async fn osup( + ctx: Context<'_>, + #[description = "The osu! username or ID to look up"] lookup: String, +) -> Result<(), Error> { let client = setup_reqwest().await?; - let mut res = client.get(format!("https://osu.ppy.sh/api/v2/users/{}?key=username", lookup)) - .send().await?.json::<OsuUser>().await?; + let mut res = client + .get(format!( + "https://osu.ppy.sh/api/v2/users/{}?key=username", + lookup + )) + .send() + .await? + .json::<OsuUser>() + .await?; res.country_code = res.country_code.to_lowercase(); @@ -90,13 +104,31 @@ pub async fn osup(ctx: Context<'_>, m.embed(|e| { e.title(format!("osu! Profile: {}", res.clone().username)); e.thumbnail(res.clone().avatar_url); - e.field("Ranks", format!(":map: #{}\n:flag_{}: #{}", res.clone().statistics.global_rank.unwrap_or(0), res.clone().country_code, res.clone().statistics.country_rank.unwrap_or(0u32)), true); - - e.field("Stats", format!("**PP:** {}\n**Acc:** {}%", res.clone().statistics.pp, res.clone().statistics.hit_accuracy.unwrap_or(0.0)), false); + e.field( + "Ranks", + format!( + ":map: #{}\n:flag_{}: #{}", + res.clone().statistics.global_rank.unwrap_or(0), + res.clone().country_code, + res.clone().statistics.country_rank.unwrap_or(0u32) + ), + true, + ); + + e.field( + "Stats", + format!( + "**PP:** {}\n**Acc:** {}%", + res.clone().statistics.pp, + res.clone().statistics.hit_accuracy.unwrap_or(0.0) + ), + false, + ); e }); m - }).await?; + }) + .await?; Ok(()) } @@ -140,28 +172,42 @@ struct OsuBeatMapSetCovers { /// Usage: /// ~osubm <id> #[poise::command(slash_command, prefix_command)] -pub async fn osubm(ctx: Context<'_>, - #[description = "The beatmap ID"] - bm_id: u32, - ) -> Result<(), Error> { +pub async fn osubm( + ctx: Context<'_>, + #[description = "The beatmap ID"] bm_id: u32, +) -> Result<(), Error> { let client = setup_reqwest().await?; - let mut res = client.get(format!("https://osu.ppy.sh/api/v2/beatmaps/{}", bm_id)) - .send().await?.json::<OsuBeatMap>().await?; + let mut res = client + .get(format!("https://osu.ppy.sh/api/v2/beatmaps/{}", bm_id)) + .send() + .await? + .json::<OsuBeatMap>() + .await?; ctx.send(|m| { m.embed(|e| { - e.title(format!("osu! Beatmap: {} by {}", res.beatmapset.title, res.beatmapset.creator)); + e.title(format!( + "osu! Beatmap: {} by {}", + res.beatmapset.title, res.beatmapset.creator + )); e.image(res.beatmapset.covers.list2); - e.description(format!("**Link:** {}\n**Length:** {} **BPM:** {}\n**Difficulty:** {}:star:", res.url, res.total_length, res.bpm, res.difficulty_rating)); + e.description(format!( + "**Link:** {}\n**Length:** {} **BPM:** {}\n**Difficulty:** {}:star:", + res.url, res.total_length, res.bpm, res.difficulty_rating + )); e.footer(|f| { - f.text(format!("BM ID {} | BM Set ID {}\nCreated {}", res.id, res.beatmapset.id, res.beatmapset.submitted_date)); + f.text(format!( + "BM ID {} | BM Set ID {}\nCreated {}", + res.id, res.beatmapset.id, res.beatmapset.submitted_date + )); f }); e }); m - }).await?; + }) + .await?; Ok(()) } |