notes.py 13 KB

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