Improved naming

This commit is contained in:
2023-10-15 18:14:22 +02:00
parent c4d31c955f
commit a0616ff285
9 changed files with 201 additions and 184 deletions

View File

@@ -26,115 +26,121 @@ logger = logging.getLogger(__name__)
# | filters.animation
| filters.document
)
async def get_submission(app: PyroClient, msg: Message):
async def get_submission(app: PyroClient, message: Message):
global USERS_WITH_CONTEXT
if not hasattr(msg.from_user, "id"):
if not hasattr(message.from_user, "id"):
return
if msg.from_user.id in USERS_WITH_CONTEXT:
if message.from_user.id in USERS_WITH_CONTEXT:
return
user = await app.find_user(msg.from_user)
user = await app.find_user(message.from_user)
user_owner = await app.find_user(app.owner)
try:
if user.banned:
return
await app.send_chat_action(msg.chat.id, ChatAction.TYPING)
await app.send_chat_action(message.chat.id, ChatAction.TYPING)
save_tmp = True
contents = None
if await user.is_limited():
await msg.reply_text(
await message.reply_text(
app._("sub_cooldown", "message", locale=user.locale).format(
app.config["submission"]["timeout"]
)
)
return
if msg.document is not None:
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",
msg.from_user.id,
msg.document.mime_type,
msg.document.file_name,
msg.document.file_size / 1024 / 1024,
message.from_user.id,
message.document.mime_type,
message.document.file_name,
message.document.file_size / 1024 / 1024,
)
if msg.document.mime_type not in app.config["submission"]["mime_types"]:
await msg.reply_text(
if message.document.mime_type not in app.config["submission"]["mime_types"]:
await message.reply_text(
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"])
),
quote=True,
)
return
if msg.document.file_size > app.config["submission"]["file_size"]:
await msg.reply_text(
if message.document.file_size > app.config["submission"]["file_size"]:
await message.reply_text(
app._("document_too_large", "message", locale=user.locale).format(
app.config["submission"]["file_size"] / 1024 / 1024
),
quote=True,
)
return
if msg.document.file_size > app.config["submission"]["tmp_size"]:
if message.document.file_size > app.config["submission"]["tmp_size"]:
save_tmp = False
contents = (
msg.document.file_id,
message.document.file_id,
SubmissionType.DOCUMENT,
) # , msg.document.file_name
) # , message.document.file_name
if msg.video is not None:
if message.video is not None:
logger.info(
"User %s is trying to submit a video with name '%s' and size of %s MB",
msg.from_user.id,
msg.video.file_name,
msg.video.file_size / 1024 / 1024,
message.from_user.id,
message.video.file_name,
message.video.file_size / 1024 / 1024,
)
if msg.video.file_size > app.config["submission"]["file_size"]:
await msg.reply_text(
if message.video.file_size > app.config["submission"]["file_size"]:
await message.reply_text(
app._("document_too_large", "message", locale=user.locale).format(
app.config["submission"]["file_size"] / 1024 / 1024
),
quote=True,
)
return
if msg.video.file_size > app.config["submission"]["tmp_size"]:
if message.video.file_size > app.config["submission"]["tmp_size"]:
save_tmp = False
contents = msg.video.file_id, SubmissionType.VIDEO # , msg.video.file_name
contents = (
message.video.file_id,
SubmissionType.VIDEO,
) # , message.video.file_name
# if msg.animation is not None:
# if message.animation is not None:
# logger.info(
# "User %s is trying to submit an animation with name '%s' and size of %s MB",
# msg.from_user.id,
# msg.animation.file_name,
# msg.animation.file_size / 1024 / 1024,
# message.from_user.id,
# message.animation.file_name,
# message.animation.file_size / 1024 / 1024,
# )
# if msg.animation.file_size > app.config["submission"]["file_size"]:
# await msg.reply_text(
# if message.animation.file_size > app.config["submission"]["file_size"]:
# await message.reply_text(
# app._("document_too_large", "message", locale=user.locale).format(
# str(app.config["submission"]["file_size"] / 1024 / 1024)
# ),
# quote=True,
# )
# return
# if msg.animation.file_size > app.config["submission"]["tmp_size"]:
# if message.animation.file_size > app.config["submission"]["tmp_size"]:
# save_tmp = False
# contents = (
# msg.animation.file_id,
# message.animation.file_id,
# SubmissionType.ANIMATION,
# ) # , msg.animation.file_name
# ) # , message.animation.file_name
if msg.photo is not None:
if message.photo is not None:
logger.info(
"User %s is trying to submit a photo with ID '%s' and size of %s MB",
msg.from_user.id,
msg.photo.file_id,
msg.photo.file_size / 1024 / 1024,
message.from_user.id,
message.photo.file_id,
message.photo.file_size / 1024 / 1024,
)
contents = msg.photo.file_id, SubmissionType.PHOTO # , "please_generate"
contents = (
message.photo.file_id,
SubmissionType.PHOTO,
) # , "please_generate"
if contents is None:
return
@@ -148,33 +154,37 @@ async def get_submission(app: PyroClient, msg: Message):
exist_ok=True,
)
downloaded = await app.download_media(
msg,
message,
str(Path(f"{app.config['locations']['data']}/submissions/{tmp_id}"))
+ sep,
)
inserted = await col_submitted.insert_one(
{
"user": msg.from_user.id,
"user": message.from_user.id,
"date": datetime.now(),
"done": False,
"type": contents[1].value,
"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,
"telegram": {"msg_id": message.id, "file_id": contents[0]},
"caption": str(message.caption)
if message.caption is not None
else None,
}
)
else:
inserted = await col_submitted.insert_one(
{
"user": msg.from_user.id,
"user": message.from_user.id,
"date": datetime.now(),
"done": False,
"type": contents[1].value,
"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,
"telegram": {"msg_id": message.id, "file_id": contents[0]},
"caption": str(message.caption)
if message.caption is not None
else None,
}
)
@@ -187,8 +197,8 @@ async def get_submission(app: PyroClient, msg: Message):
]
]
if msg.caption is not None:
caption = str(msg.caption)
if message.caption is not None:
caption = str(message.caption)
buttons[0].append(
InlineKeyboardButton(
text=app._("sub_yes_caption", "button", locale=user_owner.locale),
@@ -206,32 +216,34 @@ async def get_submission(app: PyroClient, msg: Message):
)
caption += app._("sub_by", "message", locale=user_owner.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})"
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})"
if (
msg.from_user.id in app.admins
message.from_user.id in app.admins
and app.config["submission"]["require_confirmation"]["admins"] is False
):
try:
submitted = await app.submit_media(str(inserted.inserted_id))
await msg.reply_text(
await message.reply_text(
app._("sub_yes_auto", "message", locale=user.locale),
disable_notification=True,
quote=True,
)
if app.config["submission"]["send_uploaded_id"]:
caption += f"\n\nID: `{submitted[1]}`"
await msg.copy(app.owner, caption=caption, disable_notification=True)
await message.copy(
app.owner, caption=caption, disable_notification=True
)
return
except SubmissionUnsupportedError:
await msg.reply_text(
await message.reply_text(
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"]), quote=True
),
@@ -239,7 +251,7 @@ async def get_submission(app: PyroClient, msg: Message):
)
return
except SubmissionDuplicatesError as exc:
await msg.reply_text(
await message.reply_text(
app._(
"sub_media_duplicates_list", "message", locale=user.locale
).format("\n".join(exc.duplicates)),
@@ -247,32 +259,32 @@ async def get_submission(app: PyroClient, msg: Message):
)
return
except Exception as exc:
await msg.reply_text(exc, quote=True)
await message.reply_text(exc, quote=True)
return
elif (
msg.from_user.id not in app.admins
message.from_user.id not in app.admins
and app.config["submission"]["require_confirmation"]["users"] is False
):
try:
submitted = await app.submit_photo(str(inserted.inserted_id))
await msg.reply_text(
await message.reply_text(
app._("sub_yes_auto", "message", locale=user.locale),
disable_notification=True,
quote=True,
)
if app.config["submission"]["send_uploaded_id"]:
caption += f"\n\nID: `{submitted[1]}`"
await msg.copy(app.owner, caption=caption)
await message.copy(app.owner, caption=caption)
return
except SubmissionUnsupportedError:
await msg.reply_text(
await message.reply_text(
app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"]), quote=True
)
)
return
except SubmissionDuplicatesError as exc:
await msg.reply_text(
await message.reply_text(
app._("sub_dup", "message", locale=user.locale), quote=True
)
return
@@ -281,31 +293,31 @@ async def get_submission(app: PyroClient, msg: Message):
app.owner,
app._(
"sub_error_admin", "message", locale=user_owner.locale
).format(msg.from_user.id, format_exc()),
).format(message.from_user.id, format_exc()),
)
await msg.reply_text("sub_error", quote=True)
await message.reply_text("sub_error", quote=True)
return
if msg.from_user.id not in app.admins:
if message.from_user.id not in app.admins:
buttons += [
[
InlineKeyboardButton(
text=app._("sub_block", "button", locale=user_owner.locale),
callback_data=f"sub_block_{msg.from_user.id}",
callback_data=f"sub_block_{message.from_user.id}",
)
]
]
await user.update_cooldown()
if msg.from_user.id != app.owner:
await msg.reply_text(
if message.from_user.id != app.owner:
await message.reply_text(
app._("sub_sent", "message", locale=user.locale),
disable_notification=True,
quote=True,
)
await msg.copy(
await message.copy(
app.owner, caption=caption, reply_markup=InlineKeyboardMarkup(buttons)
)