osu: Fix commands

This commit is contained in:
Natsu Kagami 2020-09-07 19:01:21 -04:00
parent 421cfd46af
commit b776a1d26e
Signed by: nki
GPG key ID: 73376E117CD20735
3 changed files with 376 additions and 303 deletions

View file

@ -5,7 +5,6 @@ use crate::{
request::{BeatmapRequestKind, UserID}, request::{BeatmapRequestKind, UserID},
Client as OsuHttpClient, Client as OsuHttpClient,
}; };
use rayon::prelude::*;
use serenity::{ use serenity::{
framework::standard::{ framework::standard::{
macros::{command, group}, macros::{command, group},
@ -14,7 +13,7 @@ use serenity::{
model::channel::Message, model::channel::Message,
utils::MessageBuilder, utils::MessageBuilder,
}; };
use std::str::FromStr; use std::{str::FromStr, sync::Arc};
use youmubot_prelude::*; use youmubot_prelude::*;
mod announcer; mod announcer;
@ -36,7 +35,7 @@ use server_rank::SERVER_RANK_COMMAND;
pub(crate) struct OsuClient; pub(crate) struct OsuClient;
impl TypeMapKey for OsuClient { impl TypeMapKey for OsuClient {
type Value = OsuHttpClient; type Value = Arc<OsuHttpClient>;
} }
/// Sets up the osu! command handling section. /// Sets up the osu! command handling section.
@ -50,9 +49,9 @@ impl TypeMapKey for OsuClient {
/// - Commands on the "osu" prefix /// - Commands on the "osu" prefix
/// - Hooks. Hooks are completely opt-in. /// - Hooks. Hooks are completely opt-in.
/// ///
pub fn setup( pub async fn setup(
path: &std::path::Path, path: &std::path::Path,
data: &mut ShareMap, data: &mut TypeMap,
announcers: &mut AnnouncerHandler, announcers: &mut AnnouncerHandler,
) -> CommandResult { ) -> CommandResult {
// Databases // Databases
@ -60,11 +59,10 @@ pub fn setup(
OsuLastBeatmap::insert_into(&mut *data, &path.join("last_beatmaps.yaml"))?; OsuLastBeatmap::insert_into(&mut *data, &path.join("last_beatmaps.yaml"))?;
// API client // API client
let http_client = data.get_cloned::<HTTPClient>(); let http_client = data.get::<HTTPClient>().unwrap().clone();
let osu_client = OsuHttpClient::new( let osu_client = Arc::new(OsuHttpClient::new(
http_client.clone(),
std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."), std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."),
); ));
data.insert::<OsuClient>(osu_client.clone()); data.insert::<OsuClient>(osu_client.clone());
data.insert::<oppai_cache::BeatmapCache>(oppai_cache::BeatmapCache::new(http_client)); data.insert::<oppai_cache::BeatmapCache>(oppai_cache::BeatmapCache::new(http_client));
data.insert::<beatmap_cache::BeatmapMetaCache>(beatmap_cache::BeatmapMetaCache::new( data.insert::<beatmap_cache::BeatmapMetaCache>(beatmap_cache::BeatmapMetaCache::new(
@ -72,7 +70,7 @@ pub fn setup(
)); ));
// Announcer // Announcer
announcers.add(announcer::ANNOUNCER_KEY, announcer::updates); announcers.add(announcer::ANNOUNCER_KEY, announcer::Announcer);
Ok(()) Ok(())
} }
@ -88,8 +86,8 @@ struct Osu;
#[description = "Receive information about an user in osu!std mode."] #[description = "Receive information about an user in osu!std mode."]
#[usage = "[username or user_id = your saved username]"] #[usage = "[username or user_id = your saved username]"]
#[max_args(1)] #[max_args(1)]
pub fn std(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { pub async fn std(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
get_user(ctx, msg, args, Mode::Std) get_user(ctx, msg, args, Mode::Std).await
} }
#[command] #[command]
@ -97,8 +95,8 @@ pub fn std(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
#[description = "Receive information about an user in osu!taiko mode."] #[description = "Receive information about an user in osu!taiko mode."]
#[usage = "[username or user_id = your saved username]"] #[usage = "[username or user_id = your saved username]"]
#[max_args(1)] #[max_args(1)]
pub fn taiko(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { pub async fn taiko(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
get_user(ctx, msg, args, Mode::Taiko) get_user(ctx, msg, args, Mode::Taiko).await
} }
#[command] #[command]
@ -106,8 +104,8 @@ pub fn taiko(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
#[description = "Receive information about an user in osu!catch mode."] #[description = "Receive information about an user in osu!catch mode."]
#[usage = "[username or user_id = your saved username]"] #[usage = "[username or user_id = your saved username]"]
#[max_args(1)] #[max_args(1)]
pub fn catch(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { pub async fn catch(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
get_user(ctx, msg, args, Mode::Catch) get_user(ctx, msg, args, Mode::Catch).await
} }
#[command] #[command]
@ -115,8 +113,8 @@ pub fn catch(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
#[description = "Receive information about an user in osu!mania mode."] #[description = "Receive information about an user in osu!mania mode."]
#[usage = "[username or user_id = your saved username]"] #[usage = "[username or user_id = your saved username]"]
#[max_args(1)] #[max_args(1)]
pub fn mania(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { pub async fn mania(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
get_user(ctx, msg, args, Mode::Mania) get_user(ctx, msg, args, Mode::Mania).await
} }
pub(crate) struct BeatmapWithMode(pub Beatmap, pub Mode); pub(crate) struct BeatmapWithMode(pub Beatmap, pub Mode);
@ -137,17 +135,15 @@ impl AsRef<Beatmap> for BeatmapWithMode {
#[description = "Save the given username as your username."] #[description = "Save the given username as your username."]
#[usage = "[username or user_id]"] #[usage = "[username or user_id]"]
#[num_args(1)] #[num_args(1)]
pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let osu = ctx.data.get_cloned::<OsuClient>(); let data = ctx.data.read().await;
let osu = data.get::<OsuClient>().unwrap();
let user = args.single::<String>()?; let user = args.single::<String>()?;
let user: Option<User> = osu.user(UserID::Auto(user), |f| f)?; let user: Option<User> = osu.user(UserID::Auto(user), |f| f).await?;
match user { match user {
Some(u) => { Some(u) => {
let db = OsuSavedUsers::open(&*ctx.data.read()); OsuSavedUsers::open(&*data).borrow_mut()?.insert(
let mut db = db.borrow_mut()?;
db.insert(
msg.author.id, msg.author.id,
OsuUser { OsuUser {
id: u.id, id: u.id,
@ -161,10 +157,11 @@ pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
.push("user has been set to ") .push("user has been set to ")
.push_mono_safe(u.username) .push_mono_safe(u.username)
.build(), .build(),
)?; )
.await?;
} }
None => { None => {
msg.reply(&ctx, "user not found...")?; msg.reply(&ctx, "user not found...").await?;
} }
} }
Ok(()) Ok(())
@ -187,7 +184,7 @@ impl FromStr for ModeArg {
fn to_user_id_query( fn to_user_id_query(
s: Option<UsernameArg>, s: Option<UsernameArg>,
data: &ShareMap, data: &TypeMap,
msg: &Message, msg: &Message,
) -> Result<UserID, Error> { ) -> Result<UserID, Error> {
let id = match s { let id = match s {
@ -223,71 +220,69 @@ impl FromStr for Nth {
} }
} }
fn list_plays(plays: Vec<Score>, mode: Mode, ctx: Context, m: &Message) -> CommandResult { async fn list_plays<'a>(
let watcher = ctx.data.get_cloned::<ReactionWatcher>(); plays: Vec<Score>,
let osu = ctx.data.get_cloned::<BeatmapMetaCache>(); mode: Mode,
let beatmap_cache = ctx.data.get_cloned::<BeatmapCache>(); ctx: &'a Context,
m: &'a Message,
) -> CommandResult {
let plays = Arc::new(plays);
if plays.is_empty() { if plays.is_empty() {
m.reply(&ctx, "No plays found")?; m.reply(&ctx, "No plays found").await?;
return Ok(()); return Ok(());
} }
let mut beatmaps: Vec<Option<String>> = vec![None; plays.len()];
const ITEMS_PER_PAGE: usize = 5; const ITEMS_PER_PAGE: usize = 5;
let total_pages = (plays.len() + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE; let total_pages = (plays.len() + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE;
watcher.paginate_fn( paginate(
ctx, move |page, ctx, msg| {
m.channel_id, let plays = plays.clone();
move |page, e| { Box::pin(async move {
let data = ctx.data.read().await;
let osu = data.get::<BeatmapMetaCache>().unwrap();
let beatmap_cache = data.get::<BeatmapCache>().unwrap();
let page = page as usize; let page = page as usize;
let start = page * ITEMS_PER_PAGE; let start = page * ITEMS_PER_PAGE;
let end = plays.len().min(start + ITEMS_PER_PAGE); let end = plays.len().min(start + ITEMS_PER_PAGE);
if start >= end { if start >= end {
return (e, Err(Error::from("No more pages"))); return Ok(false);
} }
let plays = &plays[start..end]; let plays = &plays[start..end];
let beatmaps = { let beatmaps = plays
let b = &mut beatmaps[start..end]; .iter()
b.par_iter_mut() .map(|play| async move {
.enumerate() let beatmap = osu.get_beatmap(play.beatmap_id, mode).await?;
.map(|(i, v)| { let stars = {
v.get_or_insert_with(|| { let b = beatmap_cache.get_beatmap(beatmap.beatmap_id).await?;
if let Some(b) = osu.get_beatmap(plays[i].beatmap_id, mode).ok() { mode.to_oppai_mode()
let stars = beatmap_cache .and_then(|mode| b.get_info_with(Some(mode), play.mods).ok())
.get_beatmap(b.beatmap_id)
.ok()
.and_then(|b| {
mode.to_oppai_mode().and_then(|mode| {
b.get_info_with(Some(mode), plays[i].mods).ok()
})
})
.map(|info| info.stars as f64) .map(|info| info.stars as f64)
.unwrap_or(b.difficulty.stars); .unwrap_or(beatmap.difficulty.stars)
format!( };
let r: Result<_> = Ok(format!(
"[{:.1}*] {} - {} [{}] ({})", "[{:.1}*] {} - {} [{}] ({})",
stars, stars,
b.artist, beatmap.artist,
b.title, beatmap.title,
b.difficulty_name, beatmap.difficulty_name,
b.short_link(Some(mode), Some(plays[i].mods)), beatmap.short_link(Some(mode), Some(play.mods)),
) ));
} else { r
"FETCH_FAILED".to_owned()
}
}) })
}) .collect::<stream::FuturesOrdered<_>>()
.collect::<Vec<_>>() .map(|v| v.unwrap_or("FETCH_FAILED".to_owned()))
}; .collect::<Vec<String>>();
let pp = plays let pp = plays
.iter() .iter()
.map(|p| { .map(|p| async move {
p.pp.map(|pp| format!("{:.2}pp", pp)) match p.pp.map(|pp| format!("{:.2}pp", pp)) {
.or_else(|| { Some(v) => Ok(v),
beatmap_cache.get_beatmap(p.beatmap_id).ok().and_then(|b| { None => {
mode.to_oppai_mode().and_then(|op| { let b = beatmap_cache.get_beatmap(p.beatmap_id).await?;
let r: Result<_> = Ok(mode
.to_oppai_mode()
.and_then(|op| {
b.get_pp_from( b.get_pp_from(
oppai_rs::Combo::NonFC { oppai_rs::Combo::NonFC {
max_combo: p.max_combo as u32, max_combo: p.max_combo as u32,
@ -300,11 +295,15 @@ fn list_plays(plays: Vec<Score>, mode: Mode, ctx: Context, m: &Message) -> Comma
.ok() .ok()
.map(|pp| format!("{:.2}pp [?]", pp)) .map(|pp| format!("{:.2}pp [?]", pp))
}) })
.unwrap_or("-".to_owned()));
r
}
}
}) })
}) .collect::<stream::FuturesOrdered<_>>()
.unwrap_or("-".to_owned()) .map(|v| v.unwrap_or("-".to_owned()))
}) .collect::<Vec<String>>();
.collect::<Vec<_>>(); let (beatmaps, pp) = future::join(beatmaps, pp).await;
let pw = pp.iter().map(|v| v.len()).max().unwrap_or(2); let pw = pp.iter().map(|v| v.len()).max().unwrap_or(2);
/*mods width*/ /*mods width*/
let mw = plays let mw = plays
@ -364,10 +363,16 @@ fn list_plays(plays: Vec<Score>, mode: Mode, ctx: Context, m: &Message) -> Comma
} else { } else {
m.push_line("[?] means pp was predicted by oppai-rs."); m.push_line("[?] means pp was predicted by oppai-rs.");
} }
(e.content(m.build()), Ok(())) msg.edit(ctx, |f| f.content(m.to_string())).await?;
Ok(true)
})
}, },
ctx,
m.channel_id,
std::time::Duration::from_secs(60), std::time::Duration::from_secs(60),
) )
.await?;
Ok(())
} }
#[command] #[command]
@ -375,44 +380,49 @@ fn list_plays(plays: Vec<Score>, mode: Mode, ctx: Context, m: &Message) -> Comma
#[usage = "#[the nth recent play = --all] / [mode (std, taiko, mania, catch) = std] / [username / user id = your saved id]"] #[usage = "#[the nth recent play = --all] / [mode (std, taiko, mania, catch) = std] / [username / user id = your saved id]"]
#[example = "#1 / taiko / natsukagami"] #[example = "#1 / taiko / natsukagami"]
#[max_args(3)] #[max_args(3)]
pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let data = ctx.data.read().await;
let nth = args.single::<Nth>().unwrap_or(Nth::All); let nth = args.single::<Nth>().unwrap_or(Nth::All);
let mode = args.single::<ModeArg>().unwrap_or(ModeArg(Mode::Std)).0; let mode = args.single::<ModeArg>().unwrap_or(ModeArg(Mode::Std)).0;
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?; let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*data, msg)?;
let osu = ctx.data.get_cloned::<OsuClient>(); let osu = data.get::<OsuClient>().unwrap();
let meta_cache = ctx.data.get_cloned::<BeatmapMetaCache>(); let meta_cache = data.get::<BeatmapMetaCache>().unwrap();
let oppai = ctx.data.get_cloned::<BeatmapCache>(); let oppai = data.get::<BeatmapCache>().unwrap();
let user = osu let user = osu
.user(user, |f| f.mode(mode))? .user(user, |f| f.mode(mode))
.await?
.ok_or(Error::from("User not found"))?; .ok_or(Error::from("User not found"))?;
match nth { match nth {
Nth::Nth(nth) => { Nth::Nth(nth) => {
let recent_play = osu let recent_play = osu
.user_recent(UserID::ID(user.id), |f| f.mode(mode).limit(nth))? .user_recent(UserID::ID(user.id), |f| f.mode(mode).limit(nth))
.await?
.into_iter() .into_iter()
.last() .last()
.ok_or(Error::from("No such play"))?; .ok_or(Error::from("No such play"))?;
let beatmap = meta_cache let beatmap = meta_cache.get_beatmap(recent_play.beatmap_id, mode).await?;
.get_beatmap(recent_play.beatmap_id, mode) let content = oppai.get_beatmap(beatmap.beatmap_id).await?;
.unwrap();
let content = oppai.get_beatmap(beatmap.beatmap_id)?;
let beatmap_mode = BeatmapWithMode(beatmap, mode); let beatmap_mode = BeatmapWithMode(beatmap, mode);
msg.channel_id.send_message(&ctx, |m| { msg.channel_id
.send_message(&ctx, |m| {
m.content(format!( m.content(format!(
"{}: here is the play that you requested", "{}: here is the play that you requested",
msg.author msg.author
)) ))
.embed(|m| score_embed(&recent_play, &beatmap_mode, &content, &user, None, m)) .embed(|m| score_embed(&recent_play, &beatmap_mode, &content, &user, None, m))
})?; })
.await?;
// Save the beatmap... // Save the beatmap...
cache::save_beatmap(&*ctx.data.read(), msg.channel_id, &beatmap_mode)?; cache::save_beatmap(&*data, msg.channel_id, &beatmap_mode)?;
} }
Nth::All => { Nth::All => {
let plays = osu.user_recent(UserID::ID(user.id), |f| f.mode(mode).limit(50))?; let plays = osu
list_plays(plays, mode, ctx.clone(), msg)?; .user_recent(UserID::ID(user.id), |f| f.mode(mode).limit(50))
.await?;
list_plays(plays, mode, ctx, msg).await?;
} }
} }
Ok(()) Ok(())
@ -422,28 +432,33 @@ pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
#[description = "Show information from the last queried beatmap."] #[description = "Show information from the last queried beatmap."]
#[usage = "[mods = no mod]"] #[usage = "[mods = no mod]"]
#[max_args(1)] #[max_args(1)]
pub fn last(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let b = cache::get_beatmap(&*ctx.data.read(), msg.channel_id)?; let data = ctx.data.read().await;
let b = cache::get_beatmap(&*data, msg.channel_id)?;
match b { match b {
Some(BeatmapWithMode(b, m)) => { Some(BeatmapWithMode(b, m)) => {
let mods = args.find::<Mods>().unwrap_or(Mods::NOMOD); let mods = args.find::<Mods>().unwrap_or(Mods::NOMOD);
let info = ctx let info = data
.data .get::<BeatmapCache>()
.get_cloned::<BeatmapCache>() .unwrap()
.get_beatmap(b.beatmap_id)? .get_beatmap(b.beatmap_id)
.await?
.get_info_with(m.to_oppai_mode(), mods) .get_info_with(m.to_oppai_mode(), mods)
.ok(); .ok();
msg.channel_id.send_message(&ctx, |f| { msg.channel_id
.send_message(&ctx, |f| {
f.content(format!( f.content(format!(
"{}: here is the beatmap you requested!", "{}: here is the beatmap you requested!",
msg.author msg.author
)) ))
.embed(|c| beatmap_embed(&b, m, mods, info, c)) .embed(|c| beatmap_embed(&b, m, mods, info, c))
})?; })
.await?;
} }
None => { None => {
msg.reply(&ctx, "No beatmap was queried on this channel.")?; msg.reply(&ctx, "No beatmap was queried on this channel.")
.await?;
} }
} }
@ -454,36 +469,43 @@ pub fn last(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
#[aliases("c", "chk")] #[aliases("c", "chk")]
#[description = "Check your own or someone else's best record on the last beatmap."] #[description = "Check your own or someone else's best record on the last beatmap."]
#[max_args(1)] #[max_args(1)]
pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let bm = cache::get_beatmap(&*ctx.data.read(), msg.channel_id)?; let data = ctx.data.read().await;
let bm = cache::get_beatmap(&*data, msg.channel_id)?;
match bm { match bm {
None => { None => {
msg.reply(&ctx, "No beatmap queried on this channel.")?; msg.reply(&ctx, "No beatmap queried on this channel.")
.await?;
} }
Some(bm) => { Some(bm) => {
let b = &bm.0; let b = &bm.0;
let m = bm.1; let m = bm.1;
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?; let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*data, msg)?;
let osu = ctx.data.get_cloned::<OsuClient>(); let osu = data.get::<OsuClient>().unwrap();
let oppai = ctx.data.get_cloned::<BeatmapCache>(); let oppai = data.get::<BeatmapCache>().unwrap();
let content = oppai.get_beatmap(b.beatmap_id)?; let content = oppai.get_beatmap(b.beatmap_id).await?;
let user = osu let user = osu
.user(user, |f| f)? .user(user, |f| f)
.await?
.ok_or(Error::from("User not found"))?; .ok_or(Error::from("User not found"))?;
let scores = osu.scores(b.beatmap_id, |f| f.user(UserID::ID(user.id)).mode(m))?; let scores = osu
.scores(b.beatmap_id, |f| f.user(UserID::ID(user.id)).mode(m))
.await?;
if scores.is_empty() { if scores.is_empty() {
msg.reply(&ctx, "No scores found")?; msg.reply(&ctx, "No scores found").await?;
} }
for score in scores.into_iter() { for score in scores.into_iter() {
msg.channel_id.send_message(&ctx, |c| { msg.channel_id
.send_message(&ctx, |c| {
c.embed(|m| score_embed(&score, &bm, &content, &user, None, m)) c.embed(|m| score_embed(&score, &bm, &content, &user, None, m))
})?; })
.await?;
} }
} }
} }
@ -493,27 +515,31 @@ pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
#[command] #[command]
#[description = "Get the n-th top record of an user."] #[description = "Get the n-th top record of an user."]
#[usage = "#[n-th = --all] / [mode (std, taiko, catch, mania) = std / [username or user_id = your saved user id]"] #[usage = "[mode (std, taiko, catch, mania)] = std / #[n-th = --all] / [username or user_id = your saved user id]"]
#[example = "#2 / taiko / natsukagami"] #[example = "taiko / #2 / natsukagami"]
#[max_args(3)] #[max_args(3)]
pub fn top(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let data = ctx.data.read().await;
let nth = args.single::<Nth>().unwrap_or(Nth::All); let nth = args.single::<Nth>().unwrap_or(Nth::All);
let mode = args let mode = args
.single::<ModeArg>() .single::<ModeArg>()
.map(|ModeArg(t)| t) .map(|ModeArg(t)| t)
.unwrap_or(Mode::Std); .unwrap_or(Mode::Std);
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?; let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*data, msg)?;
let osu = ctx.data.get_cloned::<OsuClient>(); let osu = data.get::<OsuClient>().unwrap();
let oppai = ctx.data.get_cloned::<BeatmapCache>(); let oppai = data.get::<BeatmapCache>().unwrap();
let user = osu let user = osu
.user(user, |f| f.mode(mode))? .user(user, |f| f.mode(mode))
.await?
.ok_or(Error::from("User not found"))?; .ok_or(Error::from("User not found"))?;
match nth { match nth {
Nth::Nth(nth) => { Nth::Nth(nth) => {
let top_play = osu.user_best(UserID::ID(user.id), |f| f.mode(mode).limit(nth))?; let top_play = osu
.user_best(UserID::ID(user.id), |f| f.mode(mode).limit(nth))
.await?;
let rank = top_play.len() as u8; let rank = top_play.len() as u8;
@ -524,66 +550,80 @@ pub fn top(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let beatmap = osu let beatmap = osu
.beatmaps(BeatmapRequestKind::Beatmap(top_play.beatmap_id), |f| { .beatmaps(BeatmapRequestKind::Beatmap(top_play.beatmap_id), |f| {
f.mode(mode, true) f.mode(mode, true)
})? })
.await?
.into_iter() .into_iter()
.next() .next()
.unwrap(); .unwrap();
let content = oppai.get_beatmap(beatmap.beatmap_id)?; let content = oppai.get_beatmap(beatmap.beatmap_id).await?;
let beatmap = BeatmapWithMode(beatmap, mode); let beatmap = BeatmapWithMode(beatmap, mode);
msg.channel_id.send_message(&ctx, |m| { msg.channel_id
.send_message(&ctx, |m| {
m.content(format!( m.content(format!(
"{}: here is the play that you requested", "{}: here is the play that you requested",
msg.author msg.author
)) ))
.embed(|m| score_embed(&top_play, &beatmap, &content, &user, Some(rank), m)) .embed(|m| score_embed(&top_play, &beatmap, &content, &user, Some(rank), m))
})?; })
.await?;
// Save the beatmap... // Save the beatmap...
cache::save_beatmap(&*ctx.data.read(), msg.channel_id, &beatmap)?; cache::save_beatmap(&*data, msg.channel_id, &beatmap)?;
} }
Nth::All => { Nth::All => {
let plays = osu.user_best(UserID::ID(user.id), |f| f.mode(mode).limit(100))?; let plays = osu
list_plays(plays, mode, ctx.clone(), msg)?; .user_best(UserID::ID(user.id), |f| f.mode(mode).limit(100))
.await?;
list_plays(plays, mode, ctx, msg).await?;
} }
} }
Ok(()) Ok(())
} }
fn get_user(ctx: &mut Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult { async fn get_user(ctx: &Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult {
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?; let data = ctx.data.read().await;
let osu = ctx.data.get_cloned::<OsuClient>(); let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*data, msg)?;
let cache = ctx.data.get_cloned::<BeatmapMetaCache>(); let osu = data.get::<OsuClient>().unwrap();
let user = osu.user(user, |f| f.mode(mode))?; let cache = data.get::<BeatmapMetaCache>().unwrap();
let oppai = ctx.data.get_cloned::<BeatmapCache>(); let user = osu.user(user, |f| f.mode(mode)).await?;
let oppai = data.get::<BeatmapCache>().unwrap();
match user { match user {
Some(u) => { Some(u) => {
let best = osu let best = match osu
.user_best(UserID::ID(u.id), |f| f.limit(1).mode(mode))? .user_best(UserID::ID(u.id), |f| f.limit(1).mode(mode))
.await?
.into_iter() .into_iter()
.next() .next()
.map(|m| -> Result<_, Error> { {
let beatmap = cache.get_beatmap(m.beatmap_id, mode)?; Some(m) => {
let info = mode let beatmap = cache.get_beatmap(m.beatmap_id, mode).await?;
.to_oppai_mode() let info = match mode.to_oppai_mode() {
.map(|mode| -> Result<_, Error> { Some(mode) => Some(
Ok(oppai oppai
.get_beatmap(m.beatmap_id)? .get_beatmap(m.beatmap_id)
.get_info_with(Some(mode), m.mods)?) .await?
}) .get_info_with(Some(mode), m.mods)?,
.transpose()?; ),
Ok((m, BeatmapWithMode(beatmap, mode), info)) None => None,
}) };
.transpose()?; Some((m, BeatmapWithMode(beatmap, mode), info))
msg.channel_id.send_message(&ctx, |m| { }
None => None,
};
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, best, m)) .embed(|m| user_embed(u, best, m))
}) })
.await?;
} }
None => msg.reply(&ctx, "🔍 user not found!"), None => {
}?; msg.reply(&ctx, "🔍 user not found!").await?;
}
};
Ok(()) Ok(())
} }

View file

@ -1,8 +1,7 @@
use super::{db::OsuSavedUsers, ModeArg}; use super::{db::OsuSavedUsers, ModeArg};
use crate::models::Mode; use crate::models::Mode;
use serenity::{ use serenity::{
builder::EditMessage, framework::standard::{macros::command, Args, CommandResult},
framework::standard::{macros::command, Args, CommandError as Error, CommandResult},
model::channel::Message, model::channel::Message,
utils::MessageBuilder, utils::MessageBuilder,
}; };
@ -15,15 +14,15 @@ const ITEMS_PER_PAGE: usize = 10;
#[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"] #[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"]
#[max_args(1)] #[max_args(1)]
#[only_in(guilds)] #[only_in(guilds)]
pub fn server_rank(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult { pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandResult {
let data = ctx.data.read().await;
let mode = args.single::<ModeArg>().map(|v| v.0).unwrap_or(Mode::Std); let mode = args.single::<ModeArg>().map(|v| v.0).unwrap_or(Mode::Std);
let guild = m.guild_id.expect("Guild-only command"); let guild = m.guild_id.expect("Guild-only command");
let users = OsuSavedUsers::open(&*ctx.data.read()) let users = OsuSavedUsers::open(&*data).borrow()?.clone();
.borrow() let users = users
.expect("DB initialized") .into_iter()
.iter() .map(|(user_id, osu_user)| async move {
.filter_map(|(user_id, osu_user)| { guild.member(&ctx, user_id).await.ok().and_then(|member| {
guild.member(&ctx, user_id).ok().and_then(|member| {
osu_user osu_user
.pp .pp
.get(mode as usize) .get(mode as usize)
@ -32,7 +31,10 @@ pub fn server_rank(ctx: &mut Context, m: &Message, mut args: Args) -> CommandRes
.map(|pp| (pp, member.distinct(), osu_user.last_update.clone())) .map(|pp| (pp, member.distinct(), osu_user.last_update.clone()))
}) })
}) })
.collect::<Vec<_>>(); .collect::<stream::FuturesUnordered<_>>()
.filter_map(|v| future::ready(v))
.collect::<Vec<_>>()
.await;
let last_update = users.iter().map(|(_, _, a)| a).min().cloned(); let last_update = users.iter().map(|(_, _, a)| a).min().cloned();
let mut users = users let mut users = users
.into_iter() .into_iter()
@ -41,18 +43,21 @@ pub fn server_rank(ctx: &mut Context, m: &Message, mut args: Args) -> CommandRes
users.sort_by(|(a, _), (b, _)| (*b).partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)); users.sort_by(|(a, _), (b, _)| (*b).partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
if users.is_empty() { if users.is_empty() {
m.reply(&ctx, "No saved users in the current server...")?; m.reply(&ctx, "No saved users in the current server...")
.await?;
return Ok(()); return Ok(());
} }
let users = std::sync::Arc::new(users);
let last_update = last_update.unwrap(); let last_update = last_update.unwrap();
ctx.data.get_cloned::<ReactionWatcher>().paginate_fn( paginate(
ctx.clone(), move |page: u8, ctx: &Context, m: &mut Message| {
m.channel_id, let users = users.clone();
move |page: u8, e: &mut EditMessage| { Box::pin(async move {
let start = (page as usize) * ITEMS_PER_PAGE; let start = (page as usize) * ITEMS_PER_PAGE;
let end = (start + ITEMS_PER_PAGE).min(users.len()); let end = (start + ITEMS_PER_PAGE).min(users.len());
if start >= end { if start >= end {
return (e, Err(Error("No more items".to_owned()))); return Ok(false);
} }
let total_len = users.len(); let total_len = users.len();
let users = &users[start..end]; let users = &users[start..end];
@ -77,10 +82,15 @@ pub fn server_rank(ctx: &mut Context, m: &Message, mut args: Args) -> CommandRes
(total_len + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE, (total_len + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE,
last_update.to_rfc2822() last_update.to_rfc2822()
)); ));
(e.content(content.build()), Ok(())) m.edit(ctx, |f| f.content(content.to_string())).await?;
Ok(true)
})
}, },
ctx,
m.channel_id,
std::time::Duration::from_secs(60), std::time::Duration::from_secs(60),
)?; )
.await?;
Ok(()) Ok(())
} }

View file

@ -9,14 +9,30 @@ use models::*;
use request::builders::*; use request::builders::*;
use request::*; use request::*;
use reqwest::{Client as HTTPClient, RequestBuilder, Response}; use reqwest::{Client as HTTPClient, RequestBuilder, Response};
use std::convert::TryInto; use std::{convert::TryInto, sync::Arc};
use tower;
use youmubot_prelude::*; use youmubot_prelude::*;
/// The number of requests per minute to the osu! server.
const REQUESTS_PER_MINUTE: u64 = 50;
type BoxedResp =
std::pin::Pin<Box<dyn future::Future<Output = Result<Response, reqwest::Error>> + Send>>;
/// Client is the client that will perform calls to the osu! api server. /// Client is the client that will perform calls to the osu! api server.
#[derive(Debug)]
pub struct Client { pub struct Client {
http_client: RwLock<
Box<
dyn tower::Service<
reqwest::Request,
Response = Response,
Error = reqwest::Error,
Future = BoxedResp,
> + Send
+ Sync,
>,
>,
client: Arc<HTTPClient>,
key: String, key: String,
client: HTTPClient,
} }
fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::Error> { fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::Error> {
@ -31,17 +47,24 @@ fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::
impl Client { impl Client {
/// Create a new client from the given API key. /// Create a new client from the given API key.
pub fn new(http_client: HTTPClient, key: String) -> Client { pub fn new(key: String) -> Client {
let http_client = Arc::new(HTTPClient::new());
let _http = http_client.clone();
let srv = tower::ServiceBuilder::new()
.rate_limit(REQUESTS_PER_MINUTE, std::time::Duration::from_secs(60))
.service(tower::service_fn(move |req| -> BoxedResp {
Box::pin(_http.execute(req))
}));
Client { Client {
key, key,
http_client: RwLock::new(Box::new(srv)),
client: http_client, client: http_client,
} }
} }
async fn build_request(&self, r: RequestBuilder) -> Result<Response> { async fn build_request(&self, r: RequestBuilder) -> Result<Response> {
let v = r.query(&[("k", &*self.key)]).build()?; let v = r.query(&[("k", &*self.key)]).build()?;
// dbg!(v.url()); Ok(self.http_client.write().await.call(v).await?)
Ok(self.client.execute(v).await?)
} }
pub async fn beatmaps( pub async fn beatmaps(