notes.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. MIT License
  3. Copyright (c) 2023 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from re import findall
  21. from pyrogram import filters
  22. from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.core.decorators.permissions import adminsOnly
  25. from wbb.core.keyboard import ikb
  26. from wbb.utils.dbfunctions import delete_note, get_note, get_note_names, save_note
  27. from wbb.utils.functions import extract_text_and_keyb
  28. __MODULE__ = "Notes"
  29. __HELP__ = """/notes To Get All The Notes In The Chat.
  30. /save [NOTE_NAME] To Save A Note (Can be a sticker or text).
  31. #NOTE_NAME To Get A Note.
  32. /delete [NOTE_NAME] To Delete A Note.
  33. Checkout /markdownhelp to know more about formattings and other syntax.
  34. """
  35. @app2.on_message(
  36. filters.command("save", prefixes=USERBOT_PREFIX)
  37. & ~filters.forwarded
  38. & ~filters.via_bot
  39. & SUDOERS
  40. )
  41. @app.on_message(filters.command("save") & ~filters.private)
  42. @adminsOnly("can_change_info")
  43. async def save_notee(_, message):
  44. if len(message.command) < 2 or not message.reply_to_message:
  45. await eor(
  46. message,
  47. text="**Usage:**\nReply to a text or sticker with /save [NOTE_NAME] to save it.",
  48. )
  49. elif not message.reply_to_message.text and not message.reply_to_message.sticker:
  50. await eor(
  51. message,
  52. text="__**You can only save text or stickers in notes.**__",
  53. )
  54. else:
  55. name = message.text.split(None, 1)[1].strip()
  56. if not name:
  57. return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
  58. _type = "text" if message.reply_to_message.text else "sticker"
  59. note = {
  60. "type": _type,
  61. "data": message.reply_to_message.text.markdown
  62. if _type == "text"
  63. else message.reply_to_message.sticker.file_id,
  64. }
  65. prefix = message.text.split()[0][0]
  66. chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID
  67. await save_note(chat_id, name, note)
  68. await eor(message, text=f"__**Saved note {name}.**__")
  69. @app2.on_message(
  70. filters.command("notes", prefixes=USERBOT_PREFIX)
  71. & ~filters.forwarded
  72. & ~filters.via_bot
  73. & SUDOERS
  74. )
  75. @app.on_message(filters.command("notes") & ~filters.private)
  76. @capture_err
  77. async def get_notes(_, message):
  78. prefix = message.text.split()[0][0]
  79. is_ubot = bool(prefix == USERBOT_PREFIX)
  80. chat_id = USERBOT_ID if is_ubot else message.chat.id
  81. _notes = await get_note_names(chat_id)
  82. if not _notes:
  83. return await eor(message, text="**No notes in this chat.**")
  84. _notes.sort()
  85. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  86. for note in _notes:
  87. msg += f"**-** `{note}`\n"
  88. await eor(message, text=msg)
  89. @app2.on_message(
  90. filters.command("get", prefixes=USERBOT_PREFIX)
  91. & ~filters.forwarded
  92. & ~filters.via_bot
  93. & SUDOERS
  94. )
  95. async def get_one_note_userbot(_, message):
  96. if len(message.text.split()) < 2:
  97. return await eor(message, text="Invalid arguments")
  98. name = message.text.split(None, 1)[1]
  99. _note = await get_note(USERBOT_ID, name)
  100. if not _note:
  101. return await eor(message, text="No such note.")
  102. if _note["type"] == "text":
  103. data = _note["data"]
  104. await eor(
  105. message,
  106. text=data,
  107. disable_web_page_preview=True,
  108. )
  109. else:
  110. await message.reply_sticker(_note["data"])
  111. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  112. @capture_err
  113. async def get_one_note(_, message):
  114. name = message.text.replace("#", "", 1)
  115. if not name:
  116. return
  117. _note = await get_note(message.chat.id, name)
  118. if not _note:
  119. return
  120. if _note["type"] == "text":
  121. data = _note["data"]
  122. keyb = None
  123. if findall(r"\[.+\,.+\]", data):
  124. keyboard = extract_text_and_keyb(ikb, data)
  125. if keyboard:
  126. data, keyb = keyboard
  127. await message.reply_text(
  128. data,
  129. reply_markup=keyb,
  130. disable_web_page_preview=True,
  131. )
  132. else:
  133. await message.reply_sticker(_note["data"])
  134. @app2.on_message(
  135. filters.command("delete", prefixes=USERBOT_PREFIX)
  136. & ~filters.forwarded
  137. & ~filters.via_bot
  138. & SUDOERS
  139. )
  140. @app.on_message(filters.command("delete") & ~filters.private)
  141. @adminsOnly("can_change_info")
  142. async def del_note(_, message):
  143. if len(message.command) < 2:
  144. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  145. name = message.text.split(None, 1)[1].strip()
  146. if not name:
  147. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  148. prefix = message.text.split()[0][0]
  149. is_ubot = bool(prefix == USERBOT_PREFIX)
  150. chat_id = USERBOT_ID if is_ubot else message.chat.id
  151. deleted = await delete_note(chat_id, name)
  152. if deleted:
  153. await eor(message, text=f"**Deleted note {name} successfully.**")
  154. else:
  155. await eor(message, text="**No such note.**")