From bdcef64b7d5b8063726c216b4c3af1d4e1acae0e Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 20 Jun 2022 22:40:15 +0800 Subject: [PATCH] =?UTF-8?q?shift=20=E8=87=AA=E5=8A=A8=E8=BD=AC=E5=8F=91/?= =?UTF-8?q?=E5=A4=87=E4=BB=BD=E9=A2=91=E9=81=93=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 10 ++++ shift/main.py | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 shift/main.py diff --git a/list.json b/list.json index 3b8ff0c..73bf0fd 100644 --- a/list.json +++ b/list.json @@ -169,6 +169,16 @@ "supported": true, "des-short": "vip 捐赠用户功能", "des": "命令:天气:weather weather_pic weather_he\nDuckduckgo:duckduckgo\n彩云翻译:caiyun\n文本转语音:tts_nan tts_nv tts_tw tts_ne tts_en。与 weather 插件冲突" + }, + { + "name": "shift", + "version": "1.031", + "section": "daily", + "maintainer": "xtaodada", + "size": "5.3 kb", + "supported": true, + "des-short": "自动转发/备份频道消息", + "des": "自动转发频道新消息或者从零开始备份频道。\n指令:-shift\n查看帮助:-help shift" } ] } diff --git a/shift/main.py b/shift/main.py new file mode 100644 index 0000000..c5ee3d7 --- /dev/null +++ b/shift/main.py @@ -0,0 +1,143 @@ +""" PagerMaid module for channel help. """ +from asyncio import sleep +from random import uniform +from pyrogram.enums.chat_type import ChatType +from pyrogram.errors.exceptions.flood_420 import FloodWait + +from pagermaid import log +from pagermaid.single_utils import sqlite, Message +from pagermaid.utils import lang +from pagermaid.listener import listener + +import contextlib + + +@listener(is_plugin=False, outgoing=True, command="shift", + description='开启转发频道新消息功能', + parameters="set 自动转发频道新消息(可以使用频道用户名或者 id)\n" + "del 删除转发\n" + "backup 备份频道(可以使用频道用户名或者 id)") +async def shift_set(message: Message): + if not 1 < len(message.parameter) < 4: + await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") + return + if message.parameter[0] == "set": + if len(message.parameter) != 3: + return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") + # 检查来源频道 + try: + channel = await message.bot.get_chat(int(message.parameter[1])) + if channel.type != ChatType.CHANNEL: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.has_protected_content: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + except Exception: + try: + channel = await message.bot.get_chat(message.parameter[1]) + if channel.type != ChatType.CHANNEL: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.has_protected_content: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + except Exception: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.id in [-1001441461877]: + return await message.edit('出错了呜呜呜 ~ 此对话位于白名单中。') + # 检查目标频道 + try: + to = await message.bot.get_chat(int(message.parameter[2])) + except Exception: + try: + to = await message.bot.get_chat(message.parameter[2]) + except Exception: + return await message.edit("出错了呜呜呜 ~ 无法识别的目标对话。") + if to.id in [-1001441461877]: + await message.edit('出错了呜呜呜 ~ 此对话位于白名单中。') + return + sqlite[f"shift.{channel.id}"] = to.id + await message.edit(f"已成功配置将对话 {channel.id} 的新消息转发到 {to.id} 。") + await log(f"已成功配置将对话 {channel.id} 的新消息转发到 {to.id} 。") + elif message.parameter[0] == "del": + if len(message.parameter) != 2: + return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") + # 检查来源频道 + try: + channel = await message.bot.get_chat(int(message.parameter[1])) + except Exception: + try: + channel = await message.bot.get_chat(message.parameter[1]) + except Exception: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + try: + del sqlite[f"shift.{channel.id}"] + except Exception as e: + return await message.edit('emm...当前对话不存在于自动转发列表中。') + await message.edit(f"已成功关闭对话 {str(channel.id)} 的自动转发功能。") + await log(f"已成功关闭对话 {str(channel.id)} 的自动转发功能。") + elif message.parameter[0] == "backup": + if len(message.parameter) != 3: + return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") + # 检查来源频道 + try: + channel = await message.bot.get_chat(int(message.parameter[1])) + if channel.type != ChatType.CHANNEL: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.has_protected_content: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + except Exception: + try: + channel = await message.bot.get_chat(message.parameter[1]) + if channel.type != ChatType.CHANNEL: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.has_protected_content: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + except Exception: + return await message.edit("出错了呜呜呜 ~ 无法识别的来源对话。") + if channel.id in [-1001441461877]: + return await message.edit('出错了呜呜呜 ~ 此对话位于白名单中。') + # 检查目标频道 + try: + to = await message.bot.get_chat(int(message.parameter[2])) + except Exception: + try: + to = await message.bot.get_chat(message.parameter[2]) + except Exception: + return await message.edit("出错了呜呜呜 ~ 无法识别的目标对话。") + if to.id in [-1001441461877]: + return await message.edit('出错了呜呜呜 ~ 此对话位于白名单中。') + # 开始遍历消息 + await message.edit(f'开始备份频道 {channel.id} 到 {to.id} 。') + async for msg in message.bot.search_messages(channel.id): + await sleep(uniform(0.5, 1.0)) + await forward_msg(message, msg, to.id) + await message.edit(f'备份频道 {channel.id} 到 {to.id} 已完成。') + else: + await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") + return + + +@listener(is_plugin=True, incoming=True, ignore_edited=True) +async def shift_channel_message(message): + """ Event handler to auto forward channel messages. """ + cid = sqlite.get(f"shift.{message.chat.id}", None) + if not cid: + return + if message.chat.id in [-1001441461877]: + return + if message.chat.has_protected_content: + del sqlite[f"shift.{message.chat.id}"] + return + with contextlib.suppress(Exception): + await message.forward(cid) + + +async def forward_msg(message, msg, cid): + try: + await msg.forward(msg, cid) + except FloodWait as e: + await message.edit(f'触发 Flood ,暂停 {e.value + uniform(0.5, 1.0)} 秒。') + try: + await sleep(e.value + uniform(0.5, 1.0)) + except Exception as e: + print(f"Wait flood error: {e}") + return + await forward_msg(message, msg, cid)