Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/users_settings.py
1619 views
1
from aiofiles.os import remove, path as aiopath, makedirs
2
from asyncio import sleep
3
from functools import partial
4
from html import escape
5
from io import BytesIO
6
from os import getcwd
7
from pyrogram.filters import create
8
from pyrogram.handlers import MessageHandler
9
from time import time
10
from re import findall
11
12
from .. import (
13
user_data,
14
excluded_extensions,
15
auth_chats,
16
sudo_users,
17
included_extensions,
18
)
19
from ..core.config_manager import Config
20
from ..core.telegram_manager import TgClient
21
from ..helper.ext_utils.db_handler import database
22
from ..helper.ext_utils.media_utils import create_thumb
23
from ..helper.telegram_helper.button_build import ButtonMaker
24
from ..helper.ext_utils.help_messages import user_settings_text
25
from ..helper.ext_utils.bot_utils import (
26
update_user_ldata,
27
new_task,
28
get_size_bytes,
29
)
30
from ..helper.telegram_helper.message_utils import (
31
send_message,
32
edit_message,
33
send_file,
34
delete_message,
35
)
36
37
handler_dict = {}
38
39
leech_options = [
40
"THUMBNAIL",
41
"LEECH_SPLIT_SIZE",
42
"LEECH_DUMP_CHAT",
43
"LEECH_FILENAME_PREFIX",
44
"THUMBNAIL_LAYOUT",
45
]
46
rclone_options = ["RCLONE_CONFIG", "RCLONE_PATH", "RCLONE_FLAGS"]
47
gdrive_options = ["TOKEN_PICKLE", "GDRIVE_ID", "INDEX_URL"]
48
49
50
async def get_user_settings(from_user, stype="main"):
51
user_id = from_user.id
52
name = from_user.mention
53
buttons = ButtonMaker()
54
rclone_conf = f"rclone/{user_id}.conf"
55
token_pickle = f"tokens/{user_id}.pickle"
56
user_dict = user_data.get(user_id, {})
57
58
if stype == "leech":
59
thumbpath = f"thumbnails/{user_id}.jpg"
60
buttons.data_button("Thumbnail", f"userset {user_id} menu THUMBNAIL")
61
thumbmsg = "Exists" if await aiopath.exists(thumbpath) else "Not Exists"
62
buttons.data_button(
63
"Leech Split Size", f"userset {user_id} menu LEECH_SPLIT_SIZE"
64
)
65
if user_dict.get("LEECH_SPLIT_SIZE", False):
66
split_size = user_dict["LEECH_SPLIT_SIZE"]
67
else:
68
split_size = Config.LEECH_SPLIT_SIZE
69
buttons.data_button(
70
"Leech Destination", f"userset {user_id} menu LEECH_DUMP_CHAT"
71
)
72
if user_dict.get("LEECH_DUMP_CHAT", False):
73
leech_dest = user_dict["LEECH_DUMP_CHAT"]
74
elif "LEECH_DUMP_CHAT" not in user_dict and Config.LEECH_DUMP_CHAT:
75
leech_dest = Config.LEECH_DUMP_CHAT
76
else:
77
leech_dest = "None"
78
buttons.data_button(
79
"Leech Prefix", f"userset {user_id} menu LEECH_FILENAME_PREFIX"
80
)
81
if user_dict.get("LEECH_FILENAME_PREFIX", False):
82
lprefix = user_dict["LEECH_FILENAME_PREFIX"]
83
elif "LEECH_FILENAME_PREFIX" not in user_dict and Config.LEECH_FILENAME_PREFIX:
84
lprefix = Config.LEECH_FILENAME_PREFIX
85
else:
86
lprefix = "None"
87
if (
88
user_dict.get("AS_DOCUMENT", False)
89
or "AS_DOCUMENT" not in user_dict
90
and Config.AS_DOCUMENT
91
):
92
ltype = "DOCUMENT"
93
buttons.data_button("Send As Media", f"userset {user_id} tog AS_DOCUMENT f")
94
else:
95
ltype = "MEDIA"
96
buttons.data_button(
97
"Send As Document", f"userset {user_id} tog AS_DOCUMENT t"
98
)
99
if (
100
user_dict.get("EQUAL_SPLITS", False)
101
or "EQUAL_SPLITS" not in user_dict
102
and Config.EQUAL_SPLITS
103
):
104
buttons.data_button(
105
"Disable Equal Splits", f"userset {user_id} tog EQUAL_SPLITS f"
106
)
107
equal_splits = "Enabled"
108
else:
109
buttons.data_button(
110
"Enable Equal Splits", f"userset {user_id} tog EQUAL_SPLITS t"
111
)
112
equal_splits = "Disabled"
113
if (
114
user_dict.get("MEDIA_GROUP", False)
115
or "MEDIA_GROUP" not in user_dict
116
and Config.MEDIA_GROUP
117
):
118
buttons.data_button(
119
"Disable Media Group", f"userset {user_id} tog MEDIA_GROUP f"
120
)
121
media_group = "Enabled"
122
else:
123
buttons.data_button(
124
"Enable Media Group", f"userset {user_id} tog MEDIA_GROUP t"
125
)
126
media_group = "Disabled"
127
if (
128
TgClient.IS_PREMIUM_USER
129
and user_dict.get("USER_TRANSMISSION", False)
130
or "USER_TRANSMISSION" not in user_dict
131
and Config.USER_TRANSMISSION
132
):
133
buttons.data_button(
134
"Leech by Bot", f"userset {user_id} tog USER_TRANSMISSION f"
135
)
136
leech_method = "user"
137
elif TgClient.IS_PREMIUM_USER:
138
leech_method = "bot"
139
buttons.data_button(
140
"Leech by User", f"userset {user_id} tog USER_TRANSMISSION t"
141
)
142
else:
143
leech_method = "bot"
144
145
if (
146
TgClient.IS_PREMIUM_USER
147
and user_dict.get("HYBRID_LEECH", False)
148
or "HYBRID_LEECH" not in user_dict
149
and Config.HYBRID_LEECH
150
):
151
hybrid_leech = "Enabled"
152
buttons.data_button(
153
"Disable Hybride Leech", f"userset {user_id} tog HYBRID_LEECH f"
154
)
155
elif TgClient.IS_PREMIUM_USER:
156
hybrid_leech = "Disabled"
157
buttons.data_button(
158
"Enable HYBRID Leech", f"userset {user_id} tog HYBRID_LEECH t"
159
)
160
else:
161
hybrid_leech = "Disabled"
162
163
buttons.data_button(
164
"Thumbnail Layout", f"userset {user_id} menu THUMBNAIL_LAYOUT"
165
)
166
if user_dict.get("THUMBNAIL_LAYOUT", False):
167
thumb_layout = user_dict["THUMBNAIL_LAYOUT"]
168
elif "THUMBNAIL_LAYOUT" not in user_dict and Config.THUMBNAIL_LAYOUT:
169
thumb_layout = Config.THUMBNAIL_LAYOUT
170
else:
171
thumb_layout = "None"
172
173
buttons.data_button("Back", f"userset {user_id} back")
174
buttons.data_button("Close", f"userset {user_id} close")
175
176
text = f"""<u>Leech Settings for {name}</u>
177
Leech Type is <b>{ltype}</b>
178
Custom Thumbnail <b>{thumbmsg}</b>
179
Leech Split Size is <b>{split_size}</b>
180
Equal Splits is <b>{equal_splits}</b>
181
Media Group is <b>{media_group}</b>
182
Leech Prefix is <code>{escape(lprefix)}</code>
183
Leech Destination is <code>{leech_dest}</code>
184
Leech by <b>{leech_method}</b> session
185
HYBRID Leech is <b>{hybrid_leech}</b>
186
Thumbnail Layout is <b>{thumb_layout}</b>
187
"""
188
elif stype == "rclone":
189
buttons.data_button("Rclone Config", f"userset {user_id} menu RCLONE_CONFIG")
190
buttons.data_button(
191
"Default Rclone Path", f"userset {user_id} menu RCLONE_PATH"
192
)
193
buttons.data_button("Rclone Flags", f"userset {user_id} menu RCLONE_FLAGS")
194
buttons.data_button("Back", f"userset {user_id} back")
195
buttons.data_button("Close", f"userset {user_id} close")
196
rccmsg = "Exists" if await aiopath.exists(rclone_conf) else "Not Exists"
197
if user_dict.get("RCLONE_PATH", False):
198
rccpath = user_dict["RCLONE_PATH"]
199
elif Config.RCLONE_PATH:
200
rccpath = Config.RCLONE_PATH
201
else:
202
rccpath = "None"
203
if user_dict.get("RCLONE_FLAGS", False):
204
rcflags = user_dict["RCLONE_FLAGS"]
205
elif "RCLONE_FLAGS" not in user_dict and Config.RCLONE_FLAGS:
206
rcflags = Config.RCLONE_FLAGS
207
else:
208
rcflags = "None"
209
text = f"""<u>Rclone Settings for {name}</u>
210
Rclone Config <b>{rccmsg}</b>
211
Rclone Path is <code>{rccpath}</code>
212
Rclone Flags is <code>{rcflags}</code>"""
213
elif stype == "gdrive":
214
buttons.data_button("token.pickle", f"userset {user_id} menu TOKEN_PICKLE")
215
buttons.data_button("Default Gdrive ID", f"userset {user_id} menu GDRIVE_ID")
216
buttons.data_button("Index URL", f"userset {user_id} menu INDEX_URL")
217
if (
218
user_dict.get("STOP_DUPLICATE", False)
219
or "STOP_DUPLICATE" not in user_dict
220
and Config.STOP_DUPLICATE
221
):
222
buttons.data_button(
223
"Disable Stop Duplicate", f"userset {user_id} tog STOP_DUPLICATE f"
224
)
225
sd_msg = "Enabled"
226
else:
227
buttons.data_button(
228
"Enable Stop Duplicate", f"userset {user_id} tog STOP_DUPLICATE t"
229
)
230
sd_msg = "Disabled"
231
buttons.data_button("Back", f"userset {user_id} back")
232
buttons.data_button("Close", f"userset {user_id} close")
233
tokenmsg = "Exists" if await aiopath.exists(token_pickle) else "Not Exists"
234
if user_dict.get("GDRIVE_ID", False):
235
gdrive_id = user_dict["GDRIVE_ID"]
236
elif GDID := Config.GDRIVE_ID:
237
gdrive_id = GDID
238
else:
239
gdrive_id = "None"
240
index = user_dict["INDEX_URL"] if user_dict.get("INDEX_URL", False) else "None"
241
text = f"""<u>Gdrive API Settings for {name}</u>
242
Gdrive Token <b>{tokenmsg}</b>
243
Gdrive ID is <code>{gdrive_id}</code>
244
Index URL is <code>{index}</code>
245
Stop Duplicate is <b>{sd_msg}</b>"""
246
else:
247
buttons.data_button("Leech", f"userset {user_id} leech")
248
buttons.data_button("Rclone", f"userset {user_id} rclone")
249
buttons.data_button("Gdrive API", f"userset {user_id} gdrive")
250
251
upload_paths = user_dict.get("UPLOAD_PATHS", {})
252
if not upload_paths and "UPLOAD_PATHS" not in user_dict and Config.UPLOAD_PATHS:
253
upload_paths = Config.UPLOAD_PATHS
254
if not upload_paths:
255
upload_paths = "None"
256
257
buttons.data_button("Upload Paths", f"userset {user_id} menu UPLOAD_PATHS")
258
259
if user_dict.get("DEFAULT_UPLOAD", ""):
260
default_upload = user_dict["DEFAULT_UPLOAD"]
261
elif "DEFAULT_UPLOAD" not in user_dict:
262
default_upload = Config.DEFAULT_UPLOAD
263
du = "Gdrive API" if default_upload == "gd" else "Rclone"
264
dur = "Gdrive API" if default_upload != "gd" else "Rclone"
265
buttons.data_button(
266
f"Upload using {dur}", f"userset {user_id} {default_upload}"
267
)
268
269
user_tokens = user_dict.get("USER_TOKENS", False)
270
tr = "MY" if user_tokens else "OWNER"
271
trr = "OWNER" if user_tokens else "MY"
272
buttons.data_button(
273
f"Use {trr} token/config",
274
f"userset {user_id} tog USER_TOKENS {'f' if user_tokens else 't'}",
275
)
276
277
buttons.data_button(
278
"Excluded Extensions", f"userset {user_id} menu EXCLUDED_EXTENSIONS"
279
)
280
if user_dict.get("EXCLUDED_EXTENSIONS", False):
281
ex_ex = user_dict["EXCLUDED_EXTENSIONS"]
282
elif "EXCLUDED_EXTENSIONS" not in user_dict:
283
ex_ex = excluded_extensions
284
else:
285
ex_ex = "None"
286
287
buttons.data_button(
288
"Included Extensions", f"userset {user_id} menu INCLUDED_EXTENSIONS"
289
)
290
if user_dict.get("INCLUDED_EXTENSIONS", False):
291
inc_ex = user_dict["INCLUDED_EXTENSIONS"]
292
elif "INCLUDED_EXTENSIONS" not in user_dict:
293
inc_ex = included_extensions
294
else:
295
inc_ex = "None"
296
297
if user_dict.get("NAME_SUBSTITUTE", False):
298
ns_msg = "Added"
299
elif "NAME_SUBSTITUTE" not in user_dict and Config.NAME_SUBSTITUTE:
300
ns_msg = "Added"
301
else:
302
ns_msg = "None"
303
buttons.data_button(
304
"Name Substitute", f"userset {user_id} menu NAME_SUBSTITUTE"
305
)
306
307
buttons.data_button("YT-DLP Options", f"userset {user_id} menu YT_DLP_OPTIONS")
308
if user_dict.get("YT_DLP_OPTIONS", False):
309
ytopt = user_dict["YT_DLP_OPTIONS"]
310
elif "YT_DLP_OPTIONS" not in user_dict and Config.YT_DLP_OPTIONS:
311
ytopt = Config.YT_DLP_OPTIONS
312
else:
313
ytopt = "None"
314
315
buttons.data_button("FFmpeg Cmds", f"userset {user_id} menu FFMPEG_CMDS")
316
if user_dict.get("FFMPEG_CMDS", False):
317
ffc = "Exists"
318
elif "FFMPEG_CMDS" not in user_dict and Config.FFMPEG_CMDS:
319
ffc = "Exists"
320
else:
321
ffc = "None"
322
323
if user_dict:
324
buttons.data_button("Reset All", f"userset {user_id} reset all")
325
326
buttons.data_button("Close", f"userset {user_id} close")
327
328
text = f"""<u>Settings for {name}</u>
329
Default Package is <b>{du}</b>
330
Use <b>{tr}</b> token/config
331
Upload Paths is <code>{upload_paths}</code>
332
333
Name substitution is <code>{ns_msg}</code>
334
335
Excluded Extensions is <code>{ex_ex}</code>
336
337
Included Extensions is <code>{inc_ex}</code>
338
339
YT-DLP Options is <code>{ytopt}</code>
340
341
FFMPEG Commands is <b>{ffc}</b>"""
342
343
return text, buttons.build_menu(1)
344
345
346
async def update_user_settings(query, stype="main"):
347
handler_dict[query.from_user.id] = False
348
msg, button = await get_user_settings(query.from_user, stype)
349
await edit_message(query.message, msg, button)
350
351
352
@new_task
353
async def send_user_settings(_, message):
354
from_user = message.from_user
355
handler_dict[from_user.id] = False
356
msg, button = await get_user_settings(from_user)
357
await send_message(message, msg, button)
358
359
360
@new_task
361
async def add_file(_, message, ftype):
362
user_id = message.from_user.id
363
handler_dict[user_id] = False
364
if ftype == "THUMBNAIL":
365
des_dir = await create_thumb(message, user_id)
366
elif ftype == "RCLONE_CONFIG":
367
rpath = f"{getcwd()}/rclone/"
368
await makedirs(rpath, exist_ok=True)
369
des_dir = f"{rpath}{user_id}.conf"
370
await message.download(file_name=des_dir)
371
elif ftype == "TOKEN_PICKLE":
372
tpath = f"{getcwd()}/tokens/"
373
await makedirs(tpath, exist_ok=True)
374
des_dir = f"{tpath}{user_id}.pickle"
375
await message.download(file_name=des_dir)
376
update_user_ldata(user_id, ftype, des_dir)
377
await delete_message(message)
378
await database.update_user_doc(user_id, ftype, des_dir)
379
380
381
@new_task
382
async def add_one(_, message, option):
383
user_id = message.from_user.id
384
handler_dict[user_id] = False
385
user_dict = user_data.get(user_id, {})
386
value = message.text
387
if value.startswith("{") and value.endswith("}"):
388
try:
389
value = eval(value)
390
if user_dict[option]:
391
user_dict[option].update(value)
392
else:
393
update_user_ldata(user_id, option, value)
394
except Exception as e:
395
await send_message(message, str(e))
396
return
397
else:
398
await send_message(message, "It must be dict!")
399
return
400
await delete_message(message)
401
await database.update_user_data(user_id)
402
403
404
@new_task
405
async def remove_one(_, message, option):
406
user_id = message.from_user.id
407
handler_dict[user_id] = False
408
user_dict = user_data.get(user_id, {})
409
names = message.text.split("/")
410
for name in names:
411
if name in user_dict[option]:
412
del user_dict[option][name]
413
await delete_message(message)
414
await database.update_user_data(user_id)
415
416
417
@new_task
418
async def set_option(_, message, option):
419
user_id = message.from_user.id
420
handler_dict[user_id] = False
421
value = message.text
422
if option == "LEECH_SPLIT_SIZE":
423
if not value.isdigit():
424
value = get_size_bytes(value)
425
value = min(int(value), TgClient.MAX_SPLIT_SIZE)
426
elif option == "EXCLUDED_EXTENSIONS":
427
fx = value.split()
428
value = ["aria2", "!qB"]
429
for x in fx:
430
x = x.lstrip(".")
431
value.append(x.strip().lower())
432
elif option == "INCLUDED_EXTENSIONS":
433
fx = value.split()
434
value = []
435
for x in fx:
436
x = x.lstrip(".")
437
value.append(x.strip().lower())
438
elif option == "INDEX_URL":
439
value = value
440
elif option in ["UPLOAD_PATHS", "FFMPEG_CMDS", "YT_DLP_OPTIONS"]:
441
if value.startswith("{") and value.endswith("}"):
442
try:
443
value = eval(value)
444
except Exception as e:
445
await send_message(message, str(e))
446
return
447
else:
448
await send_message(message, "It must be dict!")
449
return
450
update_user_ldata(user_id, option, value)
451
await delete_message(message)
452
await database.update_user_data(user_id)
453
454
455
async def get_menu(option, message, user_id):
456
handler_dict[user_id] = False
457
user_dict = user_data.get(user_id, {})
458
buttons = ButtonMaker()
459
if option in ["THUMBNAIL", "RCLONE_CONFIG", "TOKEN_PICKLE"]:
460
key = "file"
461
else:
462
key = "set"
463
buttons.data_button("Set", f"userset {user_id} {key} {option}")
464
if option in user_dict and key != "file":
465
buttons.data_button("Reset", f"userset {user_id} reset {option}")
466
buttons.data_button("Remove", f"userset {user_id} remove {option}")
467
if option == "FFMPEG_CMDS":
468
ffc = None
469
if user_dict.get("FFMPEG_CMDS", False):
470
ffc = user_dict["FFMPEG_CMDS"]
471
buttons.data_button("Add one", f"userset {user_id} addone {option}")
472
buttons.data_button("Remove one", f"userset {user_id} rmone {option}")
473
elif "FFMPEG_CMDS" not in user_dict and Config.FFMPEG_CMDS:
474
ffc = Config.FFMPEG_CMDS
475
if ffc:
476
buttons.data_button("FFMPEG VARIABLES", f"userset {user_id} ffvar")
477
buttons.data_button("View", f"userset {user_id} view {option}")
478
elif option in user_dict and user_dict[option]:
479
if option == "THUMBNAIL":
480
buttons.data_button("View", f"userset {user_id} view {option}")
481
elif option in ["YT_DLP_OPTIONS", "UPLOAD_PATHS"]:
482
buttons.data_button("Add one", f"userset {user_id} addone {option}")
483
buttons.data_button("Remove one", f"userset {user_id} rmone {option}")
484
if option in leech_options:
485
back_to = "leech"
486
elif option in rclone_options:
487
back_to = "rclone"
488
elif option in gdrive_options:
489
back_to = "gdrive"
490
else:
491
back_to = "back"
492
buttons.data_button("Back", f"userset {user_id} {back_to}")
493
buttons.data_button("Close", f"userset {user_id} close")
494
text = f"Edit menu for: {option}"
495
await edit_message(message, text, buttons.build_menu(2))
496
497
498
async def set_ffmpeg_variable(_, message, key, value, index):
499
user_id = message.from_user.id
500
handler_dict[user_id] = False
501
txt = message.text
502
user_dict = user_data.setdefault(user_id, {})
503
ffvar_data = user_dict.setdefault("FFMPEG_VARIABLES", {})
504
ffvar_data = ffvar_data.setdefault(key, {})
505
ffvar_data = ffvar_data.setdefault(index, {})
506
ffvar_data[value] = txt
507
await delete_message(message)
508
await database.update_user_data(user_id)
509
510
511
async def ffmpeg_variables(
512
client, query, message, user_id, key=None, value=None, index=None
513
):
514
user_dict = user_data.get(user_id, {})
515
ffc = None
516
if user_dict.get("FFMPEG_CMDS", False):
517
ffc = user_dict["FFMPEG_CMDS"]
518
elif "FFMPEG_CMDS" not in user_dict and Config.FFMPEG_CMDS:
519
ffc = Config.FFMPEG_CMDS
520
if ffc:
521
buttons = ButtonMaker()
522
if key is None:
523
msg = "Choose which key you want to fill/edit variables in it:"
524
for k, v in list(ffc.items()):
525
add = False
526
for l in v:
527
if variables := findall(r"\{(.*?)\}", l):
528
add = True
529
if add:
530
buttons.data_button(k, f"userset {user_id} ffvar {k}")
531
buttons.data_button("Back", f"userset {user_id} menu FFMPEG_CMDS")
532
buttons.data_button("Close", f"userset {user_id} close")
533
elif key in ffc and value is None:
534
msg = f"Choose which variable you want to fill/edit: <u>{key}</u>\n\nCMDS:\n{ffc[key]}"
535
for ind, vl in enumerate(ffc[key]):
536
if variables := set(findall(r"\{(.*?)\}", vl)):
537
for var in variables:
538
buttons.data_button(
539
var, f"userset {user_id} ffvar {key} {var} {ind}"
540
)
541
buttons.data_button(
542
"Reset", f"userset {user_id} ffvar {key} ffmpegvarreset"
543
)
544
buttons.data_button("Back", f"userset {user_id} ffvar")
545
buttons.data_button("Close", f"userset {user_id} close")
546
elif key in ffc and value:
547
old_value = (
548
user_dict.get("FFMPEG_VARIABLES", {})
549
.get(key, {})
550
.get(index, {})
551
.get(value, "")
552
)
553
msg = f"Edit/Fill this FFmpeg Variable: <u>{key}</u>\n\nItem: {ffc[key][int(index)]}\n\nVariable: {value}"
554
if old_value:
555
msg += f"\n\nCurrent Value: {old_value}"
556
buttons.data_button("Back", f"userset {user_id} setevent")
557
buttons.data_button("Close", f"userset {user_id} close")
558
else:
559
return
560
await edit_message(message, msg, buttons.build_menu(2))
561
if key in ffc and value:
562
pfunc = partial(set_ffmpeg_variable, key=key, value=value, index=index)
563
await event_handler(client, query, pfunc)
564
await ffmpeg_variables(client, query, message, user_id, key)
565
566
567
async def event_handler(client, query, pfunc, photo=False, document=False):
568
user_id = query.from_user.id
569
handler_dict[user_id] = True
570
start_time = time()
571
572
async def event_filter(_, __, event):
573
if photo:
574
mtype = event.photo
575
elif document:
576
mtype = event.document
577
else:
578
mtype = event.text
579
user = event.from_user or event.sender_chat
580
return bool(
581
user.id == user_id and event.chat.id == query.message.chat.id and mtype
582
)
583
584
handler = client.add_handler(
585
MessageHandler(pfunc, filters=create(event_filter)), group=-1
586
)
587
588
while handler_dict[user_id]:
589
await sleep(0.5)
590
if time() - start_time > 60:
591
handler_dict[user_id] = False
592
client.remove_handler(*handler)
593
594
595
@new_task
596
async def edit_user_settings(client, query):
597
from_user = query.from_user
598
user_id = from_user.id
599
name = from_user.mention
600
message = query.message
601
data = query.data.split()
602
handler_dict[user_id] = False
603
thumb_path = f"thumbnails/{user_id}.jpg"
604
rclone_conf = f"rclone/{user_id}.conf"
605
token_pickle = f"tokens/{user_id}.pickle"
606
user_dict = user_data.get(user_id, {})
607
if user_id != int(data[1]):
608
await query.answer("Not Yours!", show_alert=True)
609
elif data[2] == "setevent":
610
await query.answer()
611
elif data[2] in ["leech", "gdrive", "rclone"]:
612
await query.answer()
613
await update_user_settings(query, data[2])
614
elif data[2] == "menu":
615
await query.answer()
616
await get_menu(data[3], message, user_id)
617
elif data[2] == "tog":
618
await query.answer()
619
update_user_ldata(user_id, data[3], data[4] == "t")
620
if data[3] == "STOP_DUPLICATE":
621
back_to = "gdrive"
622
elif data[3] == "USER_TOKENS":
623
back_to = "main"
624
else:
625
back_to = "leech"
626
await update_user_settings(query, stype=back_to)
627
await database.update_user_data(user_id)
628
elif data[2] == "file":
629
await query.answer()
630
buttons = ButtonMaker()
631
if data[3] == "THUMBNAIL":
632
text = "Send a photo to save it as custom thumbnail. Timeout: 60 sec"
633
elif data[3] == "RCLONE_CONFIG":
634
text = "Send rclone.conf. Timeout: 60 sec"
635
else:
636
text = "Send token.pickle. Timeout: 60 sec"
637
buttons.data_button("Back", f"userset {user_id} setevent")
638
buttons.data_button("Close", f"userset {user_id} close")
639
await edit_message(message, text, buttons.build_menu(1))
640
pfunc = partial(add_file, ftype=data[3])
641
await event_handler(
642
client,
643
query,
644
pfunc,
645
photo=data[3] == "THUMBNAIL",
646
document=data[3] != "THUMBNAIL",
647
)
648
await get_menu(data[3], message, user_id)
649
elif data[2] == "ffvar":
650
await query.answer()
651
key = data[3] if len(data) > 3 else None
652
value = data[4] if len(data) > 4 else None
653
if value == "ffmpegvarreset":
654
user_dict = user_data.get(user_id, {})
655
ff_data = user_dict.get("FFMPEG_VARIABLES", {})
656
if key in ff_data:
657
del ff_data[key]
658
await database.update_user_data(user_id)
659
return
660
index = data[5] if len(data) > 5 else None
661
await ffmpeg_variables(client, query, message, user_id, key, value, index)
662
elif data[2] in ["set", "addone", "rmone"]:
663
await query.answer()
664
buttons = ButtonMaker()
665
if data[2] == "set":
666
text = user_settings_text[data[3]]
667
func = set_option
668
elif data[2] == "addone":
669
text = f"Add one or more string key and value to {data[3]}. Example: {{'key 1': 62625261, 'key 2': 'value 2'}}. Timeout: 60 sec"
670
func = add_one
671
elif data[2] == "rmone":
672
text = f"Remove one or more key from {data[3]}. Example: key 1/key2/key 3. Timeout: 60 sec"
673
func = remove_one
674
buttons.data_button("Back", f"userset {user_id} setevent")
675
buttons.data_button("Close", f"userset {user_id} close")
676
await edit_message(message, text, buttons.build_menu(1))
677
pfunc = partial(func, option=data[3])
678
await event_handler(client, query, pfunc)
679
await get_menu(data[3], message, user_id)
680
elif data[2] == "remove":
681
await query.answer("Removed!", show_alert=True)
682
if data[3] in ["THUMBNAIL", "RCLONE_CONFIG", "TOKEN_PICKLE"]:
683
if data[3] == "THUMBNAIL":
684
fpath = thumb_path
685
elif data[3] == "RCLONE_CONFIG":
686
fpath = rclone_conf
687
else:
688
fpath = token_pickle
689
if await aiopath.exists(fpath):
690
await remove(fpath)
691
del user_dict[data[3]]
692
await database.update_user_doc(user_id, data[3])
693
else:
694
update_user_ldata(user_id, data[3], "")
695
await database.update_user_data(user_id)
696
elif data[2] == "reset":
697
await query.answer("Reseted!", show_alert=True)
698
if data[3] in user_dict:
699
del user_dict[data[3]]
700
else:
701
for k in list(user_dict.keys()):
702
if k not in [
703
"SUDO",
704
"AUTH",
705
"THUMBNAIL",
706
"RCLONE_CONFIG",
707
"TOKEN_PICKLE",
708
]:
709
del user_dict[k]
710
await update_user_settings(query)
711
await database.update_user_data(user_id)
712
elif data[2] == "view":
713
await query.answer()
714
if data[3] == "THUMBNAIL":
715
await send_file(message, thumb_path, name)
716
elif data[3] == "FFMPEG_CMDS":
717
ffc = None
718
if user_dict.get("FFMPEG_CMDS", False):
719
ffc = user_dict["FFMPEG_CMDS"]
720
elif "FFMPEG_CMDS" not in user_dict and Config.FFMPEG_CMDS:
721
ffc = Config.FFMPEG_CMDS
722
msg_ecd = str(ffc).encode()
723
with BytesIO(msg_ecd) as ofile:
724
ofile.name = "users_settings.txt"
725
await send_file(message, ofile)
726
elif data[2] in ["gd", "rc"]:
727
await query.answer()
728
du = "rc" if data[2] == "gd" else "gd"
729
update_user_ldata(user_id, "DEFAULT_UPLOAD", du)
730
await update_user_settings(query)
731
await database.update_user_data(user_id)
732
elif data[2] == "back":
733
await query.answer()
734
await update_user_settings(query)
735
else:
736
await query.answer()
737
await delete_message(message.reply_to_message)
738
await delete_message(message)
739
740
741
@new_task
742
async def get_users_settings(_, message):
743
msg = ""
744
if auth_chats:
745
msg += f"AUTHORIZED_CHATS: {auth_chats}\n"
746
if sudo_users:
747
msg += f"SUDO_USERS: {sudo_users}\n\n"
748
if user_data:
749
for u, d in user_data.items():
750
kmsg = f"\n<b>{u}:</b>\n"
751
if vmsg := "".join(
752
f"{k}: <code>{v or None}</code>\n" for k, v in d.items()
753
):
754
msg += kmsg + vmsg
755
if not msg:
756
await send_message(message, "No users data!")
757
return
758
msg_ecd = msg.encode()
759
if len(msg_ecd) > 4000:
760
with BytesIO(msg_ecd) as ofile:
761
ofile.name = "users_settings.txt"
762
await send_file(message, ofile)
763
else:
764
await send_message(message, msg)
765
else:
766
await send_message(message, "No users data!")
767
768