Pagination fixes

This commit is contained in:
Xyon 2023-08-29 16:32:33 +01:00
parent 0000312bae
commit 90be789c6b
Signed by: xyon
GPG Key ID: DD18155D6B18078D
2 changed files with 14 additions and 17 deletions

View File

@ -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<String>) -> 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(())
}

View File

@ -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<CreateEmbed>) -> 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| {
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?;
}