notes.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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__ = "群笔记"
  45. __HELP__ = """/notes - 查看本群全部笔记。
  46. /save [笔记名称] - 回复一条消息并保存为笔记。
  47. 支持文字、动图、图片、文件、视频、圆形视频、音频和语音。
  48. 使用 `/save [笔记名称] [新说明]` 可替换媒体说明。
  49. 发送 `#笔记名称` 可读取笔记。
  50. /delete [笔记名称] - 删除指定笔记。
  51. /deleteall - 永久删除本群全部笔记。
  52. 发送 /markdownhelp 查看文字格式和按钮语法。
  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="**用法:**\n回复一条消息并发送 "
  77. "/save [笔记名称],即可保存新笔记。",
  78. )
  79. else:
  80. replied_message = message.reply_to_message
  81. if not replied_message:
  82. replied_message = message
  83. data, name = await get_data_and_name(replied_message, message)
  84. if data == "error":
  85. return await message.reply_text(
  86. "**用法:**\n__/save [笔记名称] [内容]__\n"
  87. "`-----------或者-----------`\n"
  88. "回复一条消息并发送:\n/save [笔记名称]"
  89. )
  90. if replied_message.text:
  91. _type = "text"
  92. file_id = None
  93. if replied_message.sticker:
  94. _type = "sticker"
  95. file_id = replied_message.sticker.file_id
  96. if replied_message.animation:
  97. _type = "animation"
  98. file_id = replied_message.animation.file_id
  99. if replied_message.photo:
  100. _type = "photo"
  101. file_id = replied_message.photo.file_id
  102. if replied_message.document:
  103. _type = "document"
  104. file_id = replied_message.document.file_id
  105. if replied_message.video:
  106. _type = "video"
  107. file_id = replied_message.video.file_id
  108. if replied_message.video_note:
  109. _type = "video_note"
  110. file_id = replied_message.video_note.file_id
  111. if replied_message.audio:
  112. _type = "audio"
  113. file_id = replied_message.audio.file_id
  114. if replied_message.voice:
  115. _type = "voice"
  116. file_id = replied_message.voice.file_id
  117. if replied_message.reply_markup and not findall(r"\[.+\,.+\]", data):
  118. urls = extract_urls(replied_message.reply_markup)
  119. if urls:
  120. response = "\n".join(
  121. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  122. )
  123. data = data + response
  124. if data:
  125. data = await check_format(ikb, data)
  126. if not data:
  127. return await message.reply_text(
  128. "**消息格式不正确,请发送 /markdownhelp 查看说明。**"
  129. )
  130. note = {
  131. "type": _type,
  132. "data": data,
  133. "file_id": file_id,
  134. }
  135. chat_id = USERBOT_ID if message.text.startswith(USERBOT_PREFIX) else message.chat.id
  136. await save_note(chat_id, name, note)
  137. await eor(message, text=f"__**笔记“{name}”已保存。**__")
  138. except UnboundLocalError:
  139. return await message.reply_text(
  140. "**无法读取被回复消息,请先转发该消息再重试。**"
  141. )
  142. @app2.on_message(
  143. filters.command("notes", prefixes=USERBOT_PREFIX)
  144. & ~filters.forwarded
  145. & ~filters.via_bot
  146. & SUDOERS
  147. )
  148. @app.on_message(filters.command("notes") & ~filters.private)
  149. @capture_err
  150. async def get_notes(_, message):
  151. prefix = message.text.split()[0][0]
  152. is_ubot = bool(prefix == USERBOT_PREFIX)
  153. chat_id = USERBOT_ID if is_ubot else message.chat.id
  154. _notes = await get_note_names(chat_id)
  155. if not _notes:
  156. return await eor(message, text="**当前没有群笔记。**")
  157. _notes.sort()
  158. owner = "用户客户端" if is_ubot else message.chat.title
  159. msg = f"{owner} 的笔记列表:\n"
  160. for note in _notes:
  161. msg += f"**-** `{note}`\n"
  162. await eor(message, text=msg)
  163. @app2.on_message(
  164. filters.command("get", prefixes=USERBOT_PREFIX)
  165. & ~filters.forwarded
  166. & ~filters.via_bot
  167. & SUDOERS
  168. )
  169. async def get_one_note_userbot(_, message):
  170. if len(message.text.split()) < 2:
  171. return await eor(message, text="参数无效。")
  172. name = message.text.split(None, 1)[1]
  173. _note = await get_note(USERBOT_ID, name)
  174. if not _note:
  175. return await eor(message, text="未找到该笔记。")
  176. type = _note["type"]
  177. data = _note["data"]
  178. file_id = _note.get("file_id")
  179. keyb = None
  180. await get_reply(message, type, file_id, data, keyb, protect)
  181. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  182. @capture_err
  183. async def get_one_note(_, message):
  184. from_user = message.from_user if message.from_user else message.sender_chat
  185. chat_id = message.chat.id
  186. name = message.text.replace("#", "", 1)
  187. if not name:
  188. return
  189. _note = await get_note(chat_id, name)
  190. if not _note:
  191. return
  192. type = _note["type"]
  193. data = _note["data"]
  194. file_id = _note.get("file_id")
  195. keyb = None
  196. if data:
  197. if "{chat}" in data:
  198. data = data.replace("{chat}", message.chat.title)
  199. if "{name}" in data:
  200. data = data.replace(
  201. "{name}", (from_user.mention if message.from_user else from_user.title)
  202. )
  203. if findall(r"\[.+\,.+\]", data):
  204. keyboard = extract_text_and_keyb(ikb, data)
  205. if keyboard:
  206. data, keyb = keyboard
  207. replied_message = message.reply_to_message
  208. if replied_message:
  209. replied_user = replied_message.from_user if replied_message.from_user else replied_message.sender_chat
  210. if replied_user.id != from_user.id:
  211. message = replied_message
  212. await get_reply(message, type, file_id, data, keyb)
  213. async def get_reply(message, type, file_id, data, keyb):
  214. if type == "text":
  215. await message.reply_text(
  216. text=data,
  217. reply_markup=keyb,
  218. disable_web_page_preview=True,
  219. )
  220. if type == "sticker":
  221. await message.reply_sticker(
  222. sticker=file_id,
  223. )
  224. if type == "animation":
  225. await message.reply_animation(
  226. animation=file_id,
  227. caption=data,
  228. reply_markup=keyb,
  229. )
  230. if type == "photo":
  231. await message.reply_photo(
  232. photo=file_id,
  233. caption=data,
  234. reply_markup=keyb,
  235. )
  236. if type == "document":
  237. await message.reply_document(
  238. document=file_id,
  239. caption=data,
  240. reply_markup=keyb,
  241. )
  242. if type == "video":
  243. await message.reply_video(
  244. video=file_id,
  245. caption=data,
  246. reply_markup=keyb,
  247. )
  248. if type == "video_note":
  249. await message.reply_video_note(
  250. video_note=file_id,
  251. )
  252. if type == "audio":
  253. await message.reply_audio(
  254. audio=file_id,
  255. caption=data,
  256. reply_markup=keyb,
  257. )
  258. if type == "voice":
  259. await message.reply_voice(
  260. voice=file_id,
  261. caption=data,
  262. reply_markup=keyb,
  263. )
  264. @app2.on_message(
  265. filters.command("delete", prefixes=USERBOT_PREFIX)
  266. & ~filters.forwarded
  267. & ~filters.via_bot
  268. & SUDOERS
  269. )
  270. @app.on_message(filters.command("delete") & ~filters.private)
  271. @adminsOnly("can_change_info")
  272. async def del_note(_, message):
  273. if len(message.command) < 2:
  274. return await eor(message, text="**用法**\n__/delete [笔记名称]__")
  275. name = message.text.split(None, 1)[1].strip()
  276. if not name:
  277. return await eor(message, text="**用法**\n__/delete [笔记名称]__")
  278. prefix = message.text.split()[0][0]
  279. is_ubot = bool(prefix == USERBOT_PREFIX)
  280. chat_id = USERBOT_ID if is_ubot else message.chat.id
  281. deleted = await delete_note(chat_id, name)
  282. if deleted:
  283. await eor(message, text=f"**笔记“{name}”已删除。**")
  284. else:
  285. await eor(message, text="**未找到该笔记。**")
  286. @app.on_message(filters.command("deleteall") & ~filters.private)
  287. @adminsOnly("can_change_info")
  288. async def delete_all(_, message):
  289. _notes = await get_note_names(message.chat.id)
  290. if not _notes:
  291. return await message.reply_text("**本群没有笔记。**")
  292. else:
  293. keyboard = InlineKeyboardMarkup(
  294. [
  295. [
  296. InlineKeyboardButton(
  297. "确认删除", callback_data="delete_yes"
  298. ),
  299. InlineKeyboardButton("取消", callback_data="delete_no"),
  300. ]
  301. ]
  302. )
  303. await message.reply_text(
  304. "**确认永久删除本群全部笔记吗?**",
  305. reply_markup=keyboard,
  306. )
  307. @app.on_callback_query(filters.regex("delete_(.*)"))
  308. async def delete_all_cb(_, cb):
  309. chat_id = cb.message.chat.id
  310. from_user = cb.from_user
  311. permissions = await member_permissions(chat_id, from_user.id)
  312. permission = "can_change_info"
  313. if permission not in permissions:
  314. return await cb.answer(
  315. "你没有修改群信息的权限。",
  316. show_alert=True,
  317. )
  318. input = cb.data.split("_", 1)[1]
  319. if input == "yes":
  320. stoped_all = await deleteall_notes(chat_id)
  321. if stoped_all:
  322. return await cb.message.edit(
  323. "**本群全部笔记已删除。**"
  324. )
  325. if input == "no":
  326. await cb.message.reply_to_message.delete()
  327. await cb.message.delete()