shift 支持过滤消息类型,展示所有转发任务

Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
This commit is contained in:
canvex 2024-01-27 23:31:10 +08:00 committed by GitHub
parent 89fd34ba01
commit 7b4b3fb4c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 21 deletions

View File

@ -10,7 +10,7 @@
需要加入 from_channel_id 和 to_channel_id。
`, shift set [from channel_id] [to channel_id] (silent)`
`, shift set [from channel_id] [to channel_id] (silent, all, text, photo, document, video)`
### 取消转发
@ -20,6 +20,15 @@
`, shift backup [from channel_id] [to channel_id] (silent)`
### 顯示要轉發的頻道
`, shift list`
## 选项解释
- `silent`: 禁用通知
- `all`: 全部都轉發(預設會有all)
- `text`: 轉發文字
- `photo`: 轉發圖片
- `document`: 轉發檔案
- `video`: 轉發影片

View File

@ -5,17 +5,18 @@ from asyncio import sleep
from random import uniform
from typing import Any
from pagermaid import log
from pagermaid.enums import Client, Message
from pagermaid.listener import listener
from pagermaid.single_utils import sqlite
from pagermaid.utils import lang
from pyrogram.enums.chat_type import ChatType
from pyrogram.errors.exceptions.flood_420 import FloodWait
from pyrogram.types import Chat
from pagermaid import log, logs
from pagermaid.enums import Client, Message
from pagermaid.listener import listener
from pagermaid.single_utils import sqlite
from pagermaid.utils import lang
WHITELIST = [-1001441461877]
AVAILABLE_OPTIONS = {"silent"}
AVAILABLE_OPTIONS = {"silent", "text", "all", "photo", "document", "video"}
def try_cast_or_fallback(val: Any, t: type) -> Any:
@ -26,7 +27,7 @@ def try_cast_or_fallback(val: Any, t: type) -> Any:
def check_chat_available(chat: Chat):
assert chat.type == ChatType.CHANNEL and not chat.has_protected_content
assert (chat.type in [ChatType.CHANNEL, ChatType.GROUP] and not chat.has_protected_content)
@listener(
@ -34,9 +35,10 @@ def check_chat_available(chat: Chat):
description="开启转发频道新消息功能",
parameters="set [from channel] [to channel] (silent) 自动转发频道新消息(可以使用频道用户名或者 id\n"
"del [from channel] 删除转发\n"
"backup [from channel] [to channel] (silent) 备份频道(可以使用频道用户名或者 id\n\n"
"backup [from channel] [to channel] (silent) 备份频道(可以使用频道用户名或者 id\n"
"list 顯示目前轉發的頻道\n\n"
"选项说明:\n"
"silent: 禁用通知",
"silent: 禁用通知, none: 文字, all: 全部訊息都傳, photo: 圖片, document: 檔案, video: 影片",
)
async def shift_set(client: Client, message: Message):
if not message.parameter:
@ -72,7 +74,7 @@ async def shift_set(client: Client, message: Message):
return
sqlite[f"shift.{source.id}"] = target.id
sqlite[f"shift.{source.id}.options"] = (
message.parameter[3:] if len(message.parameter) > 3 else []
message.parameter[3:] if len(message.parameter) > 3 else ["all"]
)
await message.edit(f"已成功配置将对话 {source.id} 的新消息转发到 {target.id}")
await log(f"已成功配置将对话 {source.id} 的新消息转发到 {target.id}")
@ -133,16 +135,35 @@ async def shift_set(client: Client, message: Message):
disable_notification="silent" in options,
)
await message.edit(f"备份频道 {source.id}{target.id} 已完成。")
# 列出要轉存的頻道
elif message.parameter[0] == "list":
from_ids = list(filter(lambda x: (x.startswith("shift.") and (not x.endswith("options"))), list(sqlite.keys())))
if not from_ids:
return await message.edit("沒有要轉存的頻道")
output = "總共有 %d 個頻道要轉存\n\n" % len(from_ids)
for from_id in from_ids:
to_id = sqlite[from_id]
output += "%s -> %s\n" % (format_channel_id(from_id[6:]), format_channel_id(to_id))
await message.edit(output)
else:
await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
def format_channel_id(channel_id: str):
short_channel_id = str(channel_id)[4:]
return f"[{channel_id}](https://t.me/c/{short_channel_id})"
@listener(is_plugin=True, incoming=True, ignore_edited=True)
async def shift_channel_message(message: Message):
"""Event handler to auto forward channel messages."""
d = dict(sqlite)
source = message.chat.id
# 找訊息類型video、document...
media_type = message.media.name.lower() if message.media else "text"
target = d.get(f"shift.{source}")
if not target:
return
@ -152,10 +173,18 @@ async def shift_channel_message(message: Message):
options = d.get(f"shift.{source}.options") or []
with contextlib.suppress(Exception):
if (not options) or "all" in options:
await message.forward(
target,
disable_notification="silent" in options,
)
elif media_type in options:
await message.forward(
target,
disable_notification="silent" in options,
)
else:
logs.debug("skip message type: %s", media_type)
async def loosely_forward(