notes.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
  23. from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.core.decorators.permissions import adminsOnly
  26. from wbb.core.keyboard import ikb
  27. from wbb.utils.dbfunctions import delete_note, get_note, get_note_names, save_note, deleteall_notes
  28. from wbb.modules.admin import member_permissions
  29. from wbb.utils.functions import extract_text_and_keyb
  30. __MODULE__ = "Notes"
  31. __HELP__ = """/notes To Get All The Notes In The Chat.
  32. /save [NOTE_NAME] To Save A Note.
  33. Supported note types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice.
  34. To change caption of any files use.\n/save [NOTE_NAME] [NEW_CAPTION].
  35. #NOTE_NAME To Get A Note.
  36. /delete [NOTE_NAME] To Delete A Note.
  37. /deleteall To delete all the notes in a chat (permanently).
  38. Checkout /markdownhelp to know more about formattings and other syntax.
  39. """
  40. def extract_urls(reply_markup):
  41. urls = []
  42. if reply_markup.inline_keyboard:
  43. buttons = reply_markup.inline_keyboard
  44. for i, row in enumerate(buttons):
  45. for j, button in enumerate(row):
  46. if button.url:
  47. name = "\n~\nbutton" if i * len(row) + j + 1 == 1 else f"button{i * len(row) + j + 1}"
  48. urls.append((f"{name}", button.text, button.url))
  49. return urls
  50. @app2.on_message(
  51. filters.command("save", prefixes=USERBOT_PREFIX)
  52. & ~filters.forwarded
  53. & ~filters.via_bot
  54. & SUDOERS
  55. )
  56. @app.on_message(filters.command("save") & ~filters.private)
  57. @adminsOnly("can_change_info")
  58. async def save_notee(_, message):
  59. try:
  60. if len(message.command) < 2 or not message.reply_to_message:
  61. await eor(
  62. message,
  63. text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
  64. )
  65. else:
  66. text = message.text.markdown
  67. name = text.split(None, 1)[1].strip()
  68. if not name:
  69. return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
  70. replied_message = message.reply_to_message
  71. text = name.split(" ", 1)
  72. if len(text) > 1:
  73. name = text[0]
  74. data = text[1].strip()
  75. if replied_message.sticker or replied_message.video_note:
  76. data = None
  77. else:
  78. if replied_message.sticker or replied_message.video_note:
  79. data = None
  80. elif not replied_message.text and not replied_message.caption:
  81. data = None
  82. else:
  83. data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
  84. if replied_message.text:
  85. _type = "text"
  86. file_id = None
  87. if replied_message.sticker:
  88. _type = "sticker"
  89. file_id = replied_message.sticker.file_id
  90. if replied_message.animation:
  91. _type = "animation"
  92. file_id = replied_message.animation.file_id
  93. if replied_message.photo:
  94. _type = "photo"
  95. file_id = replied_message.photo.file_id
  96. if replied_message.document:
  97. _type = "document"
  98. file_id = replied_message.document.file_id
  99. if replied_message.video:
  100. _type = "video"
  101. file_id = replied_message.video.file_id
  102. if replied_message.video_note:
  103. _type = "video_note"
  104. file_id = replied_message.video_note.file_id
  105. if replied_message.audio:
  106. _type = "audio"
  107. file_id = replied_message.audio.file_id
  108. if replied_message.voice:
  109. _type = "voice"
  110. file_id = replied_message.voice.file_id
  111. if replied_message.reply_markup and not "~" in data:
  112. urls = extract_urls(replied_message.reply_markup)
  113. if urls:
  114. response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
  115. data = data + response
  116. note = {
  117. "type": _type,
  118. "data": data,
  119. "file_id": file_id,
  120. }
  121. prefix = message.text.split()[0][0]
  122. chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID
  123. await save_note(chat_id, name, note)
  124. await eor(message, text=f"__**Saved note {name}.**__")
  125. except UnboundLocalError:
  126. return await message.reply_text("**Replied message is inaccessible.\n`Forward the message and try again`**")
  127. @app2.on_message(
  128. filters.command("notes", prefixes=USERBOT_PREFIX)
  129. & ~filters.forwarded
  130. & ~filters.via_bot
  131. & SUDOERS
  132. )
  133. @app.on_message(filters.command("notes") & ~filters.private)
  134. @capture_err
  135. async def get_notes(_, message):
  136. prefix = message.text.split()[0][0]
  137. is_ubot = bool(prefix == USERBOT_PREFIX)
  138. chat_id = USERBOT_ID if is_ubot else message.chat.id
  139. _notes = await get_note_names(chat_id)
  140. if not _notes:
  141. return await eor(message, text="**No notes in this chat.**")
  142. _notes.sort()
  143. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  144. for note in _notes:
  145. msg += f"**-** `{note}`\n"
  146. await eor(message, text=msg)
  147. @app2.on_message(
  148. filters.command("get", prefixes=USERBOT_PREFIX)
  149. & ~filters.forwarded
  150. & ~filters.via_bot
  151. & SUDOERS
  152. )
  153. async def get_one_note_userbot(_, message):
  154. if len(message.text.split()) < 2:
  155. return await eor(message, text="Invalid arguments")
  156. name = message.text.split(None, 1)[1]
  157. _note = await get_note(USERBOT_ID, name)
  158. if not _note:
  159. return await eor(message, text="No such note.")
  160. if _note["type"] == "text":
  161. data = _note["data"]
  162. await eor(
  163. message,
  164. text=data,
  165. disable_web_page_preview=True,
  166. )
  167. else:
  168. await message.reply_sticker(_note["data"])
  169. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  170. @capture_err
  171. async def get_one_note(_, message):
  172. name = message.text.replace("#", "", 1)
  173. if not name:
  174. return
  175. _note = await get_note(message.chat.id, name)
  176. if not _note:
  177. return
  178. type = _note["type"]
  179. data = _note["data"]
  180. file_id = _note["file_id"]
  181. keyb = None
  182. if data:
  183. if findall(r"\[.+\,.+\]", data):
  184. keyboard = extract_text_and_keyb(ikb, data)
  185. if keyboard:
  186. data, keyb = keyboard
  187. replied_message = message.reply_to_message
  188. if replied_message:
  189. if replied_message.from_user.id != message.from_user.id:
  190. message = replied_message
  191. if type == "text":
  192. await message.reply_text(
  193. text=data,
  194. reply_markup=keyb,
  195. disable_web_page_preview=True,
  196. )
  197. if type == "sticker":
  198. await message.reply_sticker(
  199. sticker=file_id,
  200. )
  201. if type == "animation":
  202. await message.reply_animation(
  203. animation=file_id,
  204. caption=data,
  205. reply_markup=keyb,
  206. )
  207. if type == "photo":
  208. await message.reply_photo(
  209. photo=file_id,
  210. caption=data,
  211. reply_markup=keyb,
  212. )
  213. if type == "document":
  214. await message.reply_document(
  215. document=file_id,
  216. caption=data,
  217. reply_markup=keyb,
  218. )
  219. if type == "video":
  220. await message.reply_video(
  221. video=file_id,
  222. caption=data,
  223. reply_markup=keyb,
  224. )
  225. if type == "video_note":
  226. await message.reply_video_note(
  227. video_note=file_id,
  228. )
  229. if type == "audio":
  230. await message.reply_audio(
  231. audio=file_id,
  232. caption=data,
  233. reply_markup=keyb,
  234. )
  235. if type == "voice":
  236. await message.reply_voice(
  237. voice=file_id,
  238. caption=data,
  239. reply_markup=keyb,
  240. )
  241. @app2.on_message(
  242. filters.command("delete", prefixes=USERBOT_PREFIX)
  243. & ~filters.forwarded
  244. & ~filters.via_bot
  245. & SUDOERS
  246. )
  247. @app.on_message(filters.command("delete") & ~filters.private)
  248. @adminsOnly("can_change_info")
  249. async def del_note(_, message):
  250. if len(message.command) < 2:
  251. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  252. name = message.text.split(None, 1)[1].strip()
  253. if not name:
  254. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  255. prefix = message.text.split()[0][0]
  256. is_ubot = bool(prefix == USERBOT_PREFIX)
  257. chat_id = USERBOT_ID if is_ubot else message.chat.id
  258. deleted = await delete_note(chat_id, name)
  259. if deleted:
  260. await eor(message, text=f"**Deleted note {name} successfully.**")
  261. else:
  262. await eor(message, text="**No such note.**")
  263. @app.on_message(filters.command("deleteall") & ~filters.private)
  264. @adminsOnly("can_change_info")
  265. async def delete_all(_, message):
  266. _notes = await get_note_names(message.chat.id)
  267. if not _notes:
  268. return await message.reply_text("**No notes in this chat.**")
  269. else:
  270. keyboard = InlineKeyboardMarkup(
  271. [
  272. [InlineKeyboardButton("YES, DO IT", callback_data="delete_yes"),
  273. InlineKeyboardButton("Cancel", callback_data="delete_no")
  274. ]
  275. ]
  276. )
  277. await message.reply_text("**Are you sure you want to delete all the notes in this chat forever ?.**", reply_markup=keyboard)
  278. @app.on_callback_query(filters.regex("delete_(.*)"))
  279. async def delete_all_cb(_, cb):
  280. chat_id = cb.message.chat.id
  281. from_user = cb.from_user
  282. permissions = await member_permissions(chat_id, from_user.id)
  283. permission = "can_change_info"
  284. if permission not in permissions:
  285. return await cb.answer(f"You don't have the required permission.\n Permission: {permission}", show_alert=True)
  286. input = cb.data.split("_", 1)[1]
  287. if input == "yes":
  288. stoped_all = await deleteall_notes(chat_id)
  289. if stoped_all:
  290. return await cb.message.edit("**Successfully deleted all notes on this chat.**")
  291. if input == "no":
  292. await cb.message.reply_to_message.delete()
  293. await cb.message.delete()