63 lines
1.6 KiB
Rust
63 lines
1.6 KiB
Rust
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<Bot>) {
|
|
let _ = bot.delete_message(self.0, self.1).await;
|
|
}
|
|
}
|
|
|
|
impl<T: Borrow<Message>> From<T> for MessageIds {
|
|
#[inline]
|
|
fn from(value: T) -> Self {
|
|
let value: &Message = value.borrow();
|
|
Self(value.chat.id, value.id)
|
|
}
|
|
}
|
|
|
|
type DynHanlder<T> = Box<
|
|
dyn FnOnce(
|
|
Throttle<Bot>,
|
|
Message,
|
|
DatabaseConnection,
|
|
MainDialogue,
|
|
T,
|
|
) -> BoxFuture<'static, crate::Result<()>>
|
|
+ Send,
|
|
>;
|
|
|
|
pub struct Handler<T> {
|
|
pub func: Option<DynHanlder<T>>,
|
|
|
|
pub previous: MessageIds,
|
|
}
|
|
|
|
pub type PackagedHandler<T> = Arc<Mutex<Handler<T>>>;
|
|
|
|
impl<T> Handler<T> {
|
|
/// Convinience method to convert a simple async function and a previous message into PackagedHandler
|
|
#[inline]
|
|
pub fn new<H, F>(f: H, previous: impl Into<MessageIds>) -> PackagedHandler<T>
|
|
where
|
|
H: FnOnce(Throttle<Bot>, Message, DatabaseConnection, MainDialogue, T) -> F
|
|
+ Send
|
|
+ 'static,
|
|
F: Future<Output = crate::Result<()>> + 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))
|
|
}
|
|
}
|