osu: Add view beatmap button

This commit is contained in:
Natsu Kagami 2024-07-14 00:50:21 +02:00
parent 3f8198f19c
commit d48ea0c377
Signed by: nki
GPG key ID: 55A032EB38B49ADB
3 changed files with 75 additions and 16 deletions

View file

@ -9,14 +9,14 @@ use youmubot_prelude::*;
use crate::Mods; 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_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. /// Create an action row for score pages.
pub fn score_components() -> CreateActionRow { pub fn score_components() -> CreateActionRow {
CreateActionRow::Buttons(vec![check_button()]) CreateActionRow::Buttons(vec![check_button(), last_button()])
} }
/// Create an action row for score pages. /// 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 (msg, author) = (&*comp.message, comp.user.id);
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone(); let env = ctx.data.read().await.get::<OsuEnv>().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 user_id = super::to_user_id_query(None, &env, author).await?;
let scores = super::do_check(&env, &bm, Mods::NOMOD, &user_id).await?; let scores = super::do_check(&env, &bm, Mods::NOMOD, &user_id).await?;
@ -76,3 +78,58 @@ pub fn handle_check_button<'a>(
Ok(()) 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<Box<dyn Future<Output = Result<()>> + 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::<OsuEnv>().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(())
})
}

View file

@ -222,7 +222,8 @@ pub async fn show_leaderboard(ctx: &Context, msg: &Message, mut args: Args) -> C
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone(); let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
let (bm, _) = match super::load_beatmap(&env, msg).await { let (bm, _) =
match super::load_beatmap(&env, msg.channel_id, msg.referenced_message.as_ref()).await {
Some((bm, mods_def)) => { Some((bm, mods_def)) => {
let mods = args.find::<Mods>().ok().or(mods_def).unwrap_or(Mods::NOMOD); let mods = args.find::<Mods>().ok().or(mods_def).unwrap_or(Mods::NOMOD);
(bm, mods) (bm, mods)

View file

@ -167,7 +167,8 @@ async fn main() {
handler.push_hook(youmubot_osu::discord::hook); handler.push_hook(youmubot_osu::discord::hook);
handler.push_hook(youmubot_osu::discord::dot_osu_hook); handler.push_hook(youmubot_osu::discord::dot_osu_hook);
handler.push_hook(youmubot_osu::discord::score_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")] #[cfg(feature = "codeforces")]
handler.push_hook(youmubot_cf::InfoHook); handler.push_hook(youmubot_cf::InfoHook);