Skip to content

Commit

Permalink
Alert when bot couldn't delete some messages for any reason
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterGroosha committed May 18, 2021
1 parent d803f22 commit 82b1b35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bot/handlers/callbacks_reports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from contextlib import suppress
from aiogram import types, Dispatcher
from aiogram.utils.exceptions import MessageToDeleteNotFound
from aiogram.utils.exceptions import MessageToDeleteNotFound, MessageCantBeDeleted
from bot.common import report_msg_cb
from bot.config_reader import Config
from bot.localization import get_string
Expand All @@ -20,17 +19,23 @@ async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict
user_id = callback_data.get("user_id")
message_ids = callback_data.get("message_ids")

delete_ok = True
for msg_id in message_ids.split(","):
with suppress(MessageToDeleteNotFound):
try:
await call.bot.delete_message(config.group.main, msg_id)
except (MessageToDeleteNotFound, MessageCantBeDeleted):
delete_ok = False

if option == "del":
await call.message.edit_text(
call.message.html_text + get_string(config.lang, "action_deleted"))
elif option == "ban":
await call.bot.kick_chat_member(config.group.main, user_id)
await call.message.edit_text(call.message.html_text + get_string(config.lang, "action_deleted_banned"))
await call.answer()
if delete_ok:
await call.answer()
else:
await call.answer(show_alert=True, text=get_string(config.lang, "action_deleted_partially"))


def register_callbacks_reports(dp: Dispatcher):
Expand Down
2 changes: 2 additions & 0 deletions bot/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

"action_deleted": "\n\n🗑 <b>Deleted</b>",
"action_deleted_banned": "\n\n🗑❌ <b>Deleted, user banned</b>",
"action_deleted_partially": "Some messages couldn't be found or deleted",

"readonly_forever": "🙊 <i>User set to read-only mode forever</i>",
"readonly_temporary": "🙊 <i>User set to read-only mode until {time} (server time)</i>",
Expand All @@ -40,6 +41,7 @@

"action_deleted": "\n\n🗑 <b>Удалено</b>",
"action_deleted_banned": "\n\n🗑❌ <b>Удалено, юзер забанен</b>",
"action_deleted_partially": "Не удалось найти или удалить некоторые сообщения",

"readonly_forever": "🙊 <i>Пользователь переведён в режим «только чтение» навсегда</i>",
"readonly_temporary": "🙊 <i>Пользователь переведён в режим «только чтение» до {time} (время серверное)</i>",
Expand Down

0 comments on commit 82b1b35

Please sign in to comment.