Telegram/modules/commands/message.py

98 lines
3.6 KiB
Python

from app import app, isAnAdmin
from pyrogram import filters
from pyrogram.types import Message
from pyrogram.client import Client
from convopyro import listen_message
from classes.errors.holo_user import UserInvalidError
from classes.holo_user import HoloUser
from modules.utils import configGet, logWrite, locale, should_quote, find_user
from modules import custom_filters
from modules.database import col_messages
@app.on_message(
custom_filters.enabled_general
& ~filters.scheduled
& filters.command(["message"], prefixes=["/"])
# & custom_filters.admin
)
async def cmd_message(app: Client, msg: Message):
try:
if await isAnAdmin(msg.from_user.id):
try:
destination = HoloUser(int(msg.command[1]))
except (ValueError, UserInvalidError):
destination = HoloUser(await find_user(app, query=msg.command[1]))
if (msg.text is not None) and (len(str(msg.text).split()) > 2):
await destination.message(
context=msg,
text=" ".join(str(msg.text).split()[2:]),
caption=msg.caption,
photo=msg.photo,
video=msg.video,
file=msg.document,
voice=msg.voice,
animation=msg.animation,
adm_context=True,
)
elif (msg.caption is not None) and (len(msg.caption.split()) > 2):
await destination.message(
context=msg,
text=str(msg.text),
caption=" ".join(msg.caption.split()[2:]),
photo=msg.photo,
video=msg.video,
file=msg.document,
voice=msg.voice,
animation=msg.animation,
adm_context=True,
)
else:
await destination.message(
context=msg,
text=None,
caption=None,
photo=msg.photo,
video=msg.video,
file=msg.document,
voice=msg.voice,
animation=msg.animation,
adm_context=True,
)
else:
await msg.reply_text(
locale("message_enter", "message", locale=msg.from_user)
)
message = await listen_message(app, msg.chat.id)
if (
message is None
or message.text is not None
and message.text == "/cancel"
):
return
sent = await app.forward_messages(
configGet("admin", "groups"), msg.chat.id, message.id
)
col_messages.insert_one(
{
"origin": {"chat": message.chat.id, "id": message.id},
"destination": {"chat": sent.chat.id, "id": sent.id},
}
)
await message.reply_text(
locale("message_sent", "message", locale=message.from_user), quote=True
)
except IndexError:
await msg.reply_text(
locale("message_invalid_syntax", "message", locale=msg.from_user),
quote=should_quote(msg),
)
logWrite(f"Admin {msg.from_user.id} tried to send message but 'IndexError'")
except ValueError:
await msg.reply_text(
locale("message_invalid_syntax", "message", locale=msg.from_user),
quote=should_quote(msg),
)
logWrite(f"Admin {msg.from_user.id} tried to send message but 'ValueError'")