-
Notifications
You must be signed in to change notification settings - Fork 33
/
shift.py
167 lines (161 loc) · 7.1 KB
/
shift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
""" PagerMaid module for channel help. """
from asyncio import sleep
from random import uniform
from telethon.errors.rpcerrorlist import FloodWaitError
from pagermaid import redis, log, redis_status, version
from pagermaid.utils import lang, alias_command
from pagermaid.listener import listener
@listener(is_plugin=False, outgoing=True, command=alias_command('shift'),
description='开启转发频道新消息功能,需要 Redis',
parameters="set <from channel> <to channel> 自动转发频道新消息(可以使用频道用户名或者 id)\n"
"del <from channel> 删除转发\n"
"backup <from channel> <to channel> 备份频道(可以使用频道用户名或者 id)")
async def shift_set(context):
if not redis_status():
await context.edit(f"{lang('error_prefix')}{lang('redis_dis')}")
return
if not 1 < len(context.parameter) < 4:
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
if context.parameter[0] == "set":
if len(context.parameter) != 3:
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
# 检查来源频道
try:
channel = await context.client.get_entity(int(context.parameter[1]))
if not channel.broadcast:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
channel = int(f'-100{channel.id}')
except Exception:
try:
channel = await context.client.get_entity(context.parameter[1])
if not channel.broadcast:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
channel = int(f'-100{channel.id}')
except Exception:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
if channel in [-1001441461877]:
await context.edit('出错了呜呜呜 ~ 此对话位于白名单中。')
return
# 检查目标频道
try:
to = int(context.parameter[2])
except Exception:
try:
to = await context.client.get_entity(context.parameter[2])
if to.broadcast or to.gigagroup or to.megagroup:
to = int(f'-100{to.id}')
else:
to = to.id
except Exception:
await context.edit("出错了呜呜呜 ~ 无法识别的目标对话。")
return
if to in [-1001441461877]:
await context.edit('出错了呜呜呜 ~ 此对话位于白名单中。')
return
redis.set("shift." + str(channel), f"{to}")
await context.edit(f"已成功配置将对话 {channel} 的新消息转发到 {to} 。")
await log(f"已成功配置将对话 {channel} 的新消息转发到 {to} 。")
elif context.parameter[0] == "del":
if len(context.parameter) != 2:
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
# 检查来源频道
try:
channel = int(context.parameter[1])
except Exception:
try:
channel = (await context.client.get_entity(context.parameter[1])).id
if channel.broadcast or channel.gigagroup or channel.megagroup:
channel = int(f'-100{channel.id}')
except Exception:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
try:
redis.delete("shift." + str(channel))
except:
await context.edit('emm...当前对话不存在于自动转发列表中。')
return
await context.edit(f"已成功关闭对话 {str(channel)} 的自动转发功能。")
await log(f"已成功关闭对话 {str(channel)} 的自动转发功能。")
elif context.parameter[0] == "backup":
if len(context.parameter) != 3:
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
# 检查来源频道
try:
channel = await context.client.get_entity(int(context.parameter[1]))
if not channel.broadcast:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
channel = int(f'-100{channel.id}')
except Exception:
try:
channel = await context.client.get_entity(context.parameter[1])
if not channel.broadcast:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
channel = int(f'-100{channel.id}')
except Exception:
await context.edit("出错了呜呜呜 ~ 无法识别的来源对话。")
return
if channel in [-1001441461877]:
await context.edit('出错了呜呜呜 ~ 此对话位于白名单中。')
return
# 检查目标频道
try:
to = int(context.parameter[2])
except Exception:
try:
to = await context.client.get_entity(context.parameter[2])
if to.broadcast or to.gigagroup or to.megagroup:
to = int(f'-100{to.id}')
else:
to = to.id
except Exception:
await context.edit("出错了呜呜呜 ~ 无法识别的目标对话。")
return
if to in [-1001441461877]:
await context.edit('出错了呜呜呜 ~ 此对话位于白名单中。')
return
# 开始遍历消息
await context.edit(f'开始备份频道 {channel} 到 {to} 。')
async for msg in context.client.iter_messages(int(channel), reverse=True):
await sleep(uniform(0.5, 1.0))
try:
await forward_msg(context, msg, to)
except BaseException:
pass
await context.edit(f'备份频道 {channel} 到 {to} 已完成。')
else:
await context.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
@listener(is_plugin=False, incoming=True, ignore_edited=True)
async def shift_channel_message(context):
""" Event handler to auto forward channel messages. """
if not redis_status():
return
if not redis.get("shift." + str(context.chat_id)):
return
if context.chat_id in [-1001441461877]:
return
cid = int(redis.get("shift." + str(context.chat_id)).decode())
try:
await context.forward_to(cid)
except Exception as e:
pass
async def forward_msg(context, msg, cid):
try:
await msg.forward_to(cid)
except FloodWaitError as e:
await context.edit(f'触发 Flood ,暂停 {e.seconds + uniform(0.5, 1.0)} 秒。')
try:
await sleep(e.seconds + uniform(0.5, 1.0))
except Exception as e:
print(f"Wait flood error: {e}")
return
await forward_msg(context, msg, cid)