notes.py 12 KB

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