notes.py 12 KB

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