This repository has been archived on 2024-10-10. You can view files and clone it, but cannot push or open issues or pull requests.

116 lines
3.7 KiB
Python

import logging
from io import BytesIO
from convopyro import listen_message
from pykeyboard import ReplyButton, ReplyKeyboard
from pyrogram import filters
from pyrogram.types import Message, ReplyKeyboardRemove
from ujson import dumps
from classes.pyroclient import PyroClient
from modules import custom_filters
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["checkout"], prefixes=["/"]) & ~custom_filters.context # type: ignore
)
async def command_checkout(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
user_data = BytesIO(
dumps(await user.checkout(), escape_forward_slashes=False).encode()
)
await message.reply_document(
user_data,
file_name="user_data.json",
)
# Data deletion request
keyboard_delete = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_delete.add(
ReplyButton(app._("delete_yes", "buttons", locale=user.locale)),
ReplyButton(app._("delete_no", "buttons", locale=user.locale)),
)
await message.reply_text(
app._("checkout", "messages", locale=user.locale),
reply_markup=keyboard_delete,
)
while True:
app.contexts.append(message.from_user.id)
answer_delete = await listen_message(app, message.chat.id, 300)
app.contexts.remove(message.from_user.id)
if answer_delete is None or answer_delete.text == "/cancel":
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_delete.text not in app._(
"delete_yes", "buttons", locale=user.locale
) + app._("delete_no", "buttons", locale=user.locale):
await answer_delete.reply_text(
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
if answer_delete.text in app._("delete_no", "buttons", locale=user.locale):
await answer_delete.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
break
# Confirmation
keyboard_confirm = ReplyKeyboard(
row_width=1, resize_keyboard=True, one_time_keyboard=True
)
keyboard_confirm.add(
ReplyButton(app._("delete_confirm", "buttons", locale=user.locale))
)
await message.reply_text(
app._("checkout_deletion", "messages", locale=user.locale),
reply_markup=keyboard_confirm,
)
while True:
app.contexts.append(message.from_user.id)
answer_confirm = await listen_message(app, message.chat.id, 300)
app.contexts.remove(message.from_user.id)
if answer_confirm is None or answer_confirm.text == "/cancel":
await message.reply_text(
app._("cancelled", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)
return
if answer_confirm.text not in app.in_all_locales("delete_confirm", "buttons"):
await answer_confirm.reply_text(
app._("selection_invalid", "messages", locale=user.locale).format(
cancel_notice=app._("cancel", "messages", locale=user.locale)
)
)
continue
break
await user.delete()
await answer_confirm.reply_text(
app._("checkout_deleted", "messages", locale=user.locale),
reply_markup=ReplyKeyboardRemove(),
)