From 03ebeafdd4171cc412c88b5fe775eb5e24a11883 Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Sun, 11 Dec 2022 18:50:50 +0100 Subject: [PATCH] Messages command updated --- modules/commands/message.py | 5 ++--- modules/handlers/everything.py | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/modules/commands/message.py b/modules/commands/message.py index 6ff544b..63274cd 100644 --- a/modules/commands/message.py +++ b/modules/commands/message.py @@ -3,6 +3,7 @@ from app import app, isAnAdmin from pyrogram import filters from pyrogram.errors import bad_request_400 from modules.utils import jsonLoad, jsonSave, logWrite, locale, configGet, should_quote +from modules.database import col_messages # Message command ============================================================================================================== @app.on_message(~ filters.scheduled & filters.command(["message"], prefixes=["/"])) @@ -35,9 +36,7 @@ async def cmd_message(app, msg): new_message = await app.send_message(destination.id, message+locale("message_reply_notice", "message")) await msg.reply_text(locale("message_sent", "message"), quote=should_quote(msg)) logWrite(f"Admin {msg.from_user.id} sent message '{' '.join(msg.command[2:])}' to {destination.id}") - messages = jsonLoad(f"{configGet('data', 'locations')}{sep}messages.json") - messages.append({"origin": {"chat": msg.chat.id, "id": msg.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) - jsonSave(messages, f"{configGet('data', 'locations')}{sep}messages.json") + col_messages.insert_one({"origin": {"chat": msg.chat.id, "id": msg.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) except bad_request_400.PeerIdInvalid: await msg.reply_text(locale("message_no_user", "message"), quote=should_quote(msg)) logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to {destination.id} but 'PeerIdInvalid'") diff --git a/modules/handlers/everything.py b/modules/handlers/everything.py index 5032ad9..5ce2e8c 100644 --- a/modules/handlers/everything.py +++ b/modules/handlers/everything.py @@ -5,19 +5,19 @@ import asyncio from pyrogram import filters from pyrogram.types import ForceReply, ReplyKeyboardMarkup, Message from modules.utils import configGet, configSet, jsonLoad, jsonSave, locale, logWrite, should_quote +from modules.database import col_messages -async def message_involved(msg: Message): - messages = jsonLoad(f"{configGet('data', 'locations')}{sep}messages.json") - for message in messages: - if (message["destination"]["id"] == msg.reply_to_message.id) and (message["destination"]["chat"] == msg.reply_to_message.chat.id): - return True +async def message_involved(msg: Message) -> bool: + message = col_messages.find_one({"destination.id": msg.reply_to_message.id, "destination.chat": msg.reply_to_message.chat.id}) + if message is not None: + return True return False -async def message_context(msg: Message): - messages = jsonLoad(f"{configGet('data', 'locations')}{sep}messages.json") - for message in messages: - if (message["destination"]["id"] == msg.reply_to_message.id) and (message["destination"]["chat"] == msg.reply_to_message.chat.id): - return message["origin"]["chat"], message["origin"]["id"] +async def message_context(msg: Message) -> tuple: + message = col_messages.find_one({"destination.id": msg.reply_to_message.id, "destination.chat": msg.reply_to_message.chat.id}) + if message is not None: + return message["origin"]["chat"], message["origin"]["id"] + return 0, 0 # Any other input ============================================================================================================== @app.on_message(~ filters.scheduled & filters.private) @@ -32,9 +32,7 @@ async def any_stage(app, msg): else: new_message = await (await app.get_messages(context[0], context[1])).reply_text(locale("message_from", "message").format(msg.from_user.first_name, msg.from_user.id)+msg.text+locale("message_reply_notice", "message"), quote=True) await msg.reply_text(locale("message_sent", "message"), quote=should_quote(msg)) - messages = jsonLoad(f"{configGet('data', 'locations')}{sep}messages.json") - messages.append({"origin": {"chat": msg.chat.id, "id": msg.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) - jsonSave(messages, f"{configGet('data', 'locations')}{sep}messages.json") + col_messages.insert_one({"origin": {"chat": msg.chat.id, "id": msg.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) return user_stage = configGet("stage", file=str(msg.from_user.id))