Added conformation before deleting an account. No longer raising an exception if message deletion fails

This commit is contained in:
StNicolay 2022-12-25 21:32:21 +03:00
parent e165020111
commit 8858aa09a7

View File

@ -18,6 +18,15 @@ from ..decrypted_account import DecryptedAccount
Message = telebot.types.Message
def _delete_message(bot: telebot.TeleBot, mes: Message) -> bool:
try:
bot.delete_message(mes.chat.id, mes.id)
except telebot.apihelper.ApiException:
return False
else:
return True
def _send_tmp_message(
bot: telebot.TeleBot,
chat_id: telebot.types.Message,
@ -26,15 +35,15 @@ def _send_tmp_message(
) -> None:
bot_mes = bot.send_message(chat_id, text, parse_mode="MarkdownV2")
time.sleep(timeout)
bot.delete_message(chat_id, bot_mes.id, timeout=5)
_delete_message(bot, bot_mes)
def _base_handler(
bot: telebot.TeleBot, mes: Message, prev_mes: Message | None = None
) -> None:
bot.delete_message(mes.chat.id, mes.id, timeout=5)
_delete_message(bot, mes)
if prev_mes is not None:
bot.delete_message(prev_mes.chat.id, prev_mes.id, timeout=5)
_delete_message(bot, prev_mes)
def get_accounts(bot: telebot.TeleBot, engine: Engine, mes: Message) -> None:
@ -393,8 +402,11 @@ def delete_account(bot: telebot.TeleBot, engine: Engine, mes: Message) -> None:
def _delete_account2(
bot: telebot.TeleBot, engine: Engine, prev_mes: Message, mes: Message
) -> None:
bot: telebot.TeleBot,
engine: Engine,
prev_mes: Message,
mes: Message,
):
_base_handler(bot, mes, prev_mes)
text = mes.text.strip()
if text == "/cancel":
@ -403,7 +415,30 @@ def _delete_account2(
if text not in database.get.get_accounts(engine, mes.from_user.id):
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
database.delete.delete_account(engine, mes.from_user.id, text)
bot_mes = bot.send_message(
mes.from_user.id,
f'Вы уверены, что хотите удалить аккаунт "{text}"?\nОтправьте YES для '
"подтверждения",
)
bot.register_next_step_handler(
mes, functools.partial(_delete_account3, bot, engine, bot_mes, text)
)
def _delete_account3(
bot: telebot.TeleBot,
engine: Engine,
prev_mes: Message,
account_name: str,
mes: Message,
) -> None:
_base_handler(bot, mes, prev_mes)
text = mes.text.strip()
if text != "YES":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
database.delete.delete_account(engine, mes.from_user.id, account_name)
_send_tmp_message(bot, mes.chat.id, "Аккаунт удалён")
@ -473,7 +508,7 @@ def _export2(
del text, accounts, json_io
gc.collect()
time.sleep(30)
bot.delete_message(bot_mes.chat.id, bot_mes.id, timeout=5)
_delete_message(bot, bot_mes)
def import_accounts(