notes.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 (
  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. @app.on_message(filters.command("save") & ~filters.private)
  69. @adminsOnly("can_change_info")
  70. async def save_notee(_, message):
  71. try:
  72. if len(message.command) < 2:
  73. await eor(
  74. message,
  75. text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
  76. )
  77. else:
  78. replied_message = message.reply_to_message
  79. if not replied_message:
  80. replied_message = message
  81. data, name = await get_data_and_name(replied_message, message)
  82. if data == "error":
  83. return await message.reply_text(
  84. "**Usage:**\n__/save [NOTE_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/save [NOTE_NAME]"
  85. )
  86. if replied_message.text:
  87. _type = "text"
  88. file_id = None
  89. if replied_message.sticker:
  90. _type = "sticker"
  91. file_id = replied_message.sticker.file_id
  92. if replied_message.animation:
  93. _type = "animation"
  94. file_id = replied_message.animation.file_id
  95. if replied_message.photo:
  96. _type = "photo"
  97. file_id = replied_message.photo.file_id
  98. if replied_message.document:
  99. _type = "document"
  100. file_id = replied_message.document.file_id
  101. if replied_message.video:
  102. _type = "video"
  103. file_id = replied_message.video.file_id
  104. if replied_message.video_note:
  105. _type = "video_note"
  106. file_id = replied_message.video_note.file_id
  107. if replied_message.audio:
  108. _type = "audio"
  109. file_id = replied_message.audio.file_id
  110. if replied_message.voice:
  111. _type = "voice"
  112. file_id = replied_message.voice.file_id
  113. if replied_message.reply_markup and not "~" in data:
  114. urls = extract_urls(replied_message.reply_markup)
  115. if urls:
  116. response = "\n".join(
  117. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  118. )
  119. data = data + response
  120. if data:
  121. data = await check_format(ikb, data)
  122. if not data:
  123. return await message.reply_text(
  124. "**Wrong formatting, check the help section.**"
  125. )
  126. note = {
  127. "type": _type,
  128. "data": data,
  129. "file_id": file_id,
  130. }
  131. chat_id = message.chat.id
  132. await save_note(chat_id, name, note)
  133. await eor(message, text=f"__**Saved note {name}.**__")
  134. except UnboundLocalError:
  135. return await message.reply_text(
  136. "**Replied message is inaccessible.\n`Forward the message and try again`**"
  137. )
  138. @app2.on_message(
  139. filters.command("notes", prefixes=USERBOT_PREFIX)
  140. & ~filters.forwarded
  141. & ~filters.via_bot
  142. & SUDOERS
  143. )
  144. @app.on_message(filters.command("notes") & ~filters.private)
  145. @capture_err
  146. async def get_notes(_, message):
  147. prefix = message.text.split()[0][0]
  148. is_ubot = bool(prefix == USERBOT_PREFIX)
  149. chat_id = USERBOT_ID if is_ubot else message.chat.id
  150. _notes = await get_note_names(chat_id)
  151. if not _notes:
  152. return await eor(message, text="**No notes in this chat.**")
  153. _notes.sort()
  154. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  155. for note in _notes:
  156. msg += f"**-** `{note}`\n"
  157. await eor(message, text=msg)
  158. @app2.on_message(
  159. filters.command("get", prefixes=USERBOT_PREFIX)
  160. & ~filters.forwarded
  161. & ~filters.via_bot
  162. & SUDOERS
  163. )
  164. async def get_one_note_userbot(_, message):
  165. if len(message.text.split()) < 2:
  166. return await eor(message, text="Invalid arguments")
  167. name = message.text.split(None, 1)[1]
  168. _note = await get_note(USERBOT_ID, name)
  169. if not _note:
  170. return await eor(message, text="No such note.")
  171. if _note["type"] == "text":
  172. data = _note["data"]
  173. await eor(
  174. message,
  175. text=data,
  176. disable_web_page_preview=True,
  177. )
  178. else:
  179. await message.reply_sticker(_note["data"])
  180. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  181. @capture_err
  182. async def get_one_note(_, message):
  183. user_id = message.from_user.id
  184. chat_id = message.chat.id
  185. name = message.text.replace("#", "", 1)
  186. if not name:
  187. return
  188. _note = await get_note(message.chat.id, name)
  189. if not _note:
  190. return
  191. type = _note["type"]
  192. data = _note["data"]
  193. file_id = _note.get("file_id")
  194. keyb = None
  195. if data:
  196. if "{chat}" in data:
  197. data = data.replace("{chat}", (await app.get_chat(chat_id)).title)
  198. if "{name}" in data:
  199. data = data.replace(
  200. "{name}", (await app.get_users(user_id)).mention
  201. )
  202. if findall(r"\[.+\,.+\]", data):
  203. keyboard = extract_text_and_keyb(ikb, data)
  204. if keyboard:
  205. data, keyb = keyboard
  206. replied_message = message.reply_to_message
  207. if replied_message:
  208. if replied_message.from_user.id != message.from_user.id:
  209. message = replied_message
  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()