This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
recentactions.py
69 lines (58 loc) · 2.84 KB
/
recentactions.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
# -*- coding: future_fstrings -*-
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import telethon
from .. import loader, utils
def register(cb):
cb(RecentActionsMod())
class RecentActionsMod(loader.Module):
"""Reads recent actions"""
def __init__(self):
self.name = _("Recent Actions")
async def recoverdeletedcmd(self, message):
"""Restores deleted messages sent after replied message (optionally specify how many to recover)"""
msgs = message.client.iter_admin_log(message.to_id, delete=True)
if not message.is_reply:
await utils.answer(message, _("<code>Reply to a message to specify where to start</code>"))
return
if not isinstance(message.to_id, telethon.tl.types.PeerChannel):
await utils.answer(message, _("<code>This isn't a supergroup or channel"))
return
target = (await message.get_reply_message()).date
ret = []
try:
async for msg in msgs:
if msg.original.date < target:
break
if msg.original.action.message.date < target:
continue
ret += [msg]
except telethon.errors.rpcerrorlist.ChatAdminRequiredError:
await utils.answer(message, _("<code>Admin is required to read deleted messages</code>"))
args = utils.get_args(message)
if len(args) > 0:
try:
count = int(args[0])
ret = ret[-count:]
except ValueError:
pass
for msg in reversed(ret):
orig = msg.original.action.message
deldate = msg.original.date.isoformat()
origdate = orig.date.isoformat()
await message.respond(_("Deleted message {} recovered. Originally sent at {} by {}, deleted at {} by {}")
.format(msg.id, origdate, orig.from_id, deldate, msg.user_id))
if isinstance(orig, telethon.tl.types.MessageService):
await message.respond("<code>" + utils.escape_html(orig.stringify()) + "</code>")
else:
await message.respond(orig)