use crate::prelude::*; use futures::future::BoxFuture; use std::{borrow::Borrow, future::Future, sync::Arc}; use teloxide::types::MessageId; use tokio::sync::Mutex; #[derive(Clone, Copy)] pub struct MessageIds(pub ChatId, pub MessageId); impl MessageIds { #[inline] pub async fn delete(&self, bot: &Throttle) { let _ = bot.delete_message(self.0, self.1).await; } } impl> From for MessageIds { #[inline] fn from(value: T) -> Self { let value: &Message = value.borrow(); Self(value.chat.id, value.id) } } type DynHanlder = Box< dyn FnOnce( Throttle, Message, DatabaseConnection, MainDialogue, T, ) -> BoxFuture<'static, crate::Result<()>> + Send, >; pub struct Handler { pub func: Option>, pub previous: MessageIds, } pub type PackagedHandler = Arc>>; impl Handler { /// Convinience method to convert a simple async function and a previous message into PackagedHandler #[inline] pub fn new(f: H, previous: impl Into) -> PackagedHandler where H: FnOnce(Throttle, Message, DatabaseConnection, MainDialogue, T) -> F + Send + 'static, F: Future> + Send + 'static, { let handler = Self { func: Some(Box::new(|bot, msg, db, dialogue, val| { Box::pin(f(bot, msg, db, dialogue, val)) })), previous: previous.into(), }; Arc::new(Mutex::new(handler)) } }