Change menu pages to be wrapped in page type
The pages of a menu are now wrapped in an enum that contains either a builder function or a static CreateMessage. The builder function is used for the now playing message to always return the currently playing song instead of mapping a static song. This solves the problem that the old page is being rendered when the page is recreated because of being sticky. Signed-off-by: trivernis <trivernis@protonmail.com>pull/9/head
parent
411dd83240
commit
87cfba1bc8
@ -0,0 +1,40 @@
|
||||
use crate::error::SerenityUtilsResult;
|
||||
use serenity::builder::CreateMessage;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub type MessageBuildOutput<'b> =
|
||||
Pin<Box<dyn Future<Output = SerenityUtilsResult<CreateMessage<'b>>> + Send + 'b>>;
|
||||
pub type MessageBuilderFn<'b> = Arc<dyn Fn() -> MessageBuildOutput<'b> + Send + Sync>;
|
||||
|
||||
#[derive(Clone)]
|
||||
/// A page that stores a builder function for message pages
|
||||
/// or static pages
|
||||
pub enum Page<'b> {
|
||||
Builder(MessageBuilderFn<'b>),
|
||||
Static(CreateMessage<'b>),
|
||||
}
|
||||
|
||||
impl<'b> Page<'b> {
|
||||
/// Creates a new page with the given builder function
|
||||
pub fn new_builder<F: 'static>(builder_fn: F) -> Self
|
||||
where
|
||||
F: Fn() -> MessageBuildOutput<'b> + Send + Sync,
|
||||
{
|
||||
Self::Builder(Arc::new(builder_fn))
|
||||
}
|
||||
|
||||
/// Creates a new page with a static message
|
||||
pub fn new_static(page: CreateMessage<'b>) -> Self {
|
||||
Self::Static(page)
|
||||
}
|
||||
|
||||
/// Returns the CreateMessage of the page
|
||||
pub async fn get(&self) -> SerenityUtilsResult<CreateMessage<'b>> {
|
||||
match self {
|
||||
Page::Builder(b) => b().await,
|
||||
Page::Static(inner) => Ok(inner.clone()),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue