notes.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, check_format
  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. @app.on_message(filters.command("save") & ~filters.private)
  51. @adminsOnly("can_change_info")
  52. async def save_notee(_, message):
  53. try:
  54. if len(message.command) < 2:
  55. await eor(
  56. message,
  57. text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
  58. )
  59. else:
  60. replied_message = message.reply_to_message
  61. if not replied_message:
  62. replied_message = message
  63. text = message.text.markdown if message.text else message.caption.markdown
  64. name = text.split(None, 1)[1].strip()
  65. if not name:
  66. return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
  67. text = name.split(" ", 1)
  68. if len(text) > 1:
  69. name = text[0]
  70. data = text[1].strip()
  71. if replied_message and (replied_message.sticker or replied_message.video_note):
  72. data = None
  73. else:
  74. if replied_message and (replied_message.sticker or replied_message.video_note):
  75. data = None
  76. elif replied_message and not replied_message.text and not replied_message.caption:
  77. data = None
  78. else:
  79. data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
  80. match = "/save " + name
  81. if not message.reply_to_message and message.text:
  82. if match == data:
  83. return await message.reply_text("**Usage:**\n__/save [NOTE_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/save [NOTE_NAME]")
  84. elif not message.reply_to_message and not message.text:
  85. if match == data:
  86. data = None
  87. if replied_message.text:
  88. _type = "text"
  89. file_id = None
  90. if replied_message.sticker:
  91. _type = "sticker"
  92. file_id = replied_message.sticker.file_id
  93. if replied_message.animation:
  94. _type = "animation"
  95. file_id = replied_message.animation.file_id
  96. if replied_message.photo:
  97. _type = "photo"
  98. file_id = replied_message.photo.file_id
  99. if replied_message.document:
  100. _type = "document"
  101. file_id = replied_message.document.file_id
  102. if replied_message.video:
  103. _type = "video"
  104. file_id = replied_message.video.file_id
  105. if replied_message.video_note:
  106. _type = "video_note"
  107. file_id = replied_message.video_note.file_id
  108. if replied_message.audio:
  109. _type = "audio"
  110. file_id = replied_message.audio.file_id
  111. if replied_message.voice:
  112. _type = "voice"
  113. file_id = replied_message.voice.file_id
  114. if replied_message.reply_markup and not "~" in data:
  115. urls = extract_urls(replied_message.reply_markup)
  116. if urls:
  117. response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
  118. data = data + response
  119. if data:
  120. data = await check_format(ikb, data)
  121. if not data:
  122. return await message.reply_text("**Wrong formatting, check the help section.**")
  123. note = {
  124. "type": _type,
  125. "data": data,
  126. "file_id": file_id,
  127. }
  128. chat_id = message.chat.id
  129. await save_note(chat_id, name, note)
  130. await eor(message, text=f"__**Saved note {name}.**__")
  131. except UnboundLocalError:
  132. return await message.reply_text("**Replied message is inaccessible.\n`Forward the message and try again`**")
  133. @app2.on_message(
  134. filters.command("notes", prefixes=USERBOT_PREFIX)
  135. & ~filters.forwarded
  136. & ~filters.via_bot
  137. & SUDOERS
  138. )
  139. @app.on_message(filters.command("notes") & ~filters.private)
  140. @capture_err
  141. async def get_notes(_, message):
  142. prefix = message.text.split()[0][0]
  143. is_ubot = bool(prefix == USERBOT_PREFIX)
  144. chat_id = USERBOT_ID if is_ubot else message.chat.id
  145. _notes = await get_note_names(chat_id)
  146. if not _notes:
  147. return await eor(message, text="**No notes in this chat.**")
  148. _notes.sort()
  149. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  150. for note in _notes:
  151. msg += f"**-** `{note}`\n"
  152. await eor(message, text=msg)
  153. @app2.on_message(
  154. filters.command("get", prefixes=USERBOT_PREFIX)
  155. & ~filters.forwarded
  156. & ~filters.via_bot
  157. & SUDOERS
  158. )
  159. async def get_one_note_userbot(_, message):
  160. if len(message.text.split()) < 2:
  161. return await eor(message, text="Invalid arguments")
  162. name = message.text.split(None, 1)[1]
  163. _note = await get_note(USERBOT_ID, name)
  164. if not _note:
  165. return await eor(message, text="No such note.")
  166. if _note["type"] == "text":
  167. data = _note["data"]
  168. await eor(
  169. message,
  170. text=data,
  171. disable_web_page_preview=True,
  172. )
  173. else:
  174. await message.reply_sticker(_note["data"])
  175. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  176. @capture_err
  177. async def get_one_note(_, message):
  178. user_id = message.from_user.id
  179. chat_id = message.chat.id
  180. name = message.text.replace("#", "", 1)
  181. if not name:
  182. return
  183. _note = await get_note(message.chat.id, name)
  184. if not _note:
  185. return
  186. type = _note["type"]
  187. data = _note["data"]
  188. file_id = _note["file_id"]
  189. keyb = None
  190. if data:
  191. if "{chat}" in data:
  192. data = data.replace("{chat}", (await app.get_chat(chat_id)).title)
  193. if "{name}" in data:
  194. data = data.replace("{name}", (await app.get_users(user_id)).mention)
  195. if findall(r"\[.+\,.+\]", data):
  196. keyboard = extract_text_and_keyb(ikb, data)
  197. if keyboard:
  198. data, keyb = keyboard
  199. replied_message = message.reply_to_message
  200. if replied_message:
  201. if replied_message.from_user.id != message.from_user.id:
  202. message = replied_message
  203. if type == "text":
  204. await message.reply_text(
  205. text=data,
  206. reply_markup=keyb,
  207. disable_web_page_preview=True,
  208. )
  209. if type == "sticker":
  210. await message.reply_sticker(
  211. sticker=file_id,
  212. )
  213. if type == "animation":
  214. await message.reply_animation(
  215. animation=file_id,
  216. caption=data,
  217. reply_markup=keyb,
  218. )
  219. if type == "photo":
  220. await message.reply_photo(
  221. photo=file_id,
  222. caption=data,
  223. reply_markup=keyb,
  224. )
  225. if type == "document":
  226. await message.reply_document(
  227. document=file_id,
  228. caption=data,
  229. reply_markup=keyb,
  230. )
  231. if type == "video":
  232. await message.reply_video(
  233. video=file_id,
  234. caption=data,
  235. reply_markup=keyb,
  236. )
  237. if type == "video_note":
  238. await message.reply_video_note(
  239. video_note=file_id,
  240. )
  241. if type == "audio":
  242. await message.reply_audio(
  243. audio=file_id,
  244. caption=data,
  245. reply_markup=keyb,
  246. )
  247. if type == "voice":
  248. await message.reply_voice(
  249. voice=file_id,
  250. caption=data,
  251. reply_markup=keyb,
  252. )
  253. @app2.on_message(
  254. filters.command("delete", prefixes=USERBOT_PREFIX)
  255. & ~filters.forwarded
  256. & ~filters.via_bot
  257. & SUDOERS
  258. )
  259. @app.on_message(filters.command("delete") & ~filters.private)
  260. @adminsOnly("can_change_info")
  261. async def del_note(_, message):
  262. if len(message.command) < 2:
  263. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  264. name = message.text.split(None, 1)[1].strip()
  265. if not name:
  266. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  267. prefix = message.text.split()[0][0]
  268. is_ubot = bool(prefix == USERBOT_PREFIX)
  269. chat_id = USERBOT_ID if is_ubot else message.chat.id
  270. deleted = await delete_note(chat_id, name)
  271. if deleted:
  272. await eor(message, text=f"**Deleted note {name} successfully.**")
  273. else:
  274. await eor(message, text="**No such note.**")
  275. @app.on_message(filters.command("deleteall") & ~filters.private)
  276. @adminsOnly("can_change_info")
  277. async def delete_all(_, message):
  278. _notes = await get_note_names(message.chat.id)
  279. if not _notes:
  280. return await message.reply_text("**No notes in this chat.**")
  281. else:
  282. keyboard = InlineKeyboardMarkup(
  283. [
  284. [InlineKeyboardButton("YES, DO IT", callback_data="delete_yes"),
  285. InlineKeyboardButton("Cancel", callback_data="delete_no")
  286. ]
  287. ]
  288. )
  289. await message.reply_text("**Are you sure you want to delete all the notes in this chat forever ?.**", reply_markup=keyboard)
  290. @app.on_callback_query(filters.regex("delete_(.*)"))
  291. async def delete_all_cb(_, cb):
  292. chat_id = cb.message.chat.id
  293. from_user = cb.from_user
  294. permissions = await member_permissions(chat_id, from_user.id)
  295. permission = "can_change_info"
  296. if permission not in permissions:
  297. return await cb.answer(f"You don't have the required permission.\n Permission: {permission}", show_alert=True)
  298. input = cb.data.split("_", 1)[1]
  299. if input == "yes":
  300. stoped_all = await deleteall_notes(chat_id)
  301. if stoped_all:
  302. return await cb.message.edit("**Successfully deleted all notes on this chat.**")
  303. if input == "no":
  304. await cb.message.reply_to_message.delete()
  305. await cb.message.delete()