notes.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """
  2. MIT License
  3. Copyright (c) present 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 (
  27. delete_note,
  28. get_note,
  29. get_note_names,
  30. save_note,
  31. )
  32. from wbb.utils.functions import extract_text_and_keyb
  33. __MODULE__ = "Notes"
  34. __HELP__ = """/notes To Get All The Notes In The Chat.
  35. /save [NOTE_NAME] To Save A Note (Can be a sticker or text).
  36. #NOTE_NAME To Get A Note.
  37. /delete [NOTE_NAME] To Delete A Note.
  38. Checkout /markdownhelp to know more about formattings and other syntax.
  39. """
  40. @app2.on_message(
  41. filters.command("save", prefixes=USERBOT_PREFIX)
  42. & SUDOERS
  43. )
  44. @app.on_message(
  45. filters.command("save")
  46. & ~filters.private
  47. )
  48. @adminsOnly("can_change_info")
  49. async def save_notee(_, message):
  50. if len(message.command) < 2 or not message.reply_to_message:
  51. await eor(
  52. message,
  53. text="**Usage:**\nReply to a text or sticker with /save [NOTE_NAME] to save it.",
  54. )
  55. elif (
  56. not message.reply_to_message.text
  57. and not message.reply_to_message.sticker
  58. ):
  59. await eor(
  60. message,
  61. text="__**You can only save text or stickers in notes.**__",
  62. )
  63. else:
  64. name = message.text.split(None, 1)[1].strip()
  65. if not name:
  66. return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
  67. _type = "text" if message.reply_to_message.text else "sticker"
  68. note = {
  69. "type": _type,
  70. "data": message.reply_to_message.text.markdown
  71. if _type == "text"
  72. else message.reply_to_message.sticker.file_id,
  73. }
  74. prefix = message.text.split()[0][0]
  75. chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID
  76. await save_note(chat_id, name, note)
  77. await eor(message, text=f"__**Saved note {name}.**__")
  78. @app2.on_message(
  79. filters.command("notes", prefixes=USERBOT_PREFIX)
  80. & SUDOERS
  81. )
  82. @app.on_message(
  83. filters.command("notes")
  84. & ~filters.private
  85. )
  86. @capture_err
  87. async def get_notes(_, message):
  88. prefix = message.text.split()[0][0]
  89. is_ubot = bool(prefix == USERBOT_PREFIX)
  90. chat_id = USERBOT_ID if is_ubot else message.chat.id
  91. _notes = await get_note_names(chat_id)
  92. if not _notes:
  93. return await eor(message, text="**No notes in this chat.**")
  94. _notes.sort()
  95. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  96. for note in _notes:
  97. msg += f"**-** `{note}`\n"
  98. await eor(message, text=msg)
  99. @app2.on_message(
  100. filters.command("get", prefixes=USERBOT_PREFIX)
  101. & SUDOERS
  102. )
  103. async def get_one_note_userbot(_, message):
  104. if len(message.text.split()) < 2:
  105. return await eor(message, text="Invalid arguments")
  106. name = message.text.split(None, 1)[1]
  107. _note = await get_note(USERBOT_ID, name)
  108. if not _note:
  109. return await eor(message, text="No such note.")
  110. if _note["type"] == "text":
  111. data = _note["data"]
  112. await eor(
  113. message,
  114. text=data,
  115. disable_web_page_preview=True,
  116. )
  117. else:
  118. await message.reply_sticker(_note["data"])
  119. @app.on_message(
  120. filters.regex(r"^#.+")
  121. & filters.text
  122. & ~filters.private
  123. )
  124. @capture_err
  125. async def get_one_note(_, message):
  126. name = message.text.replace("#", "", 1)
  127. if not name:
  128. return
  129. _note = await get_note(message.chat.id, name)
  130. if not _note:
  131. return
  132. if _note["type"] == "text":
  133. data = _note["data"]
  134. keyb = None
  135. if findall(r"\[.+\,.+\]", data):
  136. keyboard = extract_text_and_keyb(ikb, data)
  137. if keyboard:
  138. data, keyb = keyboard
  139. await message.reply_text(
  140. data,
  141. reply_markup=keyb,
  142. disable_web_page_preview=True,
  143. )
  144. else:
  145. await message.reply_sticker(_note["data"])
  146. @app2.on_message(
  147. filters.command("delete", prefixes=USERBOT_PREFIX)
  148. & SUDOERS
  149. )
  150. @app.on_message(
  151. filters.command("delete")
  152. & ~filters.private
  153. )
  154. @adminsOnly("can_change_info")
  155. async def del_note(_, message):
  156. if len(message.command) < 2:
  157. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  158. name = message.text.split(None, 1)[1].strip()
  159. if not name:
  160. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  161. prefix = message.text.split()[0][0]
  162. is_ubot = bool(prefix == USERBOT_PREFIX)
  163. chat_id = USERBOT_ID if is_ubot else message.chat.id
  164. deleted = await delete_note(chat_id, name)
  165. if deleted:
  166. await eor(message, text=f"**Deleted note {name} successfully.**")
  167. else:
  168. await eor(message, text="**No such note.**")