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

285 lines
11 KiB
Python
Raw Normal View History

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
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
2023-02-14 12:38:54 +02:00
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
2023-02-17 22:55:38 +02:00
from pyrogram.enums.chat_action import ChatAction
2023-02-17 17:45:51 +02:00
from classes.exceptions import SubmissionDuplicatesError
2023-02-17 22:55:38 +02:00
from classes.poster_client import PosterClient
2023-02-17 23:48:37 +02:00
from classes.user import PosterUser
2023-02-14 12:38:54 +02:00
2023-01-10 13:52:44 +02:00
from modules.app import app
2023-02-15 15:06:06 +02:00
from modules.database import col_banned, col_submitted
2023-02-14 12:38:54 +02:00
from modules.logger import logWrite
2023-02-15 15:06:06 +02:00
from modules.utils import configGet, locale
from classes.enums.submission_types import SubmissionType
2023-02-14 12:38:54 +02:00
2023-01-10 13:52:44 +02:00
2023-03-09 12:33:02 +02:00
@app.on_message(
~filters.scheduled & filters.private & filters.photo
| filters.video
| filters.animation
| filters.document
)
2023-02-17 22:55:38 +02:00
async def get_submission(app: PosterClient, msg: Message):
2023-02-15 15:07:40 +02:00
try:
2023-03-09 12:33:02 +02:00
if col_banned.find_one({"user": msg.from_user.id}) is not None:
2023-02-15 15:07:40 +02:00
return
2023-03-09 12:33:02 +02:00
2023-02-17 22:55:38 +02:00
await app.send_chat_action(msg.chat.id, ChatAction.TYPING)
2023-02-15 15:07:40 +02:00
user_locale = msg.from_user.language_code
save_tmp = True
contents = None
2023-02-17 23:48:37 +02:00
if PosterUser(msg.from_user.id).is_limited():
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("sub_cooldown", "message", locale=user_locale).format(
str(configGet("timeout", "submission"))
)
)
2023-02-15 15:07:40 +02:00
return
if msg.document is not None:
2023-03-17 15:13:28 +02:00
logWrite(
f"User {msg.from_user.id} is trying to submit a file of type '{msg.document.mime_type}' with name '{msg.document.file_name}' and size of {msg.document.file_size / 1024 / 1024} MB",
debug=True,
)
2023-02-15 15:07:40 +02:00
if msg.document.mime_type not in configGet("mime_types", "submission"):
2023-03-09 12:33:02 +02:00
await msg.reply_text(
2023-03-18 21:53:26 +02:00
locale("mime_not_allowed", "message", locale=user_locale).format(
", ".join(configGet("mime_types", "submission"))
),
2023-03-09 12:33:02 +02:00
quote=True,
)
2023-02-15 15:07:40 +02:00
return
if msg.document.file_size > configGet("file_size", "submission"):
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("document_too_large", "message", locale=user_locale).format(
str(configGet("file_size", "submission") / 1024 / 1024)
),
quote=True,
)
2023-02-15 15:07:40 +02:00
return
if msg.document.file_size > configGet("tmp_size", "submission"):
save_tmp = False
2023-03-09 12:33:02 +02:00
contents = (
msg.document.file_id,
SubmissionType.DOCUMENT,
) # , msg.document.file_name
2023-02-15 15:07:40 +02:00
if msg.video is not None:
2023-03-17 15:13:28 +02:00
logWrite(
f"User {msg.from_user.id} is trying to submit a video with name '{msg.video.file_name}' and size of {msg.video.file_size / 1024 / 1024} MB",
debug=True,
)
2023-02-15 15:07:40 +02:00
if msg.video.file_size > configGet("file_size", "submission"):
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("document_too_large", "message", locale=user_locale).format(
str(configGet("file_size", "submission") / 1024 / 1024)
),
quote=True,
)
2023-02-15 15:07:40 +02:00
return
if msg.video.file_size > configGet("tmp_size", "submission"):
save_tmp = False
2023-03-09 12:33:02 +02:00
contents = msg.video.file_id, SubmissionType.VIDEO # , msg.video.file_name
2023-02-15 15:07:40 +02:00
if msg.animation is not None:
2023-03-17 15:13:28 +02:00
logWrite(
f"User {msg.from_user.id} is trying to submit an animation with name '{msg.animation.file_name}' and size of {msg.animation.file_size / 1024 / 1024} MB",
debug=True,
)
2023-02-15 15:07:40 +02:00
if msg.animation.file_size > configGet("file_size", "submission"):
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("document_too_large", "message", locale=user_locale).format(
str(configGet("file_size", "submission") / 1024 / 1024)
),
quote=True,
)
2023-02-15 15:07:40 +02:00
return
if msg.animation.file_size > configGet("tmp_size", "submission"):
save_tmp = False
2023-03-09 12:33:02 +02:00
contents = (
msg.animation.file_id,
SubmissionType.ANIMATION,
) # , msg.animation.file_name
2023-02-15 15:07:40 +02:00
if msg.photo is not None:
2023-03-17 15:13:28 +02:00
logWrite(
f"User {msg.from_user.id} is trying to submit a photo with ID '{msg.photo.file_id}' and size of {msg.photo.file_size / 1024 / 1024} MB",
debug=True,
)
2023-03-09 12:33:02 +02:00
contents = msg.photo.file_id, SubmissionType.PHOTO # , "please_generate"
2023-02-15 15:07:40 +02:00
if save_tmp is not None:
if contents is None:
return
tmp_id = str(uuid4())
# filename = tmp_id if contents[1] == "please_generate" else contents[1]
2023-03-09 12:33:02 +02:00
makedirs(
path.join(configGet("data", "locations"), "submissions", tmp_id),
exist_ok=True,
)
downloaded = await app.download_media(
msg,
path.join(configGet("data", "locations"), "submissions", tmp_id) + sep,
)
2023-02-15 15:07:40 +02:00
inserted = col_submitted.insert_one(
{
"user": msg.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))},
"telegram": {"msg_id": msg.id, "file_id": contents[0]},
"caption": str(msg.caption) if msg.caption is not None else None,
2023-02-15 15:07:40 +02:00
}
)
2023-03-09 12:33:02 +02:00
else:
2023-02-15 15:07:40 +02:00
if contents is None:
return
inserted = col_submitted.insert_one(
{
"user": msg.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},
"telegram": {"msg_id": msg.id, "file_id": contents[0]},
"caption": str(msg.caption) if msg.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(
text=locale("sub_yes", "button", locale=configGet("locale")),
callback_data=f"sub_yes_{str(inserted.inserted_id)}",
)
2023-02-15 15:07:40 +02:00
]
]
if msg.caption is not None:
caption = str(msg.caption)
buttons[0].append(
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
text=locale(
"sub_yes_caption", "button", locale=configGet("locale")
),
callback_data=f"sub_yes_{str(inserted.inserted_id)}_caption",
)
2023-02-15 15:07:40 +02:00
)
buttons[0].append(
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
text=locale("sub_no", "button", locale=configGet("locale")),
callback_data=f"sub_no_{str(inserted.inserted_id)}",
)
2023-02-15 15:07:40 +02:00
)
else:
caption = ""
buttons[0].append(
2023-03-09 12:33:02 +02:00
InlineKeyboardButton(
text=locale("sub_no", "button", locale=configGet("locale")),
callback_data=f"sub_no_{str(inserted.inserted_id)}",
)
2023-02-15 15:07:40 +02:00
)
caption += locale("sub_by", "message", locale=locale(configGet("locale")))
if msg.from_user.first_name is not None:
caption += f" {msg.from_user.first_name}"
if msg.from_user.last_name is not None:
caption += f" {msg.from_user.last_name}"
if msg.from_user.username is not None:
caption += f" (@{msg.from_user.username})"
if msg.from_user.phone_number is not None:
caption += f" ({msg.from_user.phone_number})"
2023-03-09 12:33:02 +02:00
if (
msg.from_user.id in app.admins
and configGet("admins", "submission", "require_confirmation") is False
):
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-03-09 12:33:02 +02:00
await msg.reply_text(
locale("sub_yes_auto", "message", locale=user_locale),
disable_notification=True,
quote=True,
)
2023-03-16 16:03:14 +02:00
if configGet("send_uploaded_id", "submission"):
caption += f"\n\nID: `{submitted[1]}`"
2023-02-19 21:54:58 +02:00
await msg.copy(app.owner, caption=caption, disable_notification=True)
2023-02-17 17:45:51 +02:00
return
except SubmissionDuplicatesError as exp:
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale(
"sub_media_duplicates_list", "message", locale=user_locale
).format("\n".join(exp.duplicates)),
quote=True,
)
2023-02-17 17:45:51 +02:00
return
except Exception as exp:
2023-02-19 21:54:58 +02:00
await msg.reply_text(format_exc(), quote=True)
2023-02-17 17:45:51 +02:00
return
2023-03-09 12:33:02 +02:00
elif (
msg.from_user.id not in app.admins
and configGet("users", "submission", "require_confirmation") is False
):
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-03-09 12:33:02 +02:00
await msg.reply_text(
locale("sub_yes_auto", "message", locale=user_locale),
disable_notification=True,
quote=True,
)
2023-03-16 16:03:14 +02:00
if configGet("send_uploaded_id", "submission"):
caption += f"\n\nID: `{submitted[1]}`"
2023-02-17 22:55:38 +02:00
await msg.copy(app.owner, caption=caption)
2023-02-17 17:45:51 +02:00
return
except SubmissionDuplicatesError as exp:
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("sub_dup", "message", locale=user_locale), quote=True
)
2023-02-17 17:45:51 +02:00
return
except Exception as exp:
2023-03-09 12:33:02 +02:00
await app.send_message(
app.owner,
locale("sub_error_admin", "message").format(
msg.from_user.id, format_exc()
),
)
2023-02-19 21:54:58 +02:00
await msg.reply_text("sub_error", quote=True)
2023-02-17 17:45:51 +02:00
return
2023-02-17 22:55:38 +02:00
if msg.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(
text=locale("sub_block", "button", locale=configGet("locale")),
callback_data=f"sub_block_{msg.from_user.id}",
)
2023-02-15 15:07:40 +02:00
]
]
2023-02-17 23:48:37 +02:00
PosterUser(msg.from_user.id).limit()
2023-02-15 15:07:40 +02:00
2023-02-17 22:55:38 +02:00
if msg.from_user.id != app.owner:
2023-03-09 12:33:02 +02:00
await msg.reply_text(
locale("sub_sent", "message", locale=user_locale),
disable_notification=True,
quote=True,
)
2023-02-17 22:55:38 +02:00
2023-03-09 12:33:02 +02:00
await msg.copy(
app.owner, caption=caption, reply_markup=InlineKeyboardMarkup(buttons)
)
2023-02-15 15:07:40 +02:00
except AttributeError:
2023-03-09 12:33:02 +02:00
logWrite(f"from_user in function get_submission does not seem to contain id")