From fcf3ed60d2f4471157725f1e4a9b1b59d449bc29 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 12 May 2025 00:27:18 +0200 Subject: [PATCH] display should take a lazy score future --- youmubot-osu/src/discord/commands.rs | 6 +++--- youmubot-osu/src/discord/display.rs | 20 +++++++++++--------- youmubot-osu/src/discord/interaction.rs | 7 ++++++- youmubot-osu/src/discord/mod.rs | 13 +++++++++---- youmubot-osu/src/discord/server_rank.rs | 2 +- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/youmubot-osu/src/discord/commands.rs b/youmubot-osu/src/discord/commands.rs index c5bdf31..b38a98a 100644 --- a/youmubot-osu/src/discord/commands.rs +++ b/youmubot-osu/src/discord/commands.rs @@ -307,7 +307,7 @@ async fn handle_listing( let reply = ctx.clone().reply(&header).await?; style .display_scores( - plays.try_collect::>().await?, + plays.try_collect::>(), ctx.clone().serenity_context(), ctx.guild_id(), (reply, ctx).with_header(header), @@ -489,7 +489,7 @@ async fn check( style .display_scores( - scores, + future::ok(scores), ctx.clone().serenity_context(), ctx.guild_id(), (msg, ctx).with_header(header), @@ -612,7 +612,7 @@ async fn leaderboard( let reply = ctx.reply(header).await?; style .display_scores( - scores.into_iter().map(|s| s.score).collect(), + future::ok(scores.into_iter().map(|s| s.score).collect()), ctx.serenity_context(), Some(guild.id), (reply, ctx), diff --git a/youmubot-osu/src/discord/display.rs b/youmubot-osu/src/discord/display.rs index 74e0566..c87bdd4 100644 --- a/youmubot-osu/src/discord/display.rs +++ b/youmubot-osu/src/discord/display.rs @@ -76,6 +76,7 @@ mod scores { ) -> Result<()> { let env = ctx.data.read().await.get::().unwrap().clone(); let channel_id = on.get_message().await?.channel_id; + let scores = scores.await?; if scores.is_empty() { on.apply_edit(CreateReply::default().content("No plays found")) .await?; @@ -85,7 +86,7 @@ mod scores { paginate_with_first_message( Paginate { env, - scores: scores.await?, + scores, guild_id, channel_id, }, @@ -157,6 +158,7 @@ mod scores { pub mod table { use std::borrow::Cow; + use std::future::Future; use pagination::paginate_with_first_message; use serenity::all::{CreateActionRow, CreateAttachment}; @@ -170,27 +172,26 @@ mod scores { use crate::models::Score; pub async fn display_scores_as_file( - scores: Vec, + scores: impl Future>>, ctx: &Context, mut on: impl CanEdit, ) -> Result<()> { + let header = on.headers().unwrap_or("").to_owned(); + let content = format!("{}\n\nPreparing file...", header); + let preparing = on.apply_edit(CreateReply::default().content(content)); + let (_, scores) = future::try_join(preparing, scores).await?; if scores.is_empty() { on.apply_edit(CreateReply::default().content("No plays found")) .await?; return Ok(()); } - let header = on.headers().unwrap_or("").to_owned(); - let content = format!("{}\n\nPreparing file...", header); - let first_edit = on - .apply_edit(CreateReply::default().content(content)) - .map(|v| v.pls_ok()); let p = Paginate { env: ctx.data.read().await.get::().unwrap().clone(), header: header.clone(), scores, }; - let (_, content) = future::join(first_edit, p.to_table(0, p.scores.len())).await; + let content = p.to_table(0, p.scores.len()).await; on.apply_edit( CreateReply::default() .content(header) @@ -201,10 +202,11 @@ mod scores { } pub async fn display_scores_table( - scores: Vec, + scores: impl Future>>, ctx: &Context, mut on: impl CanEdit, ) -> Result<()> { + let scores = scores.await?; if scores.is_empty() { on.apply_edit(CreateReply::default().content("No plays found")) .await?; diff --git a/youmubot-osu/src/discord/interaction.rs b/youmubot-osu/src/discord/interaction.rs index b1900db..a2bdd0d 100644 --- a/youmubot-osu/src/discord/interaction.rs +++ b/youmubot-osu/src/discord/interaction.rs @@ -109,7 +109,12 @@ pub fn handle_check_button<'a>( let guild_id = comp.guild_id; ScoreListStyle::Grid - .display_scores(scores, &ctx, guild_id, (comp, ctx).with_header(header)) + .display_scores( + future::ok(scores), + &ctx, + guild_id, + (comp, ctx).with_header(header), + ) .await .pls_ok(); Ok(()) diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index 4f36513..098c2f7 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -685,7 +685,7 @@ pub async fn recent(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu let reply = msg.reply(ctx, &header).await?; style .display_scores( - plays.try_collect::>().await?, + plays.try_collect::>(), ctx, reply.guild_id, (reply, ctx).with_header(header), @@ -758,7 +758,7 @@ pub async fn pins(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult let reply = msg.reply(ctx, &header).await?; style .display_scores( - plays.try_collect::>().await?, + plays.try_collect::>(), ctx, reply.guild_id, (reply, ctx).with_header(header), @@ -1016,7 +1016,12 @@ pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul ); let reply = msg.reply(&ctx, &header).await?; style - .display_scores(scores, ctx, msg.guild_id, (reply, ctx).with_header(header)) + .display_scores( + future::ok(scores), + ctx, + msg.guild_id, + (reply, ctx).with_header(header), + ) .await?; Ok(()) @@ -1122,7 +1127,7 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult let reply = msg.reply(&ctx, &header).await?; style .display_scores( - plays.try_collect::>().await?, + plays.try_collect::>(), ctx, msg.guild_id, (reply, ctx).with_header(header), diff --git a/youmubot-osu/src/discord/server_rank.rs b/youmubot-osu/src/discord/server_rank.rs index 7d7d092..459c9e7 100644 --- a/youmubot-osu/src/discord/server_rank.rs +++ b/youmubot-osu/src/discord/server_rank.rs @@ -438,7 +438,7 @@ pub async fn show_leaderboard(ctx: &Context, msg: &Message, mut args: Args) -> C let reply = msg.reply(&ctx, header).await?; style .display_scores( - scores.into_iter().map(|s| s.score).collect(), + future::ok(scores.into_iter().map(|s| s.score).collect()), ctx, Some(guild), (reply, ctx),