From d48ea0c377fcb2566011ab00ee9b889206bad855 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 14 Jul 2024 00:50:21 +0200 Subject: [PATCH] osu: Add view beatmap button --- youmubot-osu/src/discord/interaction.rs | 65 +++++++++++++++++++++++-- youmubot-osu/src/discord/server_rank.rs | 23 ++++----- youmubot/src/main.rs | 3 +- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/youmubot-osu/src/discord/interaction.rs b/youmubot-osu/src/discord/interaction.rs index fc5dabb..3305395 100644 --- a/youmubot-osu/src/discord/interaction.rs +++ b/youmubot-osu/src/discord/interaction.rs @@ -9,14 +9,14 @@ use youmubot_prelude::*; use crate::Mods; -use super::{display::ScoreListStyle, OsuEnv}; +use super::{display::ScoreListStyle, embeds::beatmap_embed, BeatmapWithMode, OsuEnv}; pub(super) const BTN_CHECK: &'static str = "youmubot_osu_btn_check"; -// pub(super) const BTN_LAST: &'static str = "youmubot_osu_btn_last"; +pub(super) const BTN_LAST: &'static str = "youmubot_osu_btn_last"; /// Create an action row for score pages. pub fn score_components() -> CreateActionRow { - CreateActionRow::Buttons(vec![check_button()]) + CreateActionRow::Buttons(vec![check_button(), last_button()]) } /// Create an action row for score pages. @@ -50,7 +50,9 @@ pub fn handle_check_button<'a>( let (msg, author) = (&*comp.message, comp.user.id); let env = ctx.data.read().await.get::().unwrap().clone(); - let (bm, _) = super::load_beatmap(&env, msg).await.unwrap(); + let (bm, _) = super::load_beatmap(&env, comp.channel_id, Some(msg)) + .await + .unwrap(); let user_id = super::to_user_id_query(None, &env, author).await?; let scores = super::do_check(&env, &bm, Mods::NOMOD, &user_id).await?; @@ -76,3 +78,58 @@ pub fn handle_check_button<'a>( Ok(()) }) } + +/// Creates a new check button. +pub fn last_button() -> CreateButton { + CreateButton::new(BTN_LAST) + .label("View Beatmap") + .emoji('🎼') + .style(serenity::all::ButtonStyle::Secondary) +} + +/// Implements the `last` button on scores and beatmaps. +pub fn handle_last_button<'a>( + ctx: &'a Context, + interaction: &'a Interaction, +) -> Pin> + Send + 'a>> { + Box::pin(async move { + let comp = match interaction.as_message_component() { + Some(comp) + if comp.data.custom_id == BTN_LAST + && matches!(comp.data.kind, ComponentInteractionDataKind::Button) => + { + comp + } + _ => return Ok(()), + }; + let msg = &*comp.message; + + let env = ctx.data.read().await.get::().unwrap().clone(); + + let (BeatmapWithMode(b, m), mods_def) = + super::load_beatmap(&env, comp.channel_id, Some(msg)) + .await + .unwrap(); + + let mods = mods_def.unwrap_or(Mods::NOMOD); + let info = env + .oppai + .get_beatmap(b.beatmap_id) + .await? + .get_possible_pp_with(m, mods)?; + comp.create_response( + &ctx, + serenity::all::CreateInteractionResponse::Message( + CreateInteractionResponseMessage::new() + .content("Here is the beatmap you requested!") + .embed(beatmap_embed(&b, m, mods, info)) + .components(vec![beatmap_components()]), + ), + ) + .await?; + // Save the beatmap... + super::cache::save_beatmap(&env, msg.channel_id, &BeatmapWithMode(b, m)).await?; + + Ok(()) + }) +} diff --git a/youmubot-osu/src/discord/server_rank.rs b/youmubot-osu/src/discord/server_rank.rs index d9fb40c..16c2517 100644 --- a/youmubot-osu/src/discord/server_rank.rs +++ b/youmubot-osu/src/discord/server_rank.rs @@ -222,17 +222,18 @@ pub async fn show_leaderboard(ctx: &Context, msg: &Message, mut args: Args) -> C let env = ctx.data.read().await.get::().unwrap().clone(); - let (bm, _) = match super::load_beatmap(&env, msg).await { - Some((bm, mods_def)) => { - let mods = args.find::().ok().or(mods_def).unwrap_or(Mods::NOMOD); - (bm, mods) - } - None => { - msg.reply(&ctx, "No beatmap queried on this channel.") - .await?; - return Ok(()); - } - }; + let (bm, _) = + match super::load_beatmap(&env, msg.channel_id, msg.referenced_message.as_ref()).await { + Some((bm, mods_def)) => { + let mods = args.find::().ok().or(mods_def).unwrap_or(Mods::NOMOD); + (bm, mods) + } + None => { + msg.reply(&ctx, "No beatmap queried on this channel.") + .await?; + return Ok(()); + } + }; let osu_client = env.client.clone(); diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index d13359f..c4143dd 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -167,7 +167,8 @@ async fn main() { handler.push_hook(youmubot_osu::discord::hook); handler.push_hook(youmubot_osu::discord::dot_osu_hook); handler.push_hook(youmubot_osu::discord::score_hook); - handler.push_interaction_hook(youmubot_osu::discord::interaction::handle_check_button) + handler.push_interaction_hook(youmubot_osu::discord::interaction::handle_check_button); + handler.push_interaction_hook(youmubot_osu::discord::interaction::handle_last_button) } #[cfg(feature = "codeforces")] handler.push_hook(youmubot_cf::InfoHook);