| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- """
- MIT License
- Copyright (c) 2024 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- from re import findall
- from pyrogram import filters
- from pyrogram.types import (
- CallbackQuery,
- InlineKeyboardButton,
- InlineKeyboardMarkup,
- )
- from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor
- from wbb.core.decorators.errors import capture_err
- from wbb.core.decorators.permissions import adminsOnly
- from wbb.core.keyboard import ikb
- from wbb.modules.admin import member_permissions
- from wbb.utils.dbfunctions import (
- delete_note,
- deleteall_notes,
- get_note,
- get_note_names,
- save_note,
- )
- from wbb.utils.functions import (
- check_format,
- extract_text_and_keyb,
- get_data_and_name,
- )
- __MODULE__ = "群笔记"
- __HELP__ = """/notes - 查看本群全部笔记。
- /save [笔记名称] - 回复一条消息并保存为笔记。
- 支持文字、动图、图片、文件、视频、圆形视频、音频和语音。
- 使用 `/save [笔记名称] [新说明]` 可替换媒体说明。
- 发送 `#笔记名称` 可读取笔记。
- /delete [笔记名称] - 删除指定笔记。
- /deleteall - 永久删除本群全部笔记。
- 发送 /markdownhelp 查看文字格式和按钮语法。
- """
- def extract_urls(reply_markup):
- urls = []
- if reply_markup.inline_keyboard:
- buttons = reply_markup.inline_keyboard
- for i, row in enumerate(buttons):
- for j, button in enumerate(row):
- if button.url:
- name = (
- "\n~\nbutton"
- if i * len(row) + j + 1 == 1
- else f"button{i * len(row) + j + 1}"
- )
- urls.append((f"{name}", button.text, button.url))
- return urls
- @app2.on_message(filters.command("save", prefixes=USERBOT_PREFIX) & SUDOERS & ~filters.via_bot)
- @app.on_message(filters.command("save") & ~filters.private)
- @adminsOnly("can_change_info")
- async def save_notee(_, message):
- try:
- if len(message.command) < 2:
- await eor(
- message,
- text="**用法:**\n回复一条消息并发送 "
- "/save [笔记名称],即可保存新笔记。",
- )
- else:
- replied_message = message.reply_to_message
- if not replied_message:
- replied_message = message
- data, name = await get_data_and_name(replied_message, message)
- if data == "error":
- return await message.reply_text(
- "**用法:**\n__/save [笔记名称] [内容]__\n"
- "`-----------或者-----------`\n"
- "回复一条消息并发送:\n/save [笔记名称]"
- )
- if replied_message.text:
- _type = "text"
- file_id = None
- if replied_message.sticker:
- _type = "sticker"
- file_id = replied_message.sticker.file_id
- if replied_message.animation:
- _type = "animation"
- file_id = replied_message.animation.file_id
- if replied_message.photo:
- _type = "photo"
- file_id = replied_message.photo.file_id
- if replied_message.document:
- _type = "document"
- file_id = replied_message.document.file_id
- if replied_message.video:
- _type = "video"
- file_id = replied_message.video.file_id
- if replied_message.video_note:
- _type = "video_note"
- file_id = replied_message.video_note.file_id
- if replied_message.audio:
- _type = "audio"
- file_id = replied_message.audio.file_id
- if replied_message.voice:
- _type = "voice"
- file_id = replied_message.voice.file_id
- if replied_message.reply_markup and not findall(r"\[.+\,.+\]", data):
- urls = extract_urls(replied_message.reply_markup)
- if urls:
- response = "\n".join(
- [f"{name}=[{text}, {url}]" for name, text, url in urls]
- )
- data = data + response
- if data:
- data = await check_format(ikb, data)
- if not data:
- return await message.reply_text(
- "**消息格式不正确,请发送 /markdownhelp 查看说明。**"
- )
- note = {
- "type": _type,
- "data": data,
- "file_id": file_id,
- }
- chat_id = USERBOT_ID if message.text.startswith(USERBOT_PREFIX) else message.chat.id
- await save_note(chat_id, name, note)
- await eor(message, text=f"__**笔记“{name}”已保存。**__")
- except UnboundLocalError:
- return await message.reply_text(
- "**无法读取被回复消息,请先转发该消息再重试。**"
- )
- @app2.on_message(
- filters.command("notes", prefixes=USERBOT_PREFIX)
- & ~filters.forwarded
- & ~filters.via_bot
- & SUDOERS
- )
- @app.on_message(filters.command("notes") & ~filters.private)
- @capture_err
- async def get_notes(_, message):
- prefix = message.text.split()[0][0]
- is_ubot = bool(prefix == USERBOT_PREFIX)
- chat_id = USERBOT_ID if is_ubot else message.chat.id
- _notes = await get_note_names(chat_id)
- if not _notes:
- return await eor(message, text="**当前没有群笔记。**")
- _notes.sort()
- owner = "用户客户端" if is_ubot else message.chat.title
- msg = f"{owner} 的笔记列表:\n"
- for note in _notes:
- msg += f"**-** `{note}`\n"
- await eor(message, text=msg)
- @app2.on_message(
- filters.command("get", prefixes=USERBOT_PREFIX)
- & ~filters.forwarded
- & ~filters.via_bot
- & SUDOERS
- )
- async def get_one_note_userbot(_, message):
- if len(message.text.split()) < 2:
- return await eor(message, text="参数无效。")
- name = message.text.split(None, 1)[1]
- _note = await get_note(USERBOT_ID, name)
- if not _note:
- return await eor(message, text="未找到该笔记。")
- type = _note["type"]
- data = _note["data"]
- file_id = _note.get("file_id")
- keyb = None
- await get_reply(message, type, file_id, data, keyb, protect)
- @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
- @capture_err
- async def get_one_note(_, message):
- from_user = message.from_user if message.from_user else message.sender_chat
- chat_id = message.chat.id
- name = message.text.replace("#", "", 1)
- if not name:
- return
- _note = await get_note(chat_id, name)
- if not _note:
- return
- type = _note["type"]
- data = _note["data"]
- file_id = _note.get("file_id")
- keyb = None
- if data:
- if "{chat}" in data:
- data = data.replace("{chat}", message.chat.title)
- if "{name}" in data:
- data = data.replace(
- "{name}", (from_user.mention if message.from_user else from_user.title)
- )
- if findall(r"\[.+\,.+\]", data):
- keyboard = extract_text_and_keyb(ikb, data)
- if keyboard:
- data, keyb = keyboard
- replied_message = message.reply_to_message
- if replied_message:
- replied_user = replied_message.from_user if replied_message.from_user else replied_message.sender_chat
- if replied_user.id != from_user.id:
- message = replied_message
- await get_reply(message, type, file_id, data, keyb)
- async def get_reply(message, type, file_id, data, keyb):
- if type == "text":
- await message.reply_text(
- text=data,
- reply_markup=keyb,
- disable_web_page_preview=True,
- )
- if type == "sticker":
- await message.reply_sticker(
- sticker=file_id,
- )
- if type == "animation":
- await message.reply_animation(
- animation=file_id,
- caption=data,
- reply_markup=keyb,
- )
- if type == "photo":
- await message.reply_photo(
- photo=file_id,
- caption=data,
- reply_markup=keyb,
- )
- if type == "document":
- await message.reply_document(
- document=file_id,
- caption=data,
- reply_markup=keyb,
- )
- if type == "video":
- await message.reply_video(
- video=file_id,
- caption=data,
- reply_markup=keyb,
- )
- if type == "video_note":
- await message.reply_video_note(
- video_note=file_id,
- )
- if type == "audio":
- await message.reply_audio(
- audio=file_id,
- caption=data,
- reply_markup=keyb,
- )
- if type == "voice":
- await message.reply_voice(
- voice=file_id,
- caption=data,
- reply_markup=keyb,
- )
- @app2.on_message(
- filters.command("delete", prefixes=USERBOT_PREFIX)
- & ~filters.forwarded
- & ~filters.via_bot
- & SUDOERS
- )
- @app.on_message(filters.command("delete") & ~filters.private)
- @adminsOnly("can_change_info")
- async def del_note(_, message):
- if len(message.command) < 2:
- return await eor(message, text="**用法**\n__/delete [笔记名称]__")
- name = message.text.split(None, 1)[1].strip()
- if not name:
- return await eor(message, text="**用法**\n__/delete [笔记名称]__")
- prefix = message.text.split()[0][0]
- is_ubot = bool(prefix == USERBOT_PREFIX)
- chat_id = USERBOT_ID if is_ubot else message.chat.id
- deleted = await delete_note(chat_id, name)
- if deleted:
- await eor(message, text=f"**笔记“{name}”已删除。**")
- else:
- await eor(message, text="**未找到该笔记。**")
- @app.on_message(filters.command("deleteall") & ~filters.private)
- @adminsOnly("can_change_info")
- async def delete_all(_, message):
- _notes = await get_note_names(message.chat.id)
- if not _notes:
- return await message.reply_text("**本群没有笔记。**")
- else:
- keyboard = InlineKeyboardMarkup(
- [
- [
- InlineKeyboardButton(
- "确认删除", callback_data="delete_yes"
- ),
- InlineKeyboardButton("取消", callback_data="delete_no"),
- ]
- ]
- )
- await message.reply_text(
- "**确认永久删除本群全部笔记吗?**",
- reply_markup=keyboard,
- )
- @app.on_callback_query(filters.regex("delete_(.*)"))
- async def delete_all_cb(_, cb):
- chat_id = cb.message.chat.id
- from_user = cb.from_user
- permissions = await member_permissions(chat_id, from_user.id)
- permission = "can_change_info"
- if permission not in permissions:
- return await cb.answer(
- "你没有修改群信息的权限。",
- show_alert=True,
- )
- input = cb.data.split("_", 1)[1]
- if input == "yes":
- stoped_all = await deleteall_notes(chat_id)
- if stoped_all:
- return await cb.message.edit(
- "**本群全部笔记已删除。**"
- )
- if input == "no":
- await cb.message.reply_to_message.delete()
- await cb.message.delete()
|