78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
use crate::prelude::*;
|
|
|
|
/// Checks that the account with that name exists
|
|
#[inline]
|
|
async fn check_name(
|
|
bot: &Throttle<Bot>,
|
|
msg: &Message,
|
|
db: &DatabaseConnection,
|
|
name: &str,
|
|
user_id: u64,
|
|
) -> crate::Result<Option<Message>> {
|
|
if !Account::exists(user_id, name, db).await? {
|
|
let msg = bot
|
|
.send_message(msg.chat.id, "Account doesn't exists. Try again")
|
|
.reply_markup(account_markup(user_id, db).await?)
|
|
.await?;
|
|
return Ok(Some(msg));
|
|
}
|
|
Ok(None)
|
|
}
|
|
|
|
/// Function to handle GetExistingName state
|
|
pub async fn get_existing_name(
|
|
bot: Throttle<Bot>,
|
|
msg: Message,
|
|
db: DatabaseConnection,
|
|
dialogue: MainDialogue,
|
|
next: PackagedHandler<String>,
|
|
) -> crate::Result<()> {
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
|
|
|
let mut handler = next.lock().await;
|
|
if handler.func.is_none() {
|
|
let _ = dialogue.exit().await;
|
|
return Err(HandlerUsed.into());
|
|
}
|
|
|
|
delete_optional(&bot, handler.previous.as_ref()).await;
|
|
|
|
let text = match msg.text() {
|
|
Some(text) => text.trim(),
|
|
None => {
|
|
let msg = bot
|
|
.send_message(
|
|
msg.chat.id,
|
|
"Couldn't get the text of the message. Send the name again",
|
|
)
|
|
.reply_markup(account_markup(user_id, &db).await?)
|
|
.await?;
|
|
handler.previous = Some(msg);
|
|
return Ok(());
|
|
}
|
|
};
|
|
|
|
if text == "/cancel" {
|
|
dialogue.exit().await?;
|
|
bot.send_message(msg.chat.id, "Successfully cancelled")
|
|
.reply_markup(deletion_markup())
|
|
.await?;
|
|
return Ok(());
|
|
}
|
|
|
|
if let Some(failure_message) = check_name(&bot, &msg, &db, text, user_id).await? {
|
|
handler.previous = Some(failure_message);
|
|
return Ok(());
|
|
}
|
|
|
|
let func = handler.func.take().unwrap();
|
|
drop(handler);
|
|
let text = text.to_owned();
|
|
|
|
if let Err(err) = func(bot, msg, db, dialogue.clone(), text).await {
|
|
let _ = dialogue.exit().await;
|
|
return Err(err);
|
|
}
|
|
Ok(())
|
|
}
|