simplified Handler.previous into the chat id and message id
This commit is contained in:
parent
1f016a3694
commit
e25460a3d8
@ -26,13 +26,13 @@ where
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
delete_optional(&bot, handler.previous.as_ref()).await;
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
let text = match msg.text() {
|
||||
Some(text) => text.trim(),
|
||||
None => {
|
||||
let msg = bot.send_message(msg.chat.id, no_text_message).await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@ -46,7 +46,7 @@ where
|
||||
}
|
||||
|
||||
if let Some(failure_message) = check(&bot, &msg, &db, text).await? {
|
||||
handler.previous = Some(failure_message);
|
||||
handler.previous = failure_message.into();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ pub async fn get_existing_name(
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
delete_optional(&bot, handler.previous.as_ref()).await;
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
let text = match msg.text() {
|
||||
Some(text) => text.trim(),
|
||||
@ -47,7 +47,7 @@ pub async fn get_existing_name(
|
||||
)
|
||||
.reply_markup(account_markup(user_id, &db).await?)
|
||||
.await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@ -61,7 +61,7 @@ pub async fn get_existing_name(
|
||||
}
|
||||
|
||||
if let Some(failure_message) = check_name(&bot, &msg, &db, text, user_id).await? {
|
||||
handler.previous = Some(failure_message);
|
||||
handler.previous = failure_message.into();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
@ -121,7 +121,7 @@ pub async fn get_user(
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
delete_optional(&bot, handler.previous.as_ref()).await;
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
if let Some("/cancel") = msg.text().map(str::trim) {
|
||||
dialogue.exit().await?;
|
||||
@ -137,7 +137,7 @@ pub async fn get_user(
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "You didn't send a file. Try again")
|
||||
.await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@ -151,7 +151,7 @@ pub async fn get_user(
|
||||
"Invalid file name. You need to send a json file. Try again",
|
||||
)
|
||||
.await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@ -170,14 +170,14 @@ pub async fn get_user(
|
||||
Ok(Ok(user)) => user,
|
||||
Ok(Err(error_text)) => {
|
||||
let msg = bot.send_message(msg.chat.id, error_text).await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
Err(_) => {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Error parsing the json file. Try again")
|
||||
.await?;
|
||||
handler.previous = Some(msg);
|
||||
handler.previous = msg.into();
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
@ -1,8 +1,27 @@
|
||||
use crate::prelude::*;
|
||||
use futures::future::BoxFuture;
|
||||
use std::{future::Future, sync::Arc};
|
||||
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>,
|
||||
@ -17,7 +36,7 @@ type DynHanlder<T> = Box<
|
||||
pub struct Handler<T> {
|
||||
pub func: Option<DynHanlder<T>>,
|
||||
|
||||
pub previous: Option<Message>,
|
||||
pub previous: MessageIds,
|
||||
}
|
||||
|
||||
pub type PackagedHandler<T> = Arc<Mutex<Handler<T>>>;
|
||||
@ -25,7 +44,7 @@ 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<Option<Message>>) -> PackagedHandler<T>
|
||||
pub fn new<H, F>(f: H, previous: impl Into<MessageIds>) -> PackagedHandler<T>
|
||||
where
|
||||
H: FnOnce(Throttle<Bot>, Message, DatabaseConnection, MainDialogue, T) -> F
|
||||
+ Send
|
||||
|
@ -6,14 +6,6 @@ pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
}
|
||||
|
||||
/// Deletes the message if there is one ignoring the errors
|
||||
#[inline]
|
||||
pub async fn delete_optional(bot: &Throttle<Bot>, msg: Option<&Message>) {
|
||||
if let Some(msg) = msg {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the field is valid
|
||||
#[inline]
|
||||
pub fn validate_field(field: &str) -> bool {
|
||||
|
Loading…
Reference in New Issue
Block a user