from datetime import datetime from app import app from pyrogram import filters from pyrogram.types import CallbackQuery from pyrogram.client import Client from pykeyboard import InlineKeyboard, InlineButton from modules.utils import configGet, locale from modules.database import col_warnings from bson import ObjectId @app.on_callback_query(filters.regex("w_rev_[\s\S]*")) async def callback_query_warning_revoke(app: Client, clb: CallbackQuery): warning = col_warnings.find_one({"_id": ObjectId(str(clb.data).split("_")[2])}) if warning is None: await clb.answer( text=locale("warning_not_found", "callback", locale=clb.from_user), show_alert=True, ) return col_warnings.update_one( {"_id": warning["_id"]}, {"$set": {"active": False, "revoke_date": datetime.now()}}, ) await clb.answer( text=locale("warning_revoked", "callback", locale=clb.from_user).format(), show_alert=True, ) await app.send_message( configGet("admin", "groups"), locale("warning_revoked_auto", "message").format( warning["user"], warning["date"].strftime("%d.%m.%Y") ), ) target_id = warning["user"] if col_warnings.count_documents({"user": target_id, "active": True}) == 0: await clb.edit_message_text( locale("no_warnings", "message", locale=clb.from_user).format( target_id, target_id ) ) return keyboard = InlineKeyboard() buttons = [] warnings = [] for index, warning in enumerate( list(col_warnings.find({"user": target_id, "active": True})) ): warnings.append( f'{index+1}. {warning["date"].strftime("%d.%m.%Y, %H:%M")}\n Адмін: {warning["admin"]}\n Причина: {warning["reason"]}' ) buttons.append(InlineButton(str(index + 1), f'w_rev_{str(warning["_id"])}')) keyboard.add(*buttons) await clb.edit_message_text( locale("warnings_revoke", "message", locale=clb.from_user).format( target_id, "\n".join(warnings) ), ) await clb.edit_message_reply_markup(reply_markup=keyboard)