Skip to content

Commit ce4ebfd

Browse files
committed
feat: 添加互通模式指令 /ruler 和 /sese,支持临时关闭转发
1 parent 689073d commit ce4ebfd

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@
120120
| `ONEBOT2TG_BRIDGE_GROUP_ID` | `str \| int` | `""` | 互通模式下,要互通的 QQ 群号 |
121121
| `ONEBOT2TG_BRIDGE_TG_CHAT_ID` | `str \| int` | `""` | 互通模式下,TG 对应的 chat_id(群或频道) |
122122

123+
124+
#### 互通模式指令表
125+
| 指令 | 触发端 | 作用 |
126+
| :---: | :---: | :---: |
127+
| /ruler | Telegram | 临时关闭 TG → QQ 的互通转发 |
128+
| /sese | QQ | 临时关闭 QQ → TG 的互通转发 |
129+
123130
### 转发模式配置
124131

125132
| 配置项 | 类型 | 默认值 | 说明 |

src/nonebot_plugin_onebot2tg/forwarder.py

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from nonebot import logger, get_adapter
44
from nonebot.rule import is_type
55
from nonebot.drivers import Request
6-
from nonebot.plugin.on import on_message
6+
from nonebot.plugin.on import on_command, on_message
77
from nonebot.adapters.telegram import Bot as TGBot
88
from nonebot.adapters.onebot.v11 import (
99
Bot as OB11Bot,
@@ -36,6 +36,10 @@
3636

3737
config: Config
3838

39+
# 运行时临时开关(重启后失效)
40+
_bridge_tg_to_qq_paused: bool = False
41+
_bridge_qq_to_tg_paused: bool = False
42+
3943

4044
async def get_tg_bot() -> TGBot | None:
4145
try:
@@ -224,6 +228,51 @@ async def _send_to_tg(
224228
logger.error(f"[{mode}] 发送文本到 TG 失败: {e}")
225229

226230

231+
# ============================================================
232+
# TG 命令:/ruler 临时关闭 TG → QQ
233+
# ============================================================
234+
tg_ruler = on_command("ruler", rule=is_type(TGMessageEvent), aliases={"Ruler"})
235+
236+
237+
@tg_ruler.handle()
238+
async def handle_tg_ruler(event: TGMessageEvent):
239+
global _bridge_tg_to_qq_paused
240+
if not config.onebot2tg_enable_bridge:
241+
return
242+
chat_id = str(event.chat.id) if hasattr(event, "chat") else ""
243+
if chat_id != str(config.onebot2tg_bridge_tg_chat_id):
244+
return
245+
_bridge_tg_to_qq_paused = True
246+
tg_bot = await get_tg_bot()
247+
if tg_bot is not None:
248+
await tg_bot.send_message(chat_id=chat_id, text="乳了!已临时关闭 TG→QQ 转发")
249+
250+
251+
# ============================================================
252+
# QQ 命令:/sese 临时关闭 QQ → TG
253+
# ============================================================
254+
ob_sese = on_command("sese", rule=is_type(OB11MessageEvent), aliases={"Sese"})
255+
256+
257+
@ob_sese.handle()
258+
async def handle_ob_sese(event: OB11MessageEvent):
259+
global _bridge_qq_to_tg_paused
260+
if not config.onebot2tg_enable_bridge:
261+
return
262+
is_group = isinstance(event, OB11GroupMessageEvent)
263+
group_id = str(event.group_id) if is_group else ""
264+
if is_group and group_id != str(config.onebot2tg_bridge_group_id):
265+
return
266+
_bridge_qq_to_tg_paused = True
267+
ob11_bot = await get_ob11_bot()
268+
if ob11_bot is not None:
269+
msg = OB11Message([OB11Segment.text("涩涩时间!已临时关闭 QQ→TG 转发")])
270+
if is_group:
271+
await ob11_bot.send_group_msg(group_id=event.group_id, message=msg)
272+
else:
273+
await ob11_bot.send_private_msg(user_id=event.user_id, message=msg)
274+
275+
227276
# ============================================================
228277
# TG 消息处理器(仅互通模式)
229278
# ============================================================
@@ -234,6 +283,8 @@ async def _send_to_tg(
234283
async def handle_tg_message(event: TGMessageEvent):
235284
if not config.onebot2tg_enable_bridge:
236285
return
286+
if _bridge_tg_to_qq_paused:
287+
return
237288
if not config.onebot2tg_bridge_group_id:
238289
return
239290

@@ -287,15 +338,16 @@ async def handle_ob11_message(event: OB11MessageEvent):
287338
# ---------- 互通模式 ----------
288339
if config.onebot2tg_enable_bridge and config.onebot2tg_bridge_tg_chat_id:
289340
if is_group and group_id == str(config.onebot2tg_bridge_group_id):
290-
caption, file_segments = await _ob11_message_to_tg(ob11_bot, event)
291-
await _send_to_tg(
292-
tg_bot,
293-
config.onebot2tg_bridge_tg_chat_id,
294-
display_name,
295-
caption,
296-
file_segments,
297-
"互通",
298-
)
341+
if not _bridge_qq_to_tg_paused:
342+
caption, file_segments = await _ob11_message_to_tg(ob11_bot, event)
343+
await _send_to_tg(
344+
tg_bot,
345+
config.onebot2tg_bridge_tg_chat_id,
346+
display_name,
347+
caption,
348+
file_segments,
349+
"互通",
350+
)
299351
return
300352

301353
# ---------- 转发模式(QQ → TG 单向) ----------

0 commit comments

Comments
 (0)