-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcog.py
1021 lines (962 loc) · 46.5 KB
/
cog.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import contextlib
import re
from pathlib import Path
import discord
import humanize
from deepdiff import DeepDiff
from redbot.core import commands
from redbot.core.i18n import Translator, cog_i18n, get_babel_locale
from redbot.core.utils.chat_formatting import bold, box, humanize_list, inline
from tabulate import tabulate
from pylav.compat import json
from pylav.constants.node import NODE_DEFAULT_SETTINGS
from pylav.core.client import Client
from pylav.core.context import PyLavContext
from pylav.extension.bundled_node import LAVALINK_DOWNLOAD_DIR
from pylav.extension.bundled_node.utils import get_jar_ram_actual, get_max_allocation_size
from pylav.helpers.format.ascii import EightBitANSI
from pylav.logging import getLogger
from pylav.type_hints.bot import DISCORD_BOT_TYPE, DISCORD_COG_TYPE_MIXIN
from plmanagednode.view import ConfigureHTTPProxyView, ConfigureIPRotationView
LOGGER = getLogger("PyLav.cog.ManagedNode")
_ = Translator("PyLavManagedNode", Path(__file__))
@cog_i18n(_)
class PyLavManagedNode(DISCORD_COG_TYPE_MIXIN):
"""Configure the managed Lavalink node used by PyLav"""
__version__ = "1.0.0"
lavalink: Client
def __init__(self, bot: DISCORD_BOT_TYPE, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bot = bot
@commands.group(name="plmanaged")
@commands.is_owner()
async def command_plmanaged(self, context: PyLavContext):
"""Configure the managed Lavalink node used by PyLav"""
@command_plmanaged.command(name="version")
async def command_plmanaged_version(self, context: PyLavContext) -> None:
"""Show the version of the Cog and PyLav"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
data = [
(EightBitANSI.paint_white(self.__class__.__name__), EightBitANSI.paint_blue(self.__version__)),
(EightBitANSI.paint_white("PyLav"), EightBitANSI.paint_blue(context.pylav.lib_version)),
]
await context.send(
embed=await context.pylav.construct_embed(
description=box(
tabulate(
data,
headers=(
EightBitANSI.paint_yellow(_("Library / Cog"), bold=True, underline=True),
EightBitANSI.paint_yellow(_("Version"), bold=True, underline=True),
),
tablefmt="fancy_grid",
),
lang="ansi",
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged.command(name="update")
async def command_plmanaged_update(self, context: PyLavContext, update: int = 0) -> None:
"""Update the managed Lavalink node"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
self.pylav.managed_node_controller._up_to_date = False
upstream_data = await self.pylav.managed_node_controller.get_ci_latest_info()
number = upstream_data["number"]
# noinspection PyProtectedMember
if number == await self.pylav._config.fetch_download_id():
await context.send(
embed=await context.pylav.construct_embed(
description=_("The managed Lavalink node is already up to date."),
messageable=context,
),
ephemeral=True,
)
return
if update == 0:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Your node is out of date, to update please run `{command_name_variable_do_not_translate}`."
).format(
command_name_variable_do_not_translate=f"`{context.clean_prefix}{self.command_plmanaged_update.qualified_name} 1`",
),
messageable=context,
),
ephemeral=True,
)
return
self.pylav.managed_node_controller._up_to_date = False
# noinspection PyProtectedMember
await self.pylav.managed_node_controller._download_jar(forced=True)
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"The managed Lavalink node has been updated to version {version_variable_do_not_translate}."
).format(
version_variable_do_not_translate=number,
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged.command(name="toggle")
async def command_plmanaged_toggle(self, context: PyLavContext) -> None:
"""Toggle the managed node on/off.
Changes will be applied after I restart.
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
global_config = self.pylav.lib_db_manager.get_config()
current = await global_config.fetch_enable_managed_node()
await global_config.update_enable_managed_node(not current)
if current:
await context.send(
embed=await context.pylav.construct_embed(
description=_("The PyLav managed node has been enabled."),
messageable=context,
),
ephemeral=True,
)
else:
await context.send(
embed=await context.pylav.construct_embed(
description=_("The PyLav managed node has been disabled."),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged.command(name="updates")
async def ccommand_plmanaged_updates(self, context: PyLavContext) -> None:
"""Toggle the managed node auto updates on/off.
Changes will be applied after I restart.
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
global_config = self.pylav.lib_db_manager.get_config()
current = await global_config.fetch_auto_update_managed_nodes()
await global_config.update_auto_update_managed_nodes(not current)
if current:
await context.send(
embed=await context.pylav.construct_embed(
description=_("The PyLav managed node auto updates have been enabled."),
messageable=context,
),
ephemeral=True,
)
else:
await context.send(
embed=await context.pylav.construct_embed(
description=_("The PyLav managed node auto updates have been disabled."),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged.command(name="heapsize", aliases=["hs", "ram", "memory"])
async def command_plmanaged_heapsize(self, context: PyLavContext, size: str):
"""Set the managed Lavalink node maximum heap-size.
By default, this value is 2G of available RAM in the host machine represented by (65-1023M|1+G) (256M,
256G for example)
This value only represents the maximum amount of RAM allowed to be used at any given point, and does not mean
that the managed Lavalink node will always use this amount of RAM.
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
async def validate_input(arg: str):
locale = f"{get_babel_locale()}"
with contextlib.suppress(Exception):
humanize.i18n.activate(locale)
executable = await self.pylav.lib_db_manager.get_config().fetch_java_path()
total_ram, is_64bit = get_max_allocation_size(executable)
__, __, min_allocation_size, max_allocation_size = get_jar_ram_actual(executable)
match = re.match(r"^(\d+)([MG])$", arg, flags=re.IGNORECASE)
if not match:
await context.send(
embed=await context.pylav.construct_embed(
description=_("Heap-size must be a valid measure of size, e.g. 256M, 256G"),
messageable=context,
),
ephemeral=True,
)
return 0
input_in_bytes = int(match[1]) * 1024 ** (2 if match[2].lower() == "m" else 3)
if input_in_bytes < min_allocation_size:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Heap-size must be at least 64M, however it is recommended to have it set to at least 1G"
),
messageable=context,
),
ephemeral=True,
)
return 0
elif input_in_bytes > max_allocation_size:
if is_64bit:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Heap-size must be less than your system RAM, "
"You currently have {ram_in_bytes_variable_do_not_translate} of RAM available"
).format(ram_in_bytes_variable_do_not_translate=inline(humanize.naturalsize(total_ram))),
messageable=context,
),
ephemeral=True,
)
else:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Heap-size must be less than {limit_variable_do_not_translate} due to your system limitations"
).format(limit_variable_do_not_translate=inline(humanize.naturalsize(total_ram)))
),
ephemeral=True,
)
return 0
return 1
if not (await validate_input(size)):
humanize.i18n.deactivate()
return
humanize.i18n.deactivate()
size = size.upper()
global_config = self.pylav.lib_db_manager.get_config()
extras = await global_config.fetch_extras()
extras["max_ram"] = size
await global_config.update_extras(extras)
await context.send(
embed=await context.pylav.construct_embed(
description=_("The Managed node heap-size set to {bytes_variable_do_not_translate}.").format(
bytes_variable_do_not_translate=inline(size),
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged.group(name="Settings", aliases=["config", "set"])
async def command_plmanaged_config(self, context: PyLavContext):
"""Change the managed node start up configs"""
@command_plmanaged_config.command(name="host")
async def command_plmanaged_config_host(self, context: PyLavContext, host: str):
"""Set the managed node host"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
data["server"]["host"] = host
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("The Managed node host set to {host_variable_do_not_translate}.").format(
host_variable_do_not_translate=inline(host),
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="port")
async def command_plmanaged_config_port(self, context: PyLavContext, port: int):
"""`Dangerous command` Set the managed Lavalink node connection port.
This port is the port the managed Lavalink node binds to, you should only change this if there is a
conflict with the default port because you already have an application using port 2154 on this device.
The value by default is `2154`.
"""
if port < 1024 or port > 49151:
return await context.send(
embed=await context.pylav.construct_embed(
description=_("The port must be between 1024 and 49151"),
messageable=context,
),
ephemeral=True,
)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
data["server"]["port"] = port
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("The managed node port set to {port_variable_do_not_translate}.").format(
port_variable_do_not_translate=port
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.group(name="plugins")
async def command_plmanaged_config_plugins(self, context: PyLavContext):
"""Change the managed node plugins"""
@command_plmanaged_config_plugins.command(name="disable")
async def command_plmanaged_config_plugins_disable(
self, context: PyLavContext, *, plugin: str
): # sourcery skip: low-code-quality
"""Disabled one of the available plugins"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
plugin_str = plugin.lower()
plugins = [
"lavasrc",
"skybot",
"sponsorblock",
"lavalink-filter",
"lava-xm",
"lavasearch",
"youtube",
"lavalyrics",
]
if plugin_str not in plugins:
return await context.send(
embed=await context.pylav.construct_embed(
description=_(
"The plugin must be one of the following: {plugins_variable_do_not_translate}"
).format(plugins_variable_do_not_translate=inline(humanize_list(plugins))),
messageable=context,
),
ephemeral=True,
)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
new_plugins = []
plugin_files = []
folder = LAVALINK_DOWNLOAD_DIR / "plugins"
for plugin in data["lavalink"]["plugins"].copy():
if plugin["dependency"].startswith("com.github.topi314.lavasrc:lavasrc-plugin:"):
if plugin_str != "lavasrc":
new_plugins.append(plugin)
else:
filename = "lavasrc-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("com.github.topi314.lavasearch:lavasearch-plugin:"):
if plugin_str != "lavasearch":
new_plugins.append(plugin)
else:
filename = "lavasearch-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("com.github.topi314.lavalyrics:lavalyrics-plugin:"):
if plugin_str != "lavalyrics":
new_plugins.append(plugin)
else:
filename = "lavalyrics-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("com.dunctebot:skybot-lavalink-plugin:"):
if plugin_str != "skybot":
new_plugins.append(plugin)
else:
filename = "skybot-lavalink-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("dev.lavalink.youtube:youtube-plugin:"):
if plugin_str != "youtube":
new_plugins.append(plugin)
else:
filename = "youtube-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("com.github.topi314.sponsorblock:sponsorblock-plugin:"):
if plugin_str != "sponsorblock":
new_plugins.append(plugin)
else:
filename = "sponsorblock-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
if "repository" in plugin:
plugin.pop("repository")
elif plugin["dependency"].startswith("com.github.esmBot:lava-xm-plugin:"):
if plugin_str != "lava-xm":
new_plugins.append(plugin)
else:
filename = "lava-xm-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
elif plugin["dependency"].startswith("me.rohank05:lavalink-filter-plugin:"):
if plugin_str != "lavalink-filter":
new_plugins.append(plugin)
else:
filename = "lavalink-filter-plugin-"
plugin_files.extend(
[
x
async for x in folder.iterdir()
if x.name.startswith(filename) and x.suffix == ".jar" and x.is_file()
]
)
for file in plugin_files:
try:
await file.unlink()
except Exception as exc:
LOGGER.error("Failed to delete file %s", file, exc_info=exc)
data["lavalink"]["plugins"] = new_plugins
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("Managed node plugin {plugin_variable_do_not_translate} disabled.").format(
plugin_variable_do_not_translate=inline(plugin_str),
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config_plugins.command(name="enable")
async def command_plmanaged_config_plugins_enable(self, context: PyLavContext, *, plugin: str):
"""Enable one of the available plugins"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
plugin_str = plugin.lower()
plugins = [
"lavasrc",
"skybot",
"sponsorblock",
"lavalink-filter",
"lava-xm",
"lavasearch",
"youtube",
"lavalyrics",
]
if plugin_str not in plugins:
return await context.send(
embed=await context.pylav.construct_embed(
description=_(
"The plugin must be one of the following: {plugins_variable_do_not_translate}"
).format(plugins_variable_do_not_translate=inline(humanize_list(plugins))),
messageable=context,
),
ephemeral=True,
)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
new_plugins = data["lavalink"]["plugins"].copy()
for plugin in NODE_DEFAULT_SETTINGS["lavalink"]["plugins"]:
if plugin["dependency"].startswith("com.github.topi314.lavasrc:lavasrc-plugin:"):
if plugin_str == "lavasrc":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("com.dunctebot:skybot-lavalink-plugin:"):
if plugin_str == "skybot-lavalink":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("com.github.topi314.sponsorblock:sponsorblock-plugin:"):
if plugin_str == "sponsorblock":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("com.github.topi314.lavalyrics:lavalyrics-plugin:"):
if plugin_str == "lavalyrics":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("com.github.topi314.lavasearch:lavasearch-plugin:"):
if plugin_str == "lavasearch":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("com.github.esmBot:lava-xm-plugin:"):
if plugin_str == "lavalink-filter":
new_plugins.append(plugin)
elif plugin["dependency"].startswith("me.rohank05:lavalink-filter-plugin:"):
if plugin_str == "lava-xm":
new_plugins.append(plugin)
data["lavalink"]["plugins"] = new_plugins
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("Managed node plugin {plugin_variable_do_not_translate} enabled.").format(
plugin_variable_do_not_translate=inline(plugin_str),
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config_plugins.command(name="update")
async def command_plmanaged_config_plugins_update(self, context: PyLavContext): # sourcery skip: low-code-quality
"""Update the managed node plugins"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
new_plugin_data = []
_temp = set()
for plugin in data["lavalink"]["plugins"].copy():
dependency = ":".join(plugin["dependency"].split(":")[:-1])
if dependency in _temp:
continue
_temp.add(dependency)
repository = "https://maven.lavalink.dev/releases"
if plugin["dependency"].startswith("com.github.topi314.lavasrc:lavasrc-plugin:"):
org = "topi314"
repo = "LavaSrc"
dependency += ":"
elif plugin["dependency"].startswith("com.dunctebot:skybot-lavalink-plugin:"):
org = "DuncteBot"
repo = "skybot-lavalink-plugin"
dependency += ":"
elif plugin["dependency"].startswith("com.github.topi314.sponsorblock:sponsorblock-plugin:"):
org = "topi314"
repo = "Sponsorblock-Plugin"
dependency += ":"
elif plugin["dependency"].startswith("dev.lavalink.youtube:youtube-plugin:"):
org = "lavalink-devs"
repo = "youtube-source"
dependency += ":"
elif plugin["dependency"].startswith("com.github.esmBot:lava-xm-plugin:"):
org = "esmBot"
repo = "lava-xm-plugin"
repository = "https://jitpack.io"
dependency += ":"
elif plugin["dependency"].startswith("me.rohank05:lavalink-filter-plugin:"):
org = "rohank05"
repo = "lavalink-filter-plugin"
repository = "https://jitpack.io"
dependency += ":"
else:
continue
release_data = await (
await self.pylav.cached_session.get(
f"https://api.github.com/repos/{org}/{repo}/releases/latest",
)
).json(loads=json.loads)
name = release_data["tag_name"]
new_plugin_data.append(
{
"dependency": dependency + name,
"repository": repository,
}
)
if diff := DeepDiff(
data["lavalink"]["plugins"], new_plugin_data, ignore_order=True, max_passes=3, cache_size=10000
):
data["lavalink"]["plugins"] = new_plugin_data
update_string = ""
if "values_changed" in diff:
values_changed = diff["values_changed"]
for key, root_value in values_changed.items():
if "'dependency'" not in key:
LOGGER.warning("Ignoring key %s during plugin update - %s", key, root_value)
continue
old_value = None
new_value = None
for sub_key, value in root_value.items():
if sub_key == "old_value":
old_value = value
elif sub_key == "new_value":
new_value = value
if all([old_value, new_value]):
update_string += _(
"{name_variable_do_not_translate} was updated from {old_variable_do_not_translate} to {new_variable_do_not_translate}\n"
).format(
old_variable_do_not_translate=old_value.split(":")[-1],
new_variable_do_not_translate=bold(new_value.split(":")[-1]),
name_variable_do_not_translate=bold(old_value.split(":")[-2]),
)
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("Managed node plugins updated.\n\n{update_variable_do_not_translate}").format(
update_variable_do_not_translate=update_string,
),
messageable=context,
),
ephemeral=True,
)
else:
await context.send(
embed=await context.pylav.construct_embed(
description=_("The managed node plugins are already up to date."),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="source")
async def command_plmanaged_config_source(self, context: PyLavContext, source: str, state: bool):
"""Toggle the managed node sources"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
source = source.lower().strip()
valid_sources = NODE_DEFAULT_SETTINGS["lavalink"]["server"]["sources"].copy()
valid_sources |= NODE_DEFAULT_SETTINGS["plugins"]["lavasrc"]["sources"]
valid_sources |= NODE_DEFAULT_SETTINGS["plugins"]["dunctebot"]["sources"]
if source not in valid_sources:
return await context.send(
embed=await context.pylav.construct_embed(
description=_("Invalid source, {valid_list_variable_do_not_translate} are valid sources").format(
valid_list_variable_do_not_translate=humanize_list(
sorted(list(map(inline, valid_sources.keys())), key=str.lower)
)
),
messageable=context,
),
ephemeral=True,
)
if source in data["lavalink"]["server"]["sources"] and source not in {
"youtube",
}:
data["lavalink"]["server"]["sources"][source] = state
elif source in data["plugins"]["lavasrc"]["sources"]:
data["plugins"]["lavasrc"]["sources"][source] = state
elif source in data["plugins"]["dunctebot"]["sources"]:
data["plugins"]["dunctebot"]["sources"][source] = state
elif source == "youtube":
data["lavalink"]["server"]["sources"]["youtube"] = False
await config.update_yaml(data)
state = _("enabled") if state else _("disabled")
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Managed node {source_variable_do_not_translate} source set to {state_variable_do_not_translate}."
).format(
source_variable_do_not_translate=inline(source),
state_variable_do_not_translate=state,
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="filter")
async def command_plmanaged_config_filter(self, context: PyLavContext, filter_name: str, state: bool):
"""Toggle the managed node filters"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
valid_filters = NODE_DEFAULT_SETTINGS["lavalink"]["server"]["filters"].copy()
if filter_name not in valid_filters:
return await context.send(
embed=await context.pylav.construct_embed(
description=_("Invalid source, {valid_list_variable_do_not_translate} are valid filters").format(
valid_list_variable_do_not_translate=humanize_list(
sorted(list(map(inline, valid_filters.keys())), key=str.lower)
)
),
messageable=context,
),
ephemeral=True,
)
data["lavalink"]["server"]["filters"][filter_name] = state
await config.update_yaml(data)
state = _("enabled") if state else _("disabled")
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Managed node {source_variable_do_not_translate} filter set to {state_variable_do_not_translate}."
).format(
source_variable_do_not_translate=inline(filter_name),
state_variable_do_not_translate=state,
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="server")
async def command_plmanaged_config_server(self, context: PyLavContext, setting: str, value: str):
"""Configure multiple settings for the managed node.
Run `[p]plmanaged settings server <setting> info` to show info about the settings and what they do.
**Setting names**:
`bufferDurationMs` : Integer i.e 400 (Default 400) - Set to 0 to disable JDA-NAS
`frameBufferDurationMs` : Integer i.e 1000 (Default 1000)
`trackStuckThresholdMs` : Integer i.e 1000 (Default 1000)
`youtubePlaylistLoadLimit` : Integer i.e 1000 (Default 1000)
`opusEncodingQuality` : Integer i.e 10 (Default 10)
`resamplingQuality` : String i.e LOW (Default HIGH)
`useSeekGhosting` : Boolean i.e True (Default True)
`playerUpdateInterval` : Integer i.e 30 (Default 30)
`youtubeSearchEnabled` : Boolean i.e True (Default True)
`soundcloudSearchEnabled` : Boolean i.e True (Default True)
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
user_input = setting.lower()
value = value.lower()
setting_case_map = {
"bufferdurationms": "bufferDurationMs",
"framebufferdurationms": "frameBufferDurationMs",
"trackstuckthresholdms": "trackStuckThresholdMs",
"youtubeplaylistloadlimit": "youtubePlaylistLoadLimit",
"opusencodingquality": "opusEncodingQuality",
"resamplingquality": "resamplingQuality",
"useseekghosting": "useSeekGhosting",
"playerupdateinterval": "playerUpdateInterval",
"youtubesearchenabled": "youtubeSearchEnabled",
"soundcloudsearchenabled": "soundcloudSearchEnabled",
}
setting = setting_case_map.get(user_input)
if setting is None:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"{Setting_variable_do_not_translate} is not a valid Setting; Options are:\n\n{setting_list_variable_do_not_translate}"
).format(
setting_variable_do_not_translate=user_input,
setting_list_variable_do_not_translate=humanize_list(list(setting_case_map.values())),
),
messageable=context,
),
ephemeral=True,
)
return
if value.lower() == "info":
setting_description_map = {
"bufferDurationMs": _(
"The duration of the NAS buffer in milliseconds. "
"Higher values fare better against longer GC pauses but will increase RAM usage. "
"Minimum of 40ms, lower values may introduce pauses and stutters. "
"Set to 0 to disable JDA-NAS. Accepted values: Range: 0 - 2,000"
),
"frameBufferDurationMs": _(
"How many milliseconds of audio to keep buffered, Higher values increase RAM usage. "
"Accepted values: Range: 1,000 - 10,000"
),
"trackStuckThresholdMs": _(
"The threshold in milliseconds for how long a track can be stuck. "
"A track is stuck if does not return any audio data. Accepted values: Range: 5,000 - 20,000"
),
"youtubePlaylistLoadLimit": _(
"Number of pages to return for a YouTube Playlist - Each page contains 100 songs. "
"Accepted values: Range: 5 - 100"
),
"opusEncodingQuality": _(
"Opus encoder quality. "
"Valid values range from 0 to 10, where 10 is the best quality but is the most expensive on the CPU."
),
"resamplingQuality": _(
"Quality of resampling operations. "
"Valid values are LOW, MEDIUM and HIGH, where HIGH uses the most CPU."
),
"useSeekGhosting": _(
"Seek ghosting is the effect where whilst a seek is in progress, "
"the audio buffer is read from until empty, or until seek is ready. "
"Accepted values for True: `True`, `t`, `1`, Accepted values for False: `False`, `f`, `0`"
),
"playerUpdateInterval": _(
"How frequently in seconds to send player updates to clients, "
"affects the current position accuracy. Accepted values: Range: 1 - 86400"
),
"youtubeSearchEnabled": _(
"Enable or disable YouTube searches within the node, "
"this will affect AppleMusic, Spotify and any functionality dependent on YouTube. "
"Accepted values for True: `True`, `t`, `1`, Accepted values for False: `False`, `f`, `0`"
),
"soundcloudSearchEnabled": _(
"Enable or disable SoundCloud searches within the node, "
"this will affect any functionality dependent on SoundCloud. "
"Accepted values for True: `True`, `t`, `1`, Accepted values for False: `False`, `f`, `0`"
),
}
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"{Setting_variable_do_not_translate} info.\n\n{info_variable_do_not_translate}"
).format(
setting_variable_do_not_translate=setting,
info_variable_do_not_translate=setting_description_map.get(setting),
),
messageable=context,
),
ephemeral=True,
)
return
setting_values_map = {
"bufferDurationMs": (40, 2000),
"frameBufferDurationMs": (1000, 10000),
"trackStuckThresholdMs": (5000, 20000),
"youtubePlaylistLoadLimit": (5, 100),
"opusEncodingQuality": (0, 10),
"resamplingQuality": ("low", "medium", "high"),
"useSeekGhosting": ("0", "1", "true", "false", "t", "f"),
"playerUpdateInterval": (1, 84600),
"youtubeSearchEnabled": ("0", "1", "true", "false", "t", "f"),
"soundcloudSearchEnabled": ("0", "1", "true", "false", "t", "f"),
}
possible_values = setting_values_map.get(setting)
if isinstance(possible_values[0], int):
value = int(value)
if value not in range(possible_values[0], possible_values[1] + 1):
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"{Setting_variable_do_not_translate} valid inputs are:\n\nRange between: {start_variable_do_not_translate} - {end_variable_do_not_translate}"
).format(
setting_variable_do_not_translate=setting,
start_variable_do_not_translate=possible_values[0],
end_variable_do_not_translate=possible_values[1],
),
messageable=context,
),
ephemeral=True,
)
return
elif value not in possible_values:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"{Setting_variable_do_not_translate} valid inputs are:\n\n{setting_list_variable_do_not_translate}"
).format(
setting_variable_do_not_translate=setting,
setting_list_variable_do_not_translate=humanize_list(possible_values),
),
messageable=context,
),
ephemeral=True,
)
return
elif possible_values == ("0", "1", "true", "false", "t", "f"):
value = value in ("0", "1", "true")
elif possible_values == ("low", "medium", "high"):
value = value.upper()
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
data["lavalink"]["server"][setting] = value
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("{Setting_variable_do_not_translate} set to {value_variable_do_not_translate}.").format(
setting_variable_do_not_translate=setting,
value_variable_do_not_translate=value,
),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="iprotation", aliases=["ir"])
async def command_plmanaged_config_iprotation(self, context: PyLavContext, *, reset: bool = False):
"""Configure Lavalink IP Rotation for rate limits.
Run `[p]plmanaged settings iprotation 1` to remove the ip rotation
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
if not reset:
await context.send(
embed=await context.pylav.construct_embed(
description=_(
"Click the button below to configure the IP rotation for your node.\n"
"More info at: {url_value_1_variable_do_not_translate} and {url_value_2_variable_do_not_translate}"
""
).format(
url_value_1_variable_do_not_translate="<https://github.com/lavalink-devs/Lavalink/blob/dev/ROUTEPLANNERS.md>",
url_value_2_variable_do_not_translate="<https://blog.arbjerg.dev/2020/3/tunnelbroker-with-lavalink>",
),
messageable=context,
),
view=ConfigureIPRotationView(self.bot, cog=self, prefix=context.clean_prefix),
ephemeral=True,
)
else:
# noinspection PyProtectedMember
config = self.pylav._node_config_manager.bundled_node_config()
data = await config.fetch_yaml()
data["lavalink"]["server"]["ratelimit"] = NODE_DEFAULT_SETTINGS["lavalink"]["server"]["ratelimit"]
await config.update_yaml(data)
await context.send(
embed=await context.pylav.construct_embed(
description=_("Removing the IP rotation from your node."),
messageable=context,
),
ephemeral=True,
)
@command_plmanaged_config.command(name="httpproxy", aliases=["hp"])
async def command_plmanaged_config_httpproxy(self, context: PyLavContext, *, reset: bool = False):
"""Configure a HTTP proxy for Lavalink
Run `[p]plmanaged settings httpproxy 1` to remove the proxy.
"""
if isinstance(context, discord.Interaction):
context = await self.bot.get_context(context)
if context.interaction and not context.interaction.response.is_done():
await context.defer(ephemeral=True)
if not reset: