notes.py 13 KB

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