From e9a6e5556939cb0e785019d32d0913b5c345b28d Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Thu, 20 Jun 2024 01:52:39 +0200 Subject: [PATCH] Reformat uploaded beatmap embed and send background --- youmubot-osu/src/discord/embeds.rs | 57 +++++++++++++++++------------- youmubot-osu/src/discord/hook.rs | 43 ++++++++++++++++++---- 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/youmubot-osu/src/discord/embeds.rs b/youmubot-osu/src/discord/embeds.rs index a890750..9fe9e72 100644 --- a/youmubot-osu/src/discord/embeds.rs +++ b/youmubot-osu/src/discord/embeds.rs @@ -4,6 +4,7 @@ use crate::{ models::{Beatmap, Difficulty, Mode, Mods, Rank, Score, User}, }; use serenity::{ + all::CreateAttachment, builder::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter}, utils::MessageBuilder, }; @@ -63,7 +64,7 @@ pub fn beatmap_offline_embed( b: &'_ crate::discord::oppai_cache::BeatmapContent, m: Mode, mods: Mods, -) -> Result { +) -> Result<(CreateEmbed, Vec)> { let bm = b.content.clone(); let metadata = b.metadata.clone(); let (info, pp) = b.get_possible_pp_with(m, mods)?; @@ -107,29 +108,37 @@ pub fn beatmap_offline_embed( total_length, } .apply_mods(mods, info.stars); - Ok( - CreateEmbed::new() - .title(beatmap_title( - &metadata.artist, - &metadata.title, - &metadata.version, - mods, - )) - .author({ - CreateEmbedAuthor::new(&metadata.creator) - .url(format!("https://osu.ppy.sh/users/{}", metadata.creator)) - }) - .color(0xffb6c1) - .field( - "Calculated pp", - format!( - "95%: **{:.2}**pp, 98%: **{:.2}**pp, 99%: **{:.2}**pp, 100%: **{:.2}**pp", - pp[0], pp[1], pp[2], pp[3] - ), - false, - ) - .field("Information", diff.format_info(m, mods, None), false), // .description(beatmap_description(b)) - ) + let mut embed = CreateEmbed::new() + .title(beatmap_title( + &metadata.artist, + &metadata.title, + &metadata.version, + mods, + )) + .author({ + CreateEmbedAuthor::new(&metadata.creator) + .url(format!("https://osu.ppy.sh/users/{}", metadata.creator)) + }) + .color(0xffb6c1) + .field( + "Calculated pp", + format!( + "95%: **{:.2}**pp, 98%: **{:.2}**pp, 99%: **{:.2}**pp, 100%: **{:.2}**pp", + pp[0], pp[1], pp[2], pp[3] + ), + false, + ) + .field("Information", diff.format_info(m, mods, None), false); + let mut attachments = Vec::new(); + if let Some(bg) = &b.beatmap_background { + embed = embed.thumbnail(format!("attachment://{}", bg.filename)); + attachments.push(CreateAttachment::bytes( + bg.content.clone().into_vec(), + bg.filename.clone(), + )); + } + + Ok((embed, attachments)) } // Some helper functions here diff --git a/youmubot-osu/src/discord/hook.rs b/youmubot-osu/src/discord/hook.rs index 9f95d82..80fbb39 100644 --- a/youmubot-osu/src/discord/hook.rs +++ b/youmubot-osu/src/discord/hook.rs @@ -101,16 +101,45 @@ pub fn dot_osu_hook<'a>( osu_embeds.extend(osz_embeds); if !osu_embeds.is_empty() { - msg.channel_id - .send_message( + let embed_len = osu_embeds.len(); + if embed_len == 1 { + let (embed, attachments) = osu_embeds.into_iter().next().unwrap(); + msg.channel_id + .send_message( + ctx, + CreateMessage::new() + .reference_message(msg) + .embed(embed) + .add_files(attachments) + .content("Attached beatmap".to_owned()), + ) + .await + .pls_ok(); + } else { + let osu_embeds = Arc::new(osu_embeds); + paginate_reply( + paginate_from_fn(|page, ctx, msg| { + let osu_embeds = osu_embeds.clone(); + Box::pin(async move { + let (embed, attachments) = &osu_embeds[page as usize]; + let mut edit = EditMessage::new() + .content(format!("Attached beatmaps ({}/{})", page + 1, embed_len)) + .embed(embed.clone()); + for att in attachments { + edit = edit.new_attachment(att.clone()); + } + msg.edit(&ctx, edit).await?; + Ok(true) + }) + }) + .with_page_count(embed_len), ctx, - CreateMessage::new() - .reference_message(msg) - .content(format!("{} attached beatmaps found", osu_embeds.len())) - .add_embeds(osu_embeds), + msg, + std::time::Duration::from_secs(180), ) .await - .ok(); + .pls_ok(); + } } Ok(())