From 85d0a1d4ae43d5c1d6518ae20bf764f3ec4270e6 Mon Sep 17 00:00:00 2001 From: yasir Date: Thu, 8 Dec 2022 11:56:03 +0700 Subject: [PATCH] Tes add notes from wbb --- database/notes_db.py | 50 +++++++++ misskaty/core/custom_filter.py | 8 ++ misskaty/core/decorator/permissions.py | 12 ++- misskaty/plugins/dev.py | 7 +- misskaty/plugins/filters.py | 9 +- misskaty/plugins/notes.py | 135 +++++++++++++++++++++++++ 6 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 database/notes_db.py create mode 100644 misskaty/core/custom_filter.py create mode 100644 misskaty/plugins/notes.py diff --git a/database/notes_db.py b/database/notes_db.py new file mode 100644 index 00000000..412356c1 --- /dev/null +++ b/database/notes_db.py @@ -0,0 +1,50 @@ +from database import dbname +from typing import Dict, Union, List + +notesdb = dbname.notes + + +async def _get_notes(chat_id: int) -> Dict[str, int]: + _notes = await notesdb.find_one({"chat_id": chat_id}) + if not _notes: + return {} + return _notes["notes"] + + +async def delete_note(chat_id: int, name: str) -> bool: + notesd = await _get_notes(chat_id) + name = name.lower().strip() + if name in notesd: + del notesd[name] + await notesdb.update_one( + {"chat_id": chat_id}, + {"$set": {"notes": notesd}}, + upsert=True, + ) + return True + return False + + +async def get_note(chat_id: int, name: str) -> Union[bool, dict]: + name = name.lower().strip() + _notes = await _get_notes(chat_id) + if name in _notes: + return _notes[name] + return False + + +async def get_note_names(chat_id: int) -> List[str]: + _notes = [] + for note in await _get_notes(chat_id): + _notes.append(note) + return _notes + + +async def save_note(chat_id: int, name: str, note: dict): + name = name.lower().strip() + _notes = await _get_notes(chat_id) + _notes[name] = note + + await notesdb.update_one( + {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True + ) diff --git a/misskaty/core/custom_filter.py b/misskaty/core/custom_filter.py new file mode 100644 index 00000000..91343572 --- /dev/null +++ b/misskaty/core/custom_filter.py @@ -0,0 +1,8 @@ +from pyrogram import filters + + +def pesanedit(_, __, m: Message): + return bool(m.edit_date) + + +edited = filters.create(pesanedit) diff --git a/misskaty/core/decorator/permissions.py b/misskaty/core/decorator/permissions.py index dcd5974f..a6964e37 100644 --- a/misskaty/core/decorator/permissions.py +++ b/misskaty/core/decorator/permissions.py @@ -49,7 +49,12 @@ async def list_admins(chat_id: int): admins_in_chat[chat_id] = { "last_updated_at": time(), - "data": [member.user.id async for member in app.get_chat_members(chat_id, filter=enums.ChatMembersFilter.ADMINISTRATORS)], + "data": [ + member.user.id + async for member in app.get_chat_members( + chat_id, filter=enums.ChatMembersFilter.ADMINISTRATORS + ) + ], } return admins_in_chat[chat_id]["data"] @@ -72,7 +77,10 @@ async def authorised(func, subFunc2, client, message, *args, **kwargs): async def unauthorised(message: Message, permission, subFunc2): chatID = message.chat.id - text = "You don't have the required permission to perform this action." + f"\n**Permission:** __{permission}__" + text = ( + "You don't have the required permission to perform this action." + + f"\n**Permission:** __{permission}__" + ) try: await message.reply_text(text) except ChatWriteForbidden: diff --git a/misskaty/plugins/dev.py b/misskaty/plugins/dev.py index c7f5c245..244f45cf 100644 --- a/misskaty/plugins/dev.py +++ b/misskaty/plugins/dev.py @@ -4,8 +4,9 @@ import os import traceback import asyncio from pyrogram import filters, enums -from misskaty.vars import COMMAND_HANDLER, SUDO from misskaty import app +from misskaty.vars import COMMAND_HANDLER, SUDO +from misskaty.core.custom_filter import edited __MODULE__ = "DevCommand" __HELP__ = """ @@ -19,7 +20,7 @@ __HELP__ = """ """ -@app.on_message(filters.command(["logs"]) & filters.user(SUDO)) +@app.on_message(filters.command(["logs"], COMMAND_HANDLER) & filters.user(SUDO)) async def log_file(bot, message): """Send log file""" try: @@ -75,7 +76,7 @@ async def shell(client, message): await message.reply("No Reply") -@app.on_message(filters.command(["ev", "run"]) & filters.user(SUDO)) +@app.on_message(filters.command(["ev", "run"], COMMAND_HANDLER) & filters.user(SUDO)) @app.on_edited_message(filters.command(["ev", "run"]) & filters.user(SUDO)) async def evaluation_cmd_t(client, message): status_message = await message.reply("__Processing eval pyrogram...__") diff --git a/misskaty/plugins/filters.py b/misskaty/plugins/filters.py index d6c41c2c..e8bf764a 100644 --- a/misskaty/plugins/filters.py +++ b/misskaty/plugins/filters.py @@ -68,7 +68,6 @@ async def save_filters(_, message): } await save_filter(chat_id, name, _filter) await message.reply(f"__**Saved filter {name}.**__") - await message.stop_propagation() @app.on_message(filters.command("filters") & ~filters.private) @@ -135,11 +134,9 @@ async def filters_re(_, message): await message.delete() return - return await app.send_message( - message.chat.id, + return await message.reply( data, reply_markup=keyb, - reply_to_message_id=message.id, disable_web_page_preview=True, ) if message.reply_to_message: @@ -148,6 +145,4 @@ async def filters_re(_, message): if text.startswith("~"): await message.delete() return - await app.send_sticker( - message.chat.id, data, reply_to_message_id=message.id - ) + await message.reply_sticker(data) diff --git a/misskaty/plugins/notes.py b/misskaty/plugins/notes.py new file mode 100644 index 00000000..9685840a --- /dev/null +++ b/misskaty/plugins/notes.py @@ -0,0 +1,135 @@ +""" +MIT License + +Copyright (c) 2021 TheHamkerCat + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +from re import findall +from pyrogram import filters +from misskaty import app +from misskaty.vars import COMMAND_HANDLER +from misskaty.core.decorator.errors import capture_err +from misskaty.core.decorator.permissions import adminsOnly +from misskaty.core.keyboard import ikb +from database.notes_db import ( + delete_note, + get_note, + get_note_names, + save_note, +) +from misskaty.helper.functions import extract_text_and_keyb + +__MODULE__ = "Notes" +__HELP__ = """/notes To Get All The Notes In The Chat. + +/addnote [NOTE_NAME] To Save A Note (Can be a sticker or text). + +#NOTE_NAME To Get A Note. + +/delnote [NOTE_NAME] To Delete A Note. +""" + + +@app.on_message(filters.command("addnote") & ~filters.private) +@adminsOnly("can_change_info") +async def save_notee(_, message): + if len(message.command) < 2 or not message.reply_to_message: + await message.reply( + text="**Usage:**\nReply to a text or sticker with /save [NOTE_NAME] to save it.", + ) + + elif not message.reply_to_message.text and not message.reply_to_message.sticker: + await message.reply("__**You can only save text or stickers in notes.**__") + else: + name = message.text.split(None, 1)[1].strip() + if not name: + return await message.reply("**Usage**\n__/save [NOTE_NAME]__") + _type = "text" if message.reply_to_message.text else "sticker" + note = { + "type": _type, + "data": message.reply_to_message.text.markdown + if _type == "text" + else message.reply_to_message.sticker.file_id, + } + prefix = message.text.split()[0][0] + chat_id = message.chat.id + await save_note(chat_id, name, note) + await message.reply(f"__**Saved note {name}.**__") + + +@app.on_message(filters.command("notes") & ~filters.private) +@capture_err +async def get_notes(_, message): + prefix = message.text.split()[0][0] + chat_id = message.chat.id + + _notes = await get_note_names(chat_id) + + if not _notes: + return await message.reply("**No notes in this chat.**") + _notes.sort() + msg = f"List of notes in {message.chat.title}\n" + for note in _notes: + msg += f"**-** `{note}`\n" + await message.reply(msg) + + +@app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private) +@capture_err +async def get_one_note(_, message): + name = message.text.replace("#", "", 1) + if not name: + return + _note = await get_note(message.chat.id, name) + if not _note: + return + if _note["type"] == "text": + data = _note["data"] + keyb = None + if findall(r"\[.+\,.+\]", data): + keyboard = extract_text_and_keyb(ikb, data) + if keyboard: + data, keyb = keyboard + await message.reply_text( + data, + reply_markup=keyb, + disable_web_page_preview=True, + ) + else: + await message.reply_sticker(_note["data"]) + + +@app.on_message(filters.command("delnote") & ~filters.private) +@adminsOnly("can_change_info") +async def del_note(_, message): + if len(message.command) == 1: + return await message.reply("**Usage**\n__/delete [NOTE_NAME]__") + name = message.text.split(None, 1)[1].strip() + if not name: + return await message.reply("**Usage**\n__/delete [NOTE_NAME]__") + + prefix = message.text.split()[0][0] + chat_id = message.chat.id + + deleted = await delete_note(chat_id, name) + if deleted: + await message.reply(f"**Deleted note {name} successfully.**") + else: + await message.reply("**No such note.**")