-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_update.py
274 lines (249 loc) · 9.32 KB
/
auto_update.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
__version__ = (1, 0, 27)
# ▄▀█ █▄ █ █▀█ █▄ █ █▀█ ▀▀█ █▀█ █ █ █▀
# █▀█ █ ▀█ █▄█ █ ▀█ ▀▀█ █ ▀▀█ ▀▀█ ▄█
#
# © Copyright 2024
#
# developed by @anon97945
#
# https://t.me/apodiktum_modules
# https://github.com/anon97945
#
# 🔒 Licensed under the GNU GPLv3
# 🌐 https://www.gnu.org/licenses/gpl-3.0.html
# meta developer: @apodiktum_modules
# meta banner: https://t.me/apodiktum_dumpster/11
# meta pic: https://t.me/apodiktum_dumpster/13
# scope: hikka_only
# scope: hikka_min 1.3.3
import asyncio
import contextlib
import logging
from telethon.tl.types import Message
from .. import loader, utils
logger = logging.getLogger(__name__)
skip_update = ["[do not install]", "[unstable]", "[test]"]
@loader.tds
class ApodiktumAutoUpdateMod(loader.Module):
"""
Automatically update your Hikka Userbot
"""
strings = {
"name": "Apo-AutoUpdater",
"developer": "@anon97945",
"_cfg_auto_update": (
"Whether the Hikka Userbot should automatically update or not."
),
"_cfg_auto_update_delay": (
"Choose a delay to wait to start the automatic update."
),
"_cfg_cst_auto_migrate": "Wheather to auto migrate defined changes on startup.",
"_cfg_update_msg_read": "Whether to mark the message as read or not.",
"skip_old": "The update was skipped due to a newer update.",
"skip_update": "The update was skipped due to {}.\n{}",
"updating": (
"Hikka Userbot will be automatically updated in {} seconds.\n\n"
"Changelog:\n{}"
),
}
strings_de = {
"_cfg_auto_update": (
"Ob der Hikka Userbot automatisch aktualisieren soll oder nicht."
),
"_cfg_auto_update_delay": (
"Wählen Sie eine Wartezeit bis zum Start des automatischen Updates."
),
"_cfg_update_msg_read": (
"Ob die Nachricht als gelesen markiert werden soll oder nicht."
),
"_cmd_doc_cautoupdate": (
"Dadurch wird die Konfiguration für das Modul geöffnet."
),
"skip_old": "Das Update wurde aufgrund eines neueren Updates übersprungen.",
"skip_update": "Das Update wurde wegen {} übersprungen.\n{}",
"updating": (
"Hikka Userbot wird in {} Sekunden automatisch aktualisiert.\n\n"
"Changelog:\n{}"
),
}
strings_ru = {
"_cfg_auto_update": (
"Должен ли Hikka UserBot обновляться автоматически или нет."
),
"_cfg_auto_update_delay": "Выберите задержку для автоматического обновления.",
"_cfg_update_msg_read": (
"Отмечать ли сообщение с обновлением как прочитанное или нет."
),
"skip_old": (
"Обновление было пропущено в связи с появлением более новой версии."
),
"skip_update": "Обновление было пропущено из-за {}.\n{}",
"_cmd_doc_cautoupdate": "Открыть конфиг модуля.",
"updating": (
"Hikka будет автоматически обновлена через {} секунд.\n\n"
"Список изменений:\n{}"
),
}
all_strings = {
"strings": strings,
"strings_en": strings,
"strings_de": strings_de,
"strings_ru": strings_ru,
}
changes = {
"migration1": {
"name": {
"old": "Apo AutoUpdater",
"new": "Apo-AutoUpdater",
},
},
}
def __init__(self):
self._ratelimit = []
self.config = loader.ModuleConfig(
loader.ConfigValue(
"auto_update",
True,
doc=lambda: self.strings("_cfg_auto_update"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"mark_read",
True,
doc=lambda: self.strings("_cfg_update_msg_read"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"update_delay",
"600",
doc=lambda: self.strings("_cfg_auto_update_delay"),
validator=loader.validators.Integer(minimum=60),
),
loader.ConfigValue(
"auto_migrate",
True,
doc=lambda: self.strings("_cfg_cst_auto_migrate"),
validator=loader.validators.Boolean(),
), # for MigratorClass
)
async def client_ready(self):
self.apo_lib = await self.import_lib(
"https://raw.githubusercontent.com/anon97945/hikka-libs/master/apodiktum_library.py",
suspend_on_error=True,
)
await self.apo_lib.migrator.auto_migrate_handler(
self.__class__.__name__,
self.strings("name"),
self.changes,
self.config["auto_migrate"],
)
asyncio.ensure_future(self._check_on_load())
async def cautoupdatecmd(self, message: Message):
"""
This will open the config for the module.
"""
name = self.strings("name")
await self.allmodules.commands["config"](
await utils.answer(message, f"{self.get_prefix()}config {name}")
)
@staticmethod
async def _buttonhandler(
bmsg: Message,
chatid: int,
caption1: str,
caption2: str,
data_btn1: str,
data_btn2: str,
) -> bool:
fnd_btn1 = False
fnd_btn2 = False
bmsg = await bmsg.client.get_messages(chatid, ids=bmsg.id)
buttons = bmsg.buttons
if (
caption1 in bmsg.message and caption2 in bmsg.message
) and bmsg.buttons is not None:
for row in buttons:
for button in row:
if data_btn1 in str(button.data):
fnd_btn1 = True
if data_btn2 in str(button.data):
fnd_btn2 = True
if fnd_btn1 and fnd_btn2:
return True
return False
async def _autoupdate(self, message: Message):
if self.config["mark_read"]:
await self._client.send_read_acknowledge(
message.peer_id,
clear_mentions=True,
)
self.apo_lib.utils.log(
logging.INFO,
__name__,
self.strings("updating").format(
self.config["update_delay"],
"\n".join(self.apo_lib.utils.raw_text(message).splitlines()[5:]),
),
)
await asyncio.sleep(self.config["update_delay"])
with contextlib.suppress(Exception):
return await message.click(0)
async def _check_skip(self, message: Message) -> bool:
last_commit = self.apo_lib.utils.raw_text(message).splitlines()[5].lower()
for x in skip_update:
if x.lower() in last_commit and "revert" not in last_commit:
self.apo_lib.utils.log(
logging.INFO,
__name__,
self.strings("skip_update").format(x, last_commit),
)
return True
return False
async def _check_on_load(self):
if not self.config["auto_update"]:
return
async for message in self.client.iter_messages(
entity=self.inline.bot_id, limit=5
):
if (
isinstance(message, Message)
and message.sender_id == self.inline.bot_id
and await self._buttonhandler(
message,
self.inline.bot_id,
"🌘",
"🔮",
"hikka_update",
"hikka_upd_ignore",
)
):
if await self._check_skip(message):
return
with contextlib.suppress(Exception):
self._autoupdate_task.cancel()
self.apo_lib.utils.log(
logging.INFO, __name__, self.strings("skip_old")
)
self._autoupdate_task = asyncio.ensure_future(self._autoupdate(message))
@loader.watcher("in", "only_messages", "only_pm")
async def watcher(self, message: Message):
if (
self.config["auto_update"]
and utils.get_chat_id(message) == self.inline.bot_id
and message.sender_id == self.inline.bot_id
and await self._buttonhandler(
message,
self.inline.bot_id,
"🌘",
"🔮",
"hikka_update",
"hikka_upd_ignore",
)
):
if await self._check_skip(message):
return
with contextlib.suppress(Exception):
self._autoupdate_task.cancel()
self.apo_lib.utils.log(logging.INFO, __name__, self.strings("skip_old"))
self._autoupdate_task = asyncio.ensure_future(self._autoupdate(message))
return