From d9250ec4c0355ee721beb823134bd93b0a3065c8 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 17 Aug 2022 23:00:49 +0200 Subject: [PATCH] Allow paginating through an embed --- src/commands/utils.rs | 101 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/src/commands/utils.rs b/src/commands/utils.rs index b947f5a..3130f20 100644 --- a/src/commands/utils.rs +++ b/src/commands/utils.rs @@ -10,7 +10,7 @@ pub async fn high_tier(ctx: Context<'_>) -> bool { 332337657113739265 => return true, // Testing _ => (), } - + match ctx.discord().cache.guild_channel(ctx.channel_id()) { None => return false, Some(chan) => match chan.parent_id { @@ -133,3 +133,102 @@ pub async fn paginate_string(ctx: Context<'_>, pages: Vec) -> Result<(), Ok(()) } + +pub async fn paginate_string_embed( + ctx: Context<'_>, + title: String, + pages: Vec, +) -> Result<(), Error> { + let uuid_command = ctx.id().to_string(); + let page_count = pages.len(); + + match pages.len() { + 0 => { + ctx.say("No data found :(").await?; + return Ok(()); + } + 1 => { + // ctx.say(pages.get(0).unwrap()).await?; + ctx.send(|m| m.embed(|e| e.title("WRC schedule").description(pages.get(0).unwrap()))) + .await?; + return Ok(()); + } + _ => {} + }; + + ctx.send(|m| { + // m.content(format!( + // "{}\n\nPage: {}/{}", + // pages.get(0).unwrap(), + // 1, + // page_count + // )) + m.embed(|e| { + e.title(format!("{} Page 1/{}", title, page_count)) + .description(pages.get(0).unwrap()) + }) + .components(|c| { + c.create_action_row(|ar| { + ar.create_button(|b| { + b.style(serenity::ButtonStyle::Primary) + .label("Previous page") + .custom_id(format!("{}_previous", uuid_command)) + }); + ar.create_button(|b| { + b.style(serenity::ButtonStyle::Primary) + .label("Next page") + .custom_id(format!("{}_next", uuid_command)) + }); + ar.create_button(|b| { + b.style(serenity::ButtonStyle::Secondary) + .label("Reset") + .custom_id(format!("{}_close", uuid_command)) + }) + }) + }) + }) + .await?; + // let interaction1 = if let ReplyHandle::Application { http, interaction } = msg.unwrap(){Some(interaction)} else {None}; + // let interaction = interaction1.unwrap(); + + let mut page = 0; + while let Some(mci) = serenity::CollectComponentInteraction::new(ctx.discord()) + // .author_id(ctx.author().id) + .channel_id(ctx.channel_id()) + .timeout(std::time::Duration::from_secs(1200)) + // .filter(move |mci| mci.data.custom_id == uuid_command.to_string()) + .await + { + if !mci.data.custom_id.contains(&uuid_command) { + continue; + } + + if mci.data.custom_id.contains("_previous") { + if page == 0 { + page = page_count - 1; + } else { + page = (page - 1) % page_count; + } + } else if mci.data.custom_id.contains("_next") { + page = (page + 1) % page_count; + } else if mci.data.custom_id.contains("_close") { + page = 0; + } + + let mut msg = mci.message.clone(); + msg.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title(format!("{} Page {}/{}", title, page + 1, page_count)) + .description(pages.get(page).unwrap()) + }) + }) + .await?; + + mci.create_interaction_response(ctx.discord(), |ir| { + ir.kind(serenity::InteractionResponseType::DeferredUpdateMessage) + }) + .await?; + } + + Ok(()) +}