-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapolib_controller.py
279 lines (250 loc) · 10.9 KB
/
apolib_controller.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
275
276
277
278
279
__version__ = (0, 1, 20)
# ▄▀█ █▄ █ █▀█ █▄ █ █▀█ ▀▀█ █▀█ █ █ █▀
# █▀█ █ ▀█ █▄█ █ ▀█ ▀▀█ █ ▀▀█ ▀▀█ ▄█
#
# © 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
import re
from telethon.tl.types import Message
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class ApodiktumLibControllerMod(loader.Module):
"""
This is a Library Controller module required for Apodiktum Library Modules and also 3rd-party modules.
>>Do not unload this!<<
"""
strings = {
"name": "Apo-LibController",
"developer": "@anon97945",
"forced_lang": "<b>Forced language {}!</b>",
"incorrect_language": "🚫 <b>Incorrect language specified.</b>",
"lang_removed": "<b>Forced chat language removed!</b>",
"lang_saved": "{} <b>forced chat language saved!</b>",
"no_lang": "No forced language in this chat.",
"q_pending": "\n<code> - Pending:</code> <code>{}</code>",
"q_total_count": "\n<code> - Total count:</code> <code>{}</code>",
"q_watcher_str": (
"<b><u><i>Queue"
" Watcher:</i></u></b>\n<b>Queue:</b>\n<code>{}</code>\n\n<b>Tasks:</b>\n<code>{}</code>"
),
"queues": "<b><u><i>Queues:</i></u></b>",
"t_cancelled": "\n<code> - Cancelled:</code> <code>{}</code>",
"t_done": "\n<code> - Done:</code> <code>{}</code>",
"t_id": "\n<code> - ID:</code> <code>{}</code>",
"t_name": "\n<code> - Name:</code> <code>{}</code>",
"tasks": "<b><u><i>Tasks:</i></u></b>",
"version_str": "📦 <b>Current Apodiktum Library <code>{}</code>.</b>",
}
strings_de = {
"_cls_doc": (
"Dies ist ein Bibliothekssteuerungsmodul, das für Apodiktum Library"
" Module und auch Module von Drittanbietern benötigt wird.\n>>Nicht"
" entfernen!<<"
),
"_cmd_doc_capolib": "Dadurch wird die Konfiguration für das Modul geöffnet.",
"_cmd_doc_vapolib": (
"Zeigt die aktuelle Version des Apodiktum_Library Moduls an."
),
"_cmd_doc_qapolib": (
"Zeigt die aktuellen Queues und Tasks der Apodiktum Library an."
),
"forced_lang": "<b>Für diesen Chat ist die Sprache {}!</b>",
"incorrect_language": "🚫 <b>Falsche Sprache angegeben.</b>",
"lang_removed": "<b>Für diesen Chat wurde die Sprache entfernt!</b>",
"lang_saved": "{} <b>Sprache für diesen Chat gespeichert!</b>",
"no_lang": "Keine Sprache für diesen Chat gesetzt.",
"q_pending": "\n<code> - Ausstehend:</code> <code>{}</code>",
"q_total_count": "\n<code> - Total count:</code> <code>{}</code>",
"q_watcher_str": (
"<b><u><i>Queue"
" Watcher:</i></u></b>\n<b>Queue:</b>\n<code>{}</code>\n\n<b>Tasks:</b>\n<code>{}</code>"
),
"queues": "<b><u><i>Queues:</i></u></b>",
"t_cancelled": "\n<code> - Abgebrochen:</code> <code>{}</code>",
"t_done": "\n<code> - Abgeschlossen:</code> <code>{}</code>",
"t_id": "\n<code> - ID:</code> <code>{}</code>",
"t_name": "\n<code> - Name:</code> <code>{}</code>",
"tasks": "<b><u><i>Tasks:</i></u></b>",
"version_str": "📦 <b>Aktuelle Apodiktum_Library <code>{}</code>.</b>",
}
strings_ru = {
"_cmd_doc_capolib": "Это откроет конфиг для модуля.",
}
all_strings = {
"strings": strings,
"strings_en": strings,
"strings_de": strings_de,
"strings_ru": strings_ru,
}
def __init__(self):
self.ratelimit = []
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,
)
self._lib_classname = self.apo_lib.__class__.__name__
self._lib_db = self._db[self._lib_classname]
self._chats_db = self._lib_db.setdefault("chats", {})
if self.apo_lib._controllerloader.unload_controller:
self.apo_lib._controllerloader.unload_controller = False
async def capolibcmd(self, message: Message):
"""
This will open the config for the module.
"""
await self.allmodules.commands["config"](
await utils.answer(
message, f"{self.get_prefix()}config {self._lib_classname}"
)
)
async def unloadapocontrollercmd(self, message: Message):
"""
This will unload the module and prevent it from loading through apo_lib.
!!Beware that this will break all modules that depend on apo_lib q_watcher. Use this only if you know what you are doing!!
"""
self.apo_lib._controllerloader.unload_controller = True
name = self.strings("name")
await self.allmodules.commands["unloadmod"](
await utils.answer(message, f"{self.get_prefix()}unloadmod {name}")
)
async def vapolibcmd(self, message: Message):
"""
shows the current version of the apodiktum_library.
"""
if lib_version := getattr(self.allmodules, "_apodiktum_lib_version", None):
version_str = f"v{lib_version[0]}.{lib_version[1]}.{lib_version[2]}"
else:
version_str = "v Unknown"
await utils.answer(
message,
self.apo_lib.utils.get_str("version_str", self.all_strings, message).format(
version_str
),
)
async def qapolibcmd(self, message):
"""
shows the current queue and tasks of the apodiktum_library.
"""
q_string = self.apo_lib.utils.get_str("queues", self.all_strings, message)
t_string = self.apo_lib.utils.get_str("tasks", self.all_strings, message)
tasks = "tasks="
await asyncio.sleep(0.01)
for name in self.apo_lib.watcher_q._watcher_q_queue:
q_string += f"\n<code>{name}</code>"
for q in self.apo_lib.watcher_q._watcher_q_queue[name]:
q_string += f"\n<code> - {q}:</code>"
q_string += self.apo_lib.utils.get_str(
"q_total_count", self.all_strings, message
).format(
"".join(
re.findall(
"\d+",
"".join(
s
for s in str(
self.apo_lib.watcher_q._watcher_q_queue[name][q]
).split()
if tasks.lower() in s.lower()
),
)
)
)
q_string += self.apo_lib.utils.get_str(
"q_pending", self.all_strings, message
).format(self.apo_lib.watcher_q._watcher_q_queue[name][q].qsize())
for name in self.apo_lib.watcher_q._watcher_q_task:
t_string += f"\n<code>{name}</code>"
for t in self.apo_lib.watcher_q._watcher_q_task[name]:
t_string += f"\n<code> - {t}:</code> "
t_string += self.apo_lib.utils.get_str(
"t_name", self.all_strings, message
).format(self.apo_lib.watcher_q._watcher_q_task[name][t].get_name())
t_string += self.apo_lib.utils.get_str(
"t_id", self.all_strings, message
).format(id((self.apo_lib.watcher_q._watcher_q_task[name][t])))
t_string += self.apo_lib.utils.get_str(
"t_cancelled", self.all_strings, message
).format(self.apo_lib.watcher_q._watcher_q_task[name][t].cancelled())
t_string += self.apo_lib.utils.get_str(
"t_done", self.all_strings, message
).format(self.apo_lib.watcher_q._watcher_q_task[name][t].done())
string = f"{q_string or None}\n\n{t_string or None}"
await utils.answer(message, string)
async def fclcmd(self, message: Message):
"""
<langcode> | force language of supported modules in this chat.
"""
args = utils.get_args_raw(message)
chat_id = utils.get_chat_id(message)
chatid_str = str(chat_id)
chatid_db = self._chats_db.setdefault(chatid_str, {})
if not args:
if len(args) not in [0, 2]:
await utils.answer(
message,
self.apo_lib.utils.get_str("incorrect_language"),
self.all_strings,
message,
)
return
if "forced_lang" in chatid_db:
await utils.answer(
message,
self.apo_lib.utils.get_str(
"forced_lang", self.all_strings, message
).format(
utils.get_lang_flag(
chatid_db.get("forced_lang").lower()
if chatid_db.get("forced_lang").lower() != "en"
else "gb"
)
),
)
else:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_lang", self.all_strings, message),
)
return
chatid_db.update({"forced_lang": args.lower()})
self._db.set(self._lib_classname, "chats", self._chats_db)
await utils.answer(
message,
self.apo_lib.utils.get_str("lang_saved", self.all_strings, message).format(
utils.get_lang_flag(args.lower() if args.lower() != "en" else "gb")
),
)
async def remfclcmd(self, message: Message):
"""
remove force language in this chat.
"""
chat_id = utils.get_chat_id(message)
chatid_str = str(chat_id)
chatid_db = self._chats_db.setdefault(chatid_str, {})
if chatid_db.get("forced_lang"):
chatid_db.pop("forced_lang")
self._db.set(self._lib_classname, "chats", self._chats_db)
await utils.answer(
message,
self.apo_lib.utils.get_str("lang_removed", self.all_strings, message),
)
@loader.watcher(only_messages=True)
async def watcher(self, message: Message):
with contextlib.suppress(Exception):
await self.apo_lib.watcher_q.msg_reciever(message)