This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/plugins/handlers/submission.py

326 lines
12 KiB
Python
Raw Normal View History

import logging
2023-02-17 23:48:37 +02:00
from datetime import datetime
2023-02-15 15:06:06 +02:00
from os import makedirs, path, sep
from pathlib import Path
2023-02-17 17:45:51 +02:00
from traceback import format_exc
2023-02-15 15:06:06 +02:00
from uuid import uuid4
2023-01-10 13:52:44 +02:00
from pyrogram import filters
from pyrogram.client import Client
2023-02-17 22:55:38 +02:00
from pyrogram.enums.chat_action import ChatAction
2023-03-20 13:03:03 +02:00
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from classes.enums.submission_types import SubmissionType
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnsupportedError
from classes.pyroclient import PyroClient
2023-10-15 18:40:56 +03:00
from modules import custom_filters
2023-06-28 11:52:00 +03:00
from modules.database import col_submitted
from modules.utils import USERS_WITH_CONTEXT
2023-02-14 12:38:54 +02:00
logger = logging.getLogger(__name__)
2023-01-10 13:52:44 +02:00
@Client.on_message(
2023-10-15 18:40:56 +03:00
custom_filters.mode_submit & ~filters.scheduled & filters.private & filters.photo
2023-03-09 12:33:02 +02:00
| filters.video
# | filters.animation
2023-03-09 12:33:02 +02:00
| filters.document
)
2023-10-15 19:14:22 +03:00
async def get_submission(app: PyroClient, message: Message):
global USERS_WITH_CONTEXT
2023-10-15 19:14:22 +03:00
if not hasattr(message.from_user, "id"):
return
2023-10-15 19:14:22 +03:00
if message.from_user.id in USERS_WITH_CONTEXT:
2023-03-20 13:03:03 +02:00
return
2023-10-15 19:14:22 +03:00
user = await app.find_user(message.from_user)
2023-06-28 11:37:18 +03:00
user_owner = await app.find_user(app.owner)
2023-02-15 15:07:40 +02:00
try:
2023-06-28 11:52:00 +03:00
if user.banned:
2023-02-15 15:07:40 +02:00
return
2023-03-09 12:33:02 +02:00
2023-10-15 19:14:22 +03:00
await app.send_chat_action(message.chat.id, ChatAction.TYPING)
2023-02-15 15:07:40 +02:00
save_tmp = True
contents = None
2023-06-28 11:37:18 +03:00
if await user.is_limited():
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_cooldown", "message", locale=user.locale).format(
app.config["submission"]["timeout"]
2023-03-09 12:33:02 +02:00
)
)
2023-02-15 15:07:40 +02:00
return
2023-10-15 19:14:22 +03:00
if message.document is not None:
logger.info(
"User %s is trying to submit a file of type '%s' with name '%s' and size of %s MB",
2023-10-15 19:14:22 +03:00
message.from_user.id,
message.document.mime_type,
message.document.file_name,
message.document.file_size / 1024 / 1024,
2023-03-17 15:13:28 +02:00
)
2023-10-15 19:14:22 +03:00
if message.document.mime_type not in app.config["submission"]["mime_types"]:
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"])
2023-03-18 21:53:26 +02:00
),
2023-03-09 12:33:02 +02:00
quote=True,
)
2023-02-15 15:07:40 +02:00
return
2023-10-15 19:14:22 +03:00
if message.document.file_size > app.config["submission"]["file_size"]:
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("document_too_large", "message", locale=user.locale).format(
app.config["submission"]["file_size"] / 1024 / 1024
2023-03-09 12:33:02 +02:00
),
quote=True,
)
2023-02-15 15:07:40 +02:00
return
2023-10-15 19:14:22 +03:00
if message.document.file_size > app.config["submission"]["tmp_size"]:
2023-02-15 15:07:40 +02:00
save_tmp = False
2023-03-09 12:33:02 +02:00
contents = (
2023-10-15 19:14:22 +03:00
message.document.file_id,
2023-03-09 12:33:02 +02:00
SubmissionType.DOCUMENT,
2023-10-15 19:14:22 +03:00
) # , message.document.file_name
2023-02-15 15:07:40 +02:00
2023-10-15 19:14:22 +03:00
if message.video is not None:
logger.info(
"User %s is trying to submit a video with name '%s' and size of %s MB",
2023-10-15 19:14:22 +03:00
message.from_user.id,
message.video.file_name,
message.video.file_size / 1024 / 1024,
2023-03-17 15:13:28 +02:00
)
2023-10-15 19:14:22 +03:00
if message.video.file_size > app.config["submission"]["file_size"]:
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("document_too_large", "message", locale=user.locale).format(
app.config["submission"]["file_size"] / 1024 / 1024
2023-03-09 12:33:02 +02:00
),
quote=True,
)
2023-02-15 15:07:40 +02:00
return
2023-10-15 19:14:22 +03:00
if message.video.file_size > app.config["submission"]["tmp_size"]:
2023-02-15 15:07:40 +02:00
save_tmp = False
2023-10-15 19:14:22 +03:00
contents = (
message.video.file_id,
SubmissionType.VIDEO,
) # , message.video.file_name
2023-02-15 15:07:40 +02:00
2023-10-15 19:14:22 +03:00
# if message.animation is not None:
# logger.info(
# "User %s is trying to submit an animation with name '%s' and size of %s MB",
2023-10-15 19:14:22 +03:00
# message.from_user.id,
# message.animation.file_name,
# message.animation.file_size / 1024 / 1024,
# )
2023-10-15 19:14:22 +03:00
# if message.animation.file_size > app.config["submission"]["file_size"]:
# await message.reply_text(
2023-06-28 11:37:18 +03:00
# app._("document_too_large", "message", locale=user.locale).format(
# str(app.config["submission"]["file_size"] / 1024 / 1024)
# ),
# quote=True,
# )
# return
2023-10-15 19:14:22 +03:00
# if message.animation.file_size > app.config["submission"]["tmp_size"]:
# save_tmp = False
# contents = (
2023-10-15 19:14:22 +03:00
# message.animation.file_id,
# SubmissionType.ANIMATION,
2023-10-15 19:14:22 +03:00
# ) # , message.animation.file_name
2023-02-15 15:07:40 +02:00
2023-10-15 19:14:22 +03:00
if message.photo is not None:
logger.info(
"User %s is trying to submit a photo with ID '%s' and size of %s MB",
2023-10-15 19:14:22 +03:00
message.from_user.id,
message.photo.file_id,
message.photo.file_size / 1024 / 1024,
2023-03-17 15:13:28 +02:00
)
2023-10-15 19:14:22 +03:00
contents = (
message.photo.file_id,
SubmissionType.PHOTO,
) # , "please_generate"
2023-02-15 15:07:40 +02:00
if contents is None:
return
2023-02-15 15:07:40 +02:00
if save_tmp is not None:
2023-02-15 15:07:40 +02:00
tmp_id = str(uuid4())
2023-02-15 15:07:40 +02:00
# filename = tmp_id if contents[1] == "please_generate" else contents[1]
2023-03-09 12:33:02 +02:00
makedirs(
Path(f"{app.config['locations']['data']}/submissions/{tmp_id}"),
2023-03-09 12:33:02 +02:00
exist_ok=True,
)
downloaded = await app.download_media(
2023-10-15 19:14:22 +03:00
message,
str(Path(f"{app.config['locations']['data']}/submissions/{tmp_id}"))
+ sep,
2023-03-09 12:33:02 +02:00
)
2023-08-14 15:52:02 +03:00
inserted = await col_submitted.insert_one(
2023-02-15 15:07:40 +02:00
{
2023-10-15 19:14:22 +03:00
"user": message.from_user.id,
2023-02-17 22:55:38 +02:00
"date": datetime.now(),
2023-02-15 15:07:40 +02:00
"done": False,
"type": contents[1].value,
2023-03-09 12:33:02 +02:00
"temp": {"uuid": tmp_id, "file": path.basename(str(downloaded))},
2023-10-15 19:14:22 +03:00
"telegram": {"msg_id": message.id, "file_id": contents[0]},
"caption": str(message.caption)
if message.caption is not None
else None,
2023-02-15 15:07:40 +02:00
}
)
2023-03-09 12:33:02 +02:00
else:
2023-08-14 15:52:02 +03:00
inserted = await col_submitted.insert_one(
2023-02-15 15:07:40 +02:00
{
2023-10-15 19:14:22 +03:00
"user": message.from_user.id,
2023-02-17 22:55:38 +02:00
"date": datetime.now(),
2023-02-15 15:07:40 +02:00
"done": False,
"type": contents[1].value,
2023-03-09 12:33:02 +02:00
"temp": {"uuid": None, "file": None},
2023-10-15 19:14:22 +03:00
"telegram": {"msg_id": message.id, "file_id": contents[0]},
"caption": str(message.caption)
if message.caption is not None
else None,
2023-02-15 15:07:40 +02:00
}
)
2023-03-09 12:33:02 +02:00
2023-02-15 15:07:40 +02:00
buttons = [
[
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=app._("sub_yes", "button", locale=user_owner.locale),
2023-03-09 12:33:02 +02:00
callback_data=f"sub_yes_{str(inserted.inserted_id)}",
)
2023-02-15 15:07:40 +02:00
]
]
2023-10-15 19:14:22 +03:00
if message.caption is not None:
caption = str(message.caption)
2023-02-15 15:07:40 +02:00
buttons[0].append(
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=app._("sub_yes_caption", "button", locale=user_owner.locale),
2023-03-09 12:33:02 +02:00
callback_data=f"sub_yes_{str(inserted.inserted_id)}_caption",
)
2023-02-15 15:07:40 +02:00
)
else:
caption = ""
buttons[0].append(
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=app._("sub_no", "button", locale=user_owner.locale),
callback_data=f"sub_no_{str(inserted.inserted_id)}",
)
)
2023-06-28 11:37:18 +03:00
caption += app._("sub_by", "message", locale=user_owner.locale)
2023-02-15 15:07:40 +02:00
2023-10-15 19:14:22 +03:00
if message.from_user.first_name is not None:
caption += f" {message.from_user.first_name}"
if message.from_user.last_name is not None:
caption += f" {message.from_user.last_name}"
if message.from_user.username is not None:
caption += f" (@{message.from_user.username})"
if message.from_user.phone_number is not None:
caption += f" ({message.from_user.phone_number})"
2023-02-15 15:07:40 +02:00
2023-03-09 12:33:02 +02:00
if (
2023-10-15 19:14:22 +03:00
message.from_user.id in app.admins
and app.config["submission"]["require_confirmation"]["admins"] is False
2023-03-09 12:33:02 +02:00
):
2023-02-17 17:45:51 +02:00
try:
submitted = await app.submit_media(str(inserted.inserted_id))
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_yes_auto", "message", locale=user.locale),
2023-03-09 12:33:02 +02:00
disable_notification=True,
quote=True,
)
if app.config["submission"]["send_uploaded_id"]:
2023-03-16 16:03:14 +02:00
caption += f"\n\nID: `{submitted[1]}`"
2023-10-15 19:14:22 +03:00
await message.copy(
app.owner, caption=caption, disable_notification=True
)
2023-02-17 17:45:51 +02:00
return
except SubmissionUnsupportedError:
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"]), quote=True
),
quote=True,
)
return
2023-08-16 14:27:23 +03:00
except SubmissionDuplicatesError as exc:
2023-10-15 19:14:22 +03:00
await message.reply_text(
app._(
2023-06-28 11:37:18 +03:00
"sub_media_duplicates_list", "message", locale=user.locale
2023-08-16 14:27:23 +03:00
).format("\n".join(exc.duplicates)),
2023-03-09 12:33:02 +02:00
quote=True,
)
2023-02-17 17:45:51 +02:00
return
2023-08-16 14:27:23 +03:00
except Exception as exc:
2023-10-15 19:14:22 +03:00
await message.reply_text(exc, quote=True)
2023-02-17 17:45:51 +02:00
return
2023-03-09 12:33:02 +02:00
elif (
2023-10-15 19:14:22 +03:00
message.from_user.id not in app.admins
and app.config["submission"]["require_confirmation"]["users"] is False
2023-03-09 12:33:02 +02:00
):
2023-02-17 17:45:51 +02:00
try:
2023-03-16 16:03:14 +02:00
submitted = await app.submit_photo(str(inserted.inserted_id))
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_yes_auto", "message", locale=user.locale),
2023-03-09 12:33:02 +02:00
disable_notification=True,
quote=True,
)
if app.config["submission"]["send_uploaded_id"]:
2023-03-16 16:03:14 +02:00
caption += f"\n\nID: `{submitted[1]}`"
2023-10-15 19:14:22 +03:00
await message.copy(app.owner, caption=caption)
2023-02-17 17:45:51 +02:00
return
except SubmissionUnsupportedError:
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"]), quote=True
)
)
return
2023-08-16 14:27:23 +03:00
except SubmissionDuplicatesError as exc:
2023-10-15 19:14:22 +03:00
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_dup", "message", locale=user.locale), quote=True
2023-03-09 12:33:02 +02:00
)
2023-02-17 17:45:51 +02:00
return
2023-08-16 14:27:23 +03:00
except Exception as exc:
2023-03-09 12:33:02 +02:00
await app.send_message(
app.owner,
2023-06-28 11:37:18 +03:00
app._(
"sub_error_admin", "message", locale=user_owner.locale
2023-10-15 19:14:22 +03:00
).format(message.from_user.id, format_exc()),
2023-03-09 12:33:02 +02:00
)
2023-10-15 19:14:22 +03:00
await message.reply_text("sub_error", quote=True)
2023-02-17 17:45:51 +02:00
return
2023-10-15 19:14:22 +03:00
if message.from_user.id not in app.admins:
2023-02-15 15:07:40 +02:00
buttons += [
[
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=app._("sub_block", "button", locale=user_owner.locale),
2023-10-15 19:14:22 +03:00
callback_data=f"sub_block_{message.from_user.id}",
2023-03-09 12:33:02 +02:00
)
2023-02-15 15:07:40 +02:00
]
]
2023-06-28 11:37:18 +03:00
await user.update_cooldown()
2023-02-15 15:07:40 +02:00
2023-10-15 19:14:22 +03:00
if message.from_user.id != app.owner:
await message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_sent", "message", locale=user.locale),
2023-03-09 12:33:02 +02:00
disable_notification=True,
quote=True,
)
2023-02-17 22:55:38 +02:00
2023-10-15 19:14:22 +03:00
await message.copy(
2023-03-09 12:33:02 +02:00
app.owner, caption=caption, reply_markup=InlineKeyboardMarkup(buttons)
)
2023-02-15 15:07:40 +02:00
except AttributeError:
logger.error("'from_user' does not seem to contain 'id'")