forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpurge.py
95 lines (84 loc) · 3.76 KB
/
purge.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
# 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/>.
from .. import loader, utils
import logging
import telethon
logger = logging.getLogger(__name__)
@loader.tds
class PurgeMod(loader.Module):
"""Deletes your messages"""
strings = {"name": "Purge",
"from_where": "<b>Which messages should be purged?</b>",
"not_supergroup_bot": "<b>Purges can only take place in supergroups</b>",
"delete_what": "<b>What message should be deleted?</b>"}
@loader.group_admin_delete_messages
@loader.ratelimit
async def purgecmd(self, message):
"""Purge from the replied message"""
if not message.is_reply:
await utils.answer(message, self.strings("from_where", message))
return
from_users = set()
args = utils.get_args(message)
for arg in args:
try:
entity = await message.client.get_entity(arg)
if isinstance(entity, telethon.tl.types.User):
from_users.add(entity.id)
except ValueError:
pass
msgs = []
from_ids = set()
if await message.client.is_bot():
if not message.is_channel:
await utils.answer(message, self.strings("not_supergroup_bot", message))
return
for msg in range(message.reply_to_msg_id, message.id + 1):
msgs.append(msg)
if len(msgs) >= 99:
logger.debug(msgs)
await message.client.delete_messages(message.to_id, msgs)
msgs.clear()
else:
async for msg in message.client.iter_messages(
entity=message.to_id,
min_id=message.reply_to_msg_id - 1,
reverse=True):
if from_users and msg.from_id not in from_users:
continue
msgs.append(msg.id)
from_ids.add(msg.from_id)
if len(msgs) >= 99:
logger.debug(msgs)
await message.client.delete_messages(message.to_id, msgs)
msgs.clear()
if msgs:
logger.debug(msgs)
await message.client.delete_messages(message.to_id, msgs)
await self.allmodules.log("purge", group=message.to_id, affected_uids=from_ids)
@loader.group_admin_delete_messages
@loader.ratelimit
async def delcmd(self, message):
"""Delete the replied message"""
msgs = [message.id]
if not message.is_reply:
if await message.client.is_bot():
await utils.answer(message, self.strings("delete_what", message))
return
msg = await message.client.iter_messages(message.to_id, 1, max_id=message.id).__anext__()
else:
msg = await message.get_reply_message()
msgs.append(msg.id)
logger.debug(msgs)
await message.client.delete_messages(message.to_id, msgs)
await self.allmodules.log("delete", group=message.to_id, affected_uids=[msg.from_id])