shift 支持所有对话类型并且支持设置备份选项 (#177)

This commit is contained in:
canvex 2024-02-24 20:38:51 +08:00 committed by GitHub
parent 312aa42ded
commit 639d795e59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,7 @@
import contextlib
from asyncio import sleep
from random import uniform
from typing import Any
from typing import Any, List, Union, Set
from pyrogram.enums.chat_type import ChatType
from pyrogram.errors.exceptions.flood_420 import FloodWait
@ -28,7 +28,7 @@ def try_cast_or_fallback(val: Any, t: type) -> Any:
def check_chat_available(chat: Chat):
assert (
chat.type in [ChatType.CHANNEL, ChatType.GROUP]
chat.type in [ChatType.CHANNEL, ChatType.GROUP, ChatType.SUPERGROUP, ChatType.BOT, ChatType.PRIVATE]
and not chat.has_protected_content
)
@ -129,12 +129,17 @@ async def shift_set(client: Client, message: Message):
return await message.edit("出错了呜呜呜 ~ 此对话位于白名单中。")
# 开始遍历消息
await message.edit(f"开始备份频道 {source.id}{target.id}")
# 如果有把get_chat_history方法merge進去就可以實現從舊訊息到新訊息,https://github.com/pyrogram/pyrogram/pull/1046
# async for msg in client.get_chat_history(source.id,reverse=True):
async for msg in client.search_messages(source.id): # type: ignore
await sleep(uniform(0.5, 1.0))
await loosely_forward(
message,
msg,
target.id,
options,
disable_notification="silent" in options,
)
await message.edit(f"备份频道 {source.id}{target.id} 已完成。")
@ -173,8 +178,7 @@ async def shift_channel_message(message: Message):
source = message.chat.id
# 找訊息類型video、document...
media_type = message.media.name.lower() if message.media else "text"
media_type = message.media.value if message.media else "text"
target = d.get(f"shift.{source}")
if not target:
return
@ -202,15 +206,30 @@ async def loosely_forward(
notifier: Message,
message: Message,
chat_id: int,
options: Union[List[str], Set[str]],
disable_notification: bool = False,
):
# 找訊息類型video、document...
media_type = message.media.value if message.media else "text"
try:
await message.forward(chat_id, disable_notification=disable_notification)
if (not options) or "all" in options:
await message.forward(
chat_id,
disable_notification=disable_notification,
)
elif media_type in options:
await message.forward(
chat_id,
disable_notification=disable_notification,
)
else:
logs.debug("skip message type: %s", media_type)
# await message.forward(chat_id, disable_notification=disable_notification)
except FloodWait as ex:
min: int = ex.value # type: ignore
delay = min + uniform(0.5, 1.0)
await notifier.edit(f"触发 Flood ,暂停 {delay} 秒。")
await sleep(delay)
await loosely_forward(notifier, message, chat_id, disable_notification)
await loosely_forward(notifier, message, chat_id, options, disable_notification)
except Exception:
pass # drop other errors