-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDelMessTools.py
More file actions
254 lines (203 loc) · 11.9 KB
/
DelMessTools.py
File metadata and controls
254 lines (203 loc) · 11.9 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# ---------------------------------------------------------------------------------
#░█▀▄░▄▀▀▄░█▀▄░█▀▀▄░█▀▀▄░█▀▀▀░▄▀▀▄░░░█▀▄▀█
#░█░░░█░░█░█░█░█▄▄▀░█▄▄█░█░▀▄░█░░█░░░█░▀░█
#░▀▀▀░░▀▀░░▀▀░░▀░▀▀░▀░░▀░▀▀▀▀░░▀▀░░░░▀░░▒▀
# Name: DelMessTools
# Description: Module to manage and delete your messages in the current chat
# Author: @codrago_m
# ---------------------------------------------------------------------------------
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# ---------------------------------------------------------------------------------
# Author: @codrago
# Commands: nopurge, purgetime, purgelength, purgekeyword, purge
# scope: hikka_only
# meta developer: @codrago_m
# meta banner: https://raw.githubusercontent.com/coddrago/modules/refs/heads/main/banner.png
# meta pic: https://envs.sh/HJx.webp
# ---------------------------------------------------------------------------------
__version__ = (1, 1, 0)
from hikkatl.tl.types import Message, DocumentAttributeFilename
from .. import loader, utils
class DelMessTools(loader.Module):
"""Module to manage and delete your messages in the current chat"""
strings = {
"name": "DelMessTools",
"purge_complete": "All your messages have been deleted.",
"purge_reply_complete": "Messages up to the replied message have been deleted.",
"purge_keyword_complete": "Messages containing the keyword have been deleted.",
"purge_time_complete": "Messages within the specified time range have been deleted.",
"purge_media_complete": "All your media messages have been deleted.",
"purge_length_complete": "Messages with the specified length have been deleted.",
"purge_type_complete": "Messages of the specified type have been deleted.",
"enabled": "It's not operational now anyway.",
"disabled": "Operation status changed to disabled.",
"interrupted": "The deletion was interrupted because you changed your mind.",
"none": "You didn't even intend to delete anything here, but anyway it's disabled now."
}
strings_ru = {
"purge_complete": "Все ваши сообщения были удалены.",
"purge_reply_complete": "Сообщения до указанного ответа были удалены.",
"purge_keyword_complete": "Сообщения, содержащие ключевое слово, были удалены.",
"purge_time_complete": "Сообщения в указанном временном диапазоне были удалены.",
"purge_media_complete": "Все ваши медиа-сообщения были удалены.",
"purge_length_complete": "Сообщения указанной длины были удалены.",
"purge_type_complete": "Сообщения указанного типа были удалены.",
"enabled": "Оно итак сейчас не работает.",
"disabled": "Режим работы изменен на выключено.",
"interrupted": "Удаление было прервано т.к вы передумали.",
"none": "Вы даже не пытались ничего здесь удалить, в любом случае сейчас оно выключено."
}
async def purgecmd(self, message: Message):
""" [reply] [-img] [-voice] [-file] [-all] - delete all your messages in current chat or only ones up to the message you replied to
-all - to delete messages in each topic if this is a forum otherwise the flag'll just be ingored
"""
reply = await message.get_reply_message()
is_last = False
args, types_filter, is_each = self.get_types_filter(message)
is_forum = (await self.client.get_entity(message.chat.id)).forum
status = self.db.get(__name__, "status", {})
status[message.chat.id] = True
self.db.set(__name__, "status", status)
async for i in self.client.iter_messages(message.peer_id):
status = self.db.get(__name__, "status", {})
if status.get(message.chat.id, None) is not True:
return await utils.answer(message, self.strings["interrupted"])
if is_forum and not is_each and utils.get_topic(message) != utils.get_topic(i):
continue
if i.from_id == self.tg_id and self.is_valid_type(i, types_filter):
if reply:
if is_last:
break
if i.id == reply.id:
is_last = True
await message.client.delete_messages(message.peer_id, [i.id])
if reply:
await utils.answer(message, self.strings["purge_reply_complete"])
else:
await utils.answer(message, self.strings["purge_complete"])
async def purgekeywordcmd(self, message: Message):
""" <keyword> [-img] [-voice] [-file] [-all] - delete all your messages containing the specified keyword in the current chat
-all - to delete messages in each topic if this is a forum otherwise the flag'll just be ingored
"""
args = utils.get_args_raw(message)
if not args:
return await utils.answer(message, "Please specify anything because you didn't.")
args, types_filter, is_each = self.get_types_filter(message)
if not args:
return await utils.answer(message, "Please specify a keyword to delete messages.")
is_forum = (await self.client.get_entity(message.chat.id)).forum
status = self.db.get(__name__, "status", {})
status[message.chat.id] = True
self.db.set(__name__, "status", status)
async for i in self.client.iter_messages(message.peer_id):
status = self.db.get(__name__, "status", {})
if status.get(message.chat.id, None) is not True:
return await utils.answer(message, self.strings["interrupted"])
if is_forum and not is_each and utils.get_topic(message) != utils.get_topic(i):
continue
if i.from_id == self.tg_id and args.lower() in (i.text or '').lower() and self.is_valid_type(i, types_filter):
await message.client.delete_messages(message.chat.id, [i.id])
await utils.answer(message, self.strings["purge_keyword_complete"])
async def purgetimecmd(self, message: Message):
""" <start_time> <end_time> [-img] [-voice] [-file] [-all] - delete all your messages within the specified time range in the current chat
-all - to delete messages in each topic if this is a forum otherwise the flag'll just be ingored
Time format: YYYY-MM-DD HH:MM:SS
"""
args = utils.get_args_raw(message)
if not args:
return await utils.answer(message, "Please specify anything because you didn't.")
args, types_filter, is_each = self.get_types_filter(message)
args = args.split()
if not args or len(args) < 2:
return await utils.answer(message, "Please specify the start and end time in the format: YYYY-MM-DD HH:MM:SS")
from datetime import datetime
try:
start_time = datetime.strptime(args[0], "%Y-%m-%d %H:%M:%S")
end_time = datetime.strptime(args[1], "%Y-%m-%d %H:%M:%S")
except ValueError:
return await utils.answer(message, "Invalid time format. Please use the format: YYYY-MM-DD HH:MM:SS")
is_forum = (await self.client.get_entity(message.chat.id)).forum
status = self.db.get(__name__, "status", {})
status[message.chat.id] = True
self.db.set(__name__, "status", status)
async for i in self.client.iter_messages(message.peer_id):
status = self.db.get(__name__, "status", {})
if status.get(message.chat.id, None) is not True:
return await utils.answer(message, self.strings["interrupted"])
if is_forum and not is_each and utils.get_topic(message) != utils.get_topic(i):
continue
if i.from_id == self.tg_id and start_time <= i.date <= end_time and self.is_valid_type(i, types_filter):
await message.client.delete_messages(message.peer_id, [i.id])
await utils.answer(message, self.strings["purge_time_complete"])
async def purgelengthcmd(self, message: Message):
""" <length> [-img] [-voice] [-file] [-all] - delete all your messages with the specified length in the current chat
-all - to delete messages in each topic if this is a forum otherwise the flag'll just be ingored
"""
args = utils.get_args_raw(message)
if not args:
return await utils.answer(message, "Please specify anything because you didn't.")
args, types_filter, is_each = self.get_types_filter(message)
if not args:
return await utils.answer(message, "Please specify a valid length.")
length = int(args)
is_forum = (await self.client.get_entity(message.chat.id)).forum
status = self.db.get(__name__, "status", {})
status[message.chat.id] = True
self.db.set(__name__, "status", status)
async for i in self.client.iter_messages(message.peer_id):
status = self.db.get(__name__, "status", {})
if status.get(message.chat.id, None) is not True:
return await utils.answer(message, self.strings["interrupted"])
if is_forum and not is_each and utils.get_topic(message) != utils.get_topic(i):
continue
if i.from_id == self.tg_id and len(i.text or '') == length and self.is_valid_type(i, types_filter):
await message.client.delete_messages(message.peer_id, [i.id])
await utils.answer(message, self.strings["purge_length_complete"])
async def nopurgecmd(self, message: Message):
"""
Interrupt the deletion process
Use in the chat where you've previously started deletion
"""
chat_id = utils.get_chat_id(message)
status = self.db.get(__name__, "status", {})
_status = status.get(chat_id, None)
status[chat_id] = False
self.db.set(__name__, "status", status)
if _status is True:
await utils.answer(message, self.strings["disabled"])
elif _status is False:
await utils.answer(message, self.strings["enabled"])
else:
await utils.answer(message, self.strings["none"])
def get_types_filter(self, message: Message):
""" Get the types filter from the command arguments."""
args = utils.get_args_raw(message).split()
types_filter = []
valid_types = ["-img", "-voice", "-file", "-all"]
is_each = "-all" in args
for i, arg in enumerate(args):
if arg in valid_types:
_args = " ".join(args[:i])
args_ = " ".join(args[i:])
break
if "-img" in args_:
types_filter.append("img")
if "-voice" in args_:
types_filter.append("voice")
if "-file" in args_:
types_filter.append("file")
if "-all" in args_:
is_each = True
return _args, types_filter, is_each
def is_valid_type(self, message: Message, types_filter):
""" Check if the message matches the specified types filter. """
if not types_filter:
return True # No filtering means all types are valid
if "img" in types_filter and message.photo:
return True
if "voice" in types_filter and message.voice:
return True
if "file" in types_filter and isinstance(message.document, DocumentAttributeFilename):
return True
return False