Show best record in user requests

This commit is contained in:
Natsu Kagami 2020-01-08 12:56:31 +09:00
parent f1742664c4
commit f7063a2992
2 changed files with 49 additions and 11 deletions

View file

@ -81,8 +81,9 @@ mod duration {
(minutes, "minute"), (minutes, "minute"),
(seconds, "second"), (seconds, "second"),
]; ];
let count = f.precision().unwrap_or(formats.len());
let mut first = true; let mut first = true;
for (val, counter) in formats.into_iter() { for (val, counter) in formats.into_iter().skip_while(|(a, _)| *a == 0).take(count) {
if *val > 0 { if *val > 0 {
write!( write!(
f, f,

View file

@ -1,5 +1,6 @@
use crate::commands::args::Duration; use crate::commands::args::Duration;
use crate::http; use crate::http;
use chrono::Utc;
use serenity::{ use serenity::{
builder::CreateEmbed, builder::CreateEmbed,
framework::standard::{ framework::standard::{
@ -8,10 +9,11 @@ use serenity::{
}, },
model::channel::Message, model::channel::Message,
prelude::*, prelude::*,
utils::MessageBuilder,
}; };
use youmubot_osu::{ use youmubot_osu::{
models::{Mode, User}, models::{Beatmap, Mode, Score, User},
request::UserID, request::{BeatmapRequestKind, UserID},
}; };
mod hook; mod hook;
@ -70,19 +72,34 @@ fn get_user(ctx: &mut Context, msg: &Message, mut args: Args, mode: Mode) -> Com
let osu = data.get::<http::Osu>().unwrap(); let osu = data.get::<http::Osu>().unwrap();
let user = osu.user(reqwest, UserID::Auto(username), |f| f.mode(mode))?; let user = osu.user(reqwest, UserID::Auto(username), |f| f.mode(mode))?;
match user { match user {
Some(u) => msg.channel_id.send_message(&ctx, |m| { Some(u) => {
let best = osu
.user_best(reqwest, UserID::ID(u.id), |f| f.limit(1).mode(mode))?
.into_iter()
.next()
.map(|m| {
osu.beatmaps(reqwest, BeatmapRequestKind::Beatmap(m.beatmap_id), |f| f)
.map(|map| (m, map.into_iter().next().unwrap()))
})
.transpose()?;
msg.channel_id.send_message(&ctx, |m| {
m.content(format!( m.content(format!(
"{}: here is the user that you requested", "{}: here is the user that you requested",
msg.author msg.author
)) ))
.embed(|m| user_embed(u, m)) .embed(|m| user_embed(u, best, m))
}), })
}
None => msg.reply(&ctx, "🔍 user not found!"), None => msg.reply(&ctx, "🔍 user not found!"),
}?; }?;
Ok(()) Ok(())
} }
fn user_embed(u: User, m: &mut CreateEmbed) -> &mut CreateEmbed { fn user_embed<'a>(
u: User,
best: Option<(Score, Beatmap)>,
m: &'a mut CreateEmbed,
) -> &'a mut CreateEmbed {
m.title(u.username) m.title(u.username)
.url(format!("https://osu.ppy.sh/users/{}", u.id)) .url(format!("https://osu.ppy.sh/users/{}", u.id))
.color(0xffb6c1) .color(0xffb6c1)
@ -122,4 +139,24 @@ fn user_embed(u: User, m: &mut CreateEmbed) -> &mut CreateEmbed {
), ),
false, false,
) )
.fields(best.map(|(v, map)| {
(
"Best Record",
MessageBuilder::new()
.push_bold(format!("{:.2}pp", v.pp))
.push(" - ")
.push_line(format!("{:.1} ago", Duration(Utc::now() - v.date)))
.push("on ")
.push(format!(
"[{} - {}]({})",
MessageBuilder::new().push_bold_safe(&map.artist).build(),
MessageBuilder::new().push_bold_safe(&map.title).build(),
map.link()
))
.push(format!(" [{}]", map.difficulty_name))
.push(format!(" ({:.1}⭐)", map.difficulty.stars))
.build(),
false,
)
}))
} }