More osu improvements (#11)

* Don't show DT when NC is on

* Allow leading + in mods parsing

* Extend Pagination

* Implement beatmapset display

* Move OkPrint to prelude

* Beatmapset display seems to work

* Put reaction handler into static

* Make clippy happy

* Add beatmapset caching and last --set

* Delay loading of beatmap info

* Simplify hook link handling

* Replies everywhere!

* Replies everywhereee!
This commit is contained in:
Natsu Kagami 2021-02-02 02:13:40 +09:00 committed by GitHub
parent 61a71b819c
commit bd845d9662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 450 additions and 156 deletions

View file

@ -8,7 +8,7 @@ use crate::{
use serenity::{
framework::standard::{
macros::{command, group},
Args, CommandError as Error, CommandResult,
Args, CommandResult,
},
model::channel::Message,
utils::MessageBuilder,
@ -20,6 +20,7 @@ mod announcer;
pub(crate) mod beatmap_cache;
mod cache;
mod db;
pub(crate) mod display;
pub(crate) mod embeds;
mod hook;
pub(crate) mod oppai_cache;
@ -292,7 +293,7 @@ fn to_user_id_query(
db.get(&id)
.cloned()
.map(|u| UserID::ID(u.id))
.ok_or(Error::from("No saved account found"))
.ok_or(Error::msg("No saved account found"))
}
enum Nth {
@ -306,7 +307,7 @@ impl FromStr for Nth {
if s == "--all" || s == "-a" || s == "##" {
Ok(Nth::All)
} else if !s.starts_with("#") {
Err(Error::from("Not an order"))
Err(Error::msg("Not an order"))
} else {
let v = s.split_at("#".len()).1.parse()?;
Ok(Nth::Nth(v))
@ -328,7 +329,7 @@ async fn list_plays<'a>(
const ITEMS_PER_PAGE: usize = 5;
let total_pages = (plays.len() + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE;
paginate_fn(
paginate_reply_fn(
move |page, ctx, msg| {
let plays = plays.clone();
Box::pin(async move {
@ -464,7 +465,7 @@ async fn list_plays<'a>(
})
},
ctx,
m.channel_id,
m,
std::time::Duration::from_secs(60),
)
.await?;
@ -488,7 +489,7 @@ pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu
let user = osu
.user(user, |f| f.mode(mode))
.await?
.ok_or(Error::from("User not found"))?;
.ok_or(Error::msg("User not found"))?;
match nth {
Nth::Nth(nth) => {
let recent_play = osu
@ -496,18 +497,18 @@ pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu
.await?
.into_iter()
.last()
.ok_or(Error::from("No such play"))?;
.ok_or(Error::msg("No such play"))?;
let beatmap = meta_cache.get_beatmap(recent_play.beatmap_id, mode).await?;
let content = oppai.get_beatmap(beatmap.beatmap_id).await?;
let beatmap_mode = BeatmapWithMode(beatmap, mode);
msg.channel_id
.send_message(&ctx, |m| {
m.content(format!(
"{}: here is the play that you requested",
msg.author
))
.embed(|m| score_embed(&recent_play, &beatmap_mode, &content, &user).build(m))
m.content(format!("Here is the play that you requested",))
.embed(|m| {
score_embed(&recent_play, &beatmap_mode, &content, &user).build(m)
})
.reference_message(msg)
})
.await?;
@ -524,17 +525,46 @@ pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu
Ok(())
}
/// Get beatmapset.
struct OptBeatmapset;
impl FromStr for OptBeatmapset {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"--set" | "-s" | "--beatmapset" => Ok(Self),
_ => Err(Error::msg("not opt beatmapset")),
}
}
}
#[command]
#[description = "Show information from the last queried beatmap."]
#[usage = "[mods = no mod]"]
#[max_args(1)]
#[usage = "[--set/-s/--beatmapset] / [mods = no mod]"]
#[max_args(2)]
pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let data = ctx.data.read().await;
let b = cache::get_beatmap(&*data, msg.channel_id)?;
let beatmapset = args.find::<OptBeatmapset>().is_ok();
match b {
Some(BeatmapWithMode(b, m)) => {
let mods = args.find::<Mods>().unwrap_or(Mods::NOMOD);
if beatmapset {
let beatmap_cache = data.get::<BeatmapMetaCache>().unwrap();
let beatmapset = beatmap_cache.get_beatmapset(b.beatmapset_id).await?;
display::display_beatmapset(
ctx,
beatmapset,
None,
Some(mods),
msg,
"Here is the beatmapset you requested!",
)
.await?;
return Ok(());
}
let info = data
.get::<BeatmapCache>()
.unwrap()
@ -544,11 +574,9 @@ pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.ok();
msg.channel_id
.send_message(&ctx, |f| {
f.content(format!(
"{}: here is the beatmap you requested!",
msg.author
))
.embed(|c| beatmap_embed(&b, m, mods, info, c))
f.content("Here is the beatmap you requested!")
.embed(|c| beatmap_embed(&b, m, mods, info, c))
.reference_message(msg)
})
.await?;
}
@ -594,7 +622,7 @@ pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
let user = osu
.user(user, |f| f)
.await?
.ok_or(Error::from("User not found"))?;
.ok_or(Error::msg("User not found"))?;
let scores = osu
.scores(b.beatmap_id, |f| f.user(UserID::ID(user.id)).mode(m))
.await?;
@ -646,7 +674,7 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let user = osu
.user(user, |f| f.mode(mode))
.await?
.ok_or(Error::from("User not found"))?;
.ok_or(Error::msg("User not found"))?;
match nth {
Nth::Nth(nth) => {
@ -659,7 +687,7 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let top_play = top_play
.into_iter()
.last()
.ok_or(Error::from("No such play"))?;
.ok_or(Error::msg("No such play"))?;
let beatmap = meta_cache.get_beatmap(top_play.beatmap_id, mode).await?;
let content = oppai.get_beatmap(beatmap.beatmap_id).await?;
let beatmap = BeatmapWithMode(beatmap, mode);