Updated generic and get_document to close dialogue on /cancel and errors

This commit is contained in:
StNicolay 2023-05-10 19:12:22 +03:00
parent 996dca00fb
commit 5f34cfb109
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 29 additions and 11 deletions

View File

@ -25,17 +25,22 @@ where
) -> PinnedFuture<'a, crate::Result<bool>>,
{
let handler = next.lock().await.take();
let previous = handler
.as_ref()
.and_then(|handler| handler.previous.as_ref());
let previous = handler.as_ref().and_then(|h| h.previous.as_ref());
delete_optional(&bot, previous).await;
if text == "/cancel" {
if text.trim_end() == "/cancel" {
dialogue.exit().await?;
bot.send_message(msg.chat.id, "Successfully cancelled")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let handler = handler.ok_or(HandlerUsed)?;
let handler = match handler {
Some(handler) => handler,
None => {
let _ = dialogue.exit().await;
return Err(HandlerUsed.into());
}
};
match check(&bot, &msg, &db, &text).await {
Ok(true) => (),
Ok(false) => {
@ -47,5 +52,9 @@ where
return Err(err);
}
};
(handler.handler)(bot, msg, db, dialogue, text).await
if let Err(err) = (handler.handler)(bot, msg, db, dialogue.clone(), text).await {
let _ = dialogue.exit().await;
return Err(err);
}
Ok(())
}

View File

@ -14,17 +14,22 @@ pub async fn get_document(
next: PackagedHandler<()>,
) -> crate::Result<()> {
let handler = next.lock().await.take();
let previous = handler
.as_ref()
.and_then(|handler| handler.previous.as_ref());
let previous = handler.as_ref().and_then(|h| h.previous.as_ref());
delete_optional(&bot, previous).await;
if let Some("/cancel") = msg.text().map(str::trim_end) {
dialogue.exit().await?;
bot.send_message(msg.chat.id, "Successfully cancelled")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let handler = handler.ok_or(HandlerUsed)?;
let handler = match handler {
Some(handler) => handler,
None => {
let _ = dialogue.exit().await;
return Err(HandlerUsed.into());
}
};
if msg.document().is_none() {
bot.send_message(msg.chat.id, "You didn't send a file")
.reply_markup(deletion_markup())
@ -32,5 +37,9 @@ pub async fn get_document(
dialogue.exit().await?;
return Ok(());
}
(handler.handler)(bot, msg, db, dialogue, ()).await
if let Err(err) = (handler.handler)(bot, msg, db, dialogue.clone(), ()).await {
let _ = dialogue.exit().await;
return Err(err);
}
Ok(())
}