From 90be789c6bd9f6a1fdde1dc51c99a12fbf9ff1d2 Mon Sep 17 00:00:00 2001 From: Xyon Date: Tue, 29 Aug 2023 16:32:33 +0100 Subject: [PATCH] Pagination fixes --- src/commands/weather.rs | 7 ++----- src/helpers/mod.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/commands/weather.rs b/src/commands/weather.rs index 79d7899..5acc669 100644 --- a/src/commands/weather.rs +++ b/src/commands/weather.rs @@ -7,6 +7,7 @@ use crate::{ManifoldCommand, ManifoldContext}; use crate::models::weather::{Weather, WeatherForecastRequestResponse}; use crate::models::user::UserInfo; use crate::error::ManifoldResult; +use crate::helpers::paginate; #[poise::command(slash_command, prefix_command, aliases("w"))] async fn weather(ctx: ManifoldContext<'_>, #[rest] #[description="Location to look up weather for"] location: Option) -> ManifoldResult<()> { @@ -67,11 +68,7 @@ async fn weather(ctx: ManifoldContext<'_>, #[rest] #[description="Location to lo }); } - my_message.edit(ctx, |m| { - m.content(""); - m.embeds.extend(pages); - m - }).await?; + paginate(ctx, my_message, pages).await?; Ok(()) } diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 5c6c2cd..1a9f776 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1,8 +1,9 @@ use crate::error::ManifoldResult; use crate::ManifoldContext; -use poise::serenity_prelude; +use poise::{ReplyHandle, serenity_prelude}; +use poise::serenity_prelude::CreateEmbed; -pub async fn paginate(ctx: ManifoldContext<'_>, pages: &[&str]) -> ManifoldResult<()> { +pub async fn paginate(ctx: ManifoldContext<'_>, message: ReplyHandle<'_>, pages: Vec) -> ManifoldResult<()> { // Define some unique identifiers for the navigation buttons let ctx_id = ctx.id(); let prev_button_id = format!("{}prev", ctx.id()); @@ -10,16 +11,15 @@ pub async fn paginate(ctx: ManifoldContext<'_>, pages: &[&str]) -> ManifoldResul // Send the embed with the first page as content let mut current_page = 0; - ctx.send(|b| { - b.embed(|b| b.description(pages[current_page])) - .components(|b| { - b.create_action_row(|b| { - b.create_button(|b| b.custom_id(&prev_button_id).emoji('◀')) - .create_button(|b| b.custom_id(&next_button_id).emoji('▶')) - }) + message.edit(ctx, |m| { + m.embeds = vec![pages[current_page].to_owned()]; + m.components(|b| { + b.create_action_row(|b| { + b.create_button(|b| b.custom_id(&prev_button_id).emoji('◀')) + .create_button(|b| b.custom_id(&next_button_id).emoji('▶')) }) - }) - .await?; + }) + }).await?; // Loop through incoming interactions with the navigation buttons while let Some(press) = serenity_prelude::CollectComponentInteraction::new(ctx) @@ -47,7 +47,7 @@ pub async fn paginate(ctx: ManifoldContext<'_>, pages: &[&str]) -> ManifoldResul press .create_interaction_response(ctx, |b| { b.kind(serenity_prelude::InteractionResponseType::UpdateMessage) - .interaction_response_data(|b| b.embed(|b| b.description(pages[current_page]))) + .interaction_response_data(|b| b.set_embeds(vec![pages[current_page.clone()].to_owned()])) }) .await?; }