-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathplugins.py
733 lines (594 loc) · 27 KB
/
plugins.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
import asyncio
import io
import json
import os
import shutil
import sys
import typing
import zipfile
from importlib import invalidate_caches
from difflib import get_close_matches
from pathlib import Path, PurePath
from re import match
from site import USER_SITE
from subprocess import PIPE
import discord
from discord.ext import commands
from pkg_resources import parse_version
from core import checks
from core.models import PermissionLevel, getLogger
from core.paginator import EmbedPaginatorSession
from core.utils import truncate, trigger_typing
logger = getLogger(__name__)
class InvalidPluginError(commands.BadArgument):
pass
class Plugin:
def __init__(self, user, repo=None, name=None, branch=None):
if repo is None:
self.user = "@local"
self.repo = "@local"
self.name = user
self.local = True
self.branch = "@local"
self.url = f"@local/{user}"
self.link = f"@local/{user}"
else:
self.user = user
self.repo = repo
self.name = name
self.local = False
self.branch = branch if branch is not None else "master"
self.url = f"https://github.com/{user}/{repo}/archive/{self.branch}.zip"
self.link = f"https://github.com/{user}/{repo}/tree/{self.branch}/{name}"
@property
def path(self):
if self.local:
return PurePath("plugins") / "@local" / self.name
return PurePath("plugins") / self.user / self.repo / f"{self.name}-{self.branch}"
@property
def abs_path(self):
return Path(__file__).absolute().parent.parent / self.path
@property
def cache_path(self):
if self.local:
raise ValueError("No cache path for local plugins!")
return (
Path(__file__).absolute().parent.parent
/ "temp"
/ "plugins-cache"
/ f"{self.user}-{self.repo}-{self.branch}.zip"
)
@property
def ext_string(self):
if self.local:
return f"plugins.@local.{self.name}.{self.name}"
return f"plugins.{self.user}.{self.repo}.{self.name}-{self.branch}.{self.name}"
def __str__(self):
if self.local:
return f"@local/{self.name}"
return f"{self.user}/{self.repo}/{self.name}@{self.branch}"
def __lt__(self, other):
return self.name.lower() < other.name.lower()
@classmethod
def from_string(cls, s, strict=False):
m = match(r"^@?local/(.+)$", s)
if m is None:
if not strict:
m = match(r"^(.+?)/(.+?)/(.+?)(?:@(.+?))?$", s)
else:
m = match(r"^(.+?)/(.+?)/(.+?)@(.+?)$", s)
if m is not None:
return Plugin(*m.groups())
raise InvalidPluginError("Cannot decipher %s.", s) # pylint: disable=raising-format-tuple
def __hash__(self):
return hash((self.user, self.repo, self.name, self.branch))
def __repr__(self):
return f"<Plugins: {self.__str__()}>"
def __eq__(self, other):
return isinstance(other, Plugin) and self.__str__() == other.__str__()
class Plugins(commands.Cog):
"""
Plugins expand Modmail functionality by allowing third-party addons.
These addons could have a range of features from moderation to simply
making your life as a moderator easier!
Learn how to create a plugin yourself here:
https://github.com/kyb3r/modmail/wiki/Plugins
"""
def __init__(self, bot):
self.bot = bot
self.registry = {}
self.loaded_plugins = set()
self._ready_event = asyncio.Event()
self.bot.loop.create_task(self.populate_registry())
if self.bot.config.get("enable_plugins"):
self.bot.loop.create_task(self.initial_load_plugins())
else:
logger.info("Plugins not loaded since ENABLE_PLUGINS=false.")
async def populate_registry(self):
url = "https://raw.githubusercontent.com/kyb3r/modmail/master/plugins/registry.json"
async with self.bot.session.get(url) as resp:
self.registry = json.loads(await resp.text())
async def initial_load_plugins(self):
await self.bot.wait_for_connected()
for plugin_name in list(self.bot.config["plugins"]):
try:
plugin = Plugin.from_string(plugin_name, strict=True)
except InvalidPluginError:
self.bot.config["plugins"].remove(plugin_name)
try:
# For backwards compat
plugin = Plugin.from_string(plugin_name)
except InvalidPluginError:
logger.error("Failed to parse plugin name: %s.", plugin_name, exc_info=True)
continue
logger.info("Migrated legacy plugin name: %s, now %s.", plugin_name, str(plugin))
self.bot.config["plugins"].append(str(plugin))
try:
await self.download_plugin(plugin)
await self.load_plugin(plugin)
except Exception:
self.bot.config["plugins"].remove(plugin_name)
logger.error(
"Error when loading plugin %s. Plugin removed from config.",
plugin,
exc_info=True,
)
continue
logger.debug("Finished loading all plugins.")
self.bot.dispatch("plugins_ready")
self._ready_event.set()
await self.bot.config.update()
async def download_plugin(self, plugin, force=False):
if plugin.abs_path.exists() and (not force or plugin.local):
return
if plugin.local:
raise InvalidPluginError(f"Local plugin {plugin} not found!")
plugin.abs_path.mkdir(parents=True, exist_ok=True)
if plugin.cache_path.exists() and not force:
plugin_io = plugin.cache_path.open("rb")
logger.debug("Loading cached %s.", plugin.cache_path)
else:
headers = {}
github_token = self.bot.config["github_token"]
if github_token is not None:
headers["Authorization"] = f"token {github_token}"
async with self.bot.session.get(plugin.url, headers=headers) as resp:
logger.debug("Downloading %s.", plugin.url)
raw = await resp.read()
try:
raw = await resp.text()
except UnicodeDecodeError:
pass
else:
if raw == "Not Found":
raise InvalidPluginError("Plugin not found")
else:
raise InvalidPluginError("Invalid download received, non-bytes object")
plugin_io = io.BytesIO(raw)
if not plugin.cache_path.parent.exists():
plugin.cache_path.parent.mkdir(parents=True)
with plugin.cache_path.open("wb") as f:
f.write(raw)
with zipfile.ZipFile(plugin_io) as zipf:
for info in zipf.infolist():
path = PurePath(info.filename)
if len(path.parts) >= 3 and path.parts[1] == plugin.name:
plugin_path = plugin.abs_path / Path(*path.parts[2:])
if info.is_dir():
plugin_path.mkdir(parents=True, exist_ok=True)
else:
plugin_path.parent.mkdir(parents=True, exist_ok=True)
with zipf.open(info) as src, plugin_path.open("wb") as dst:
shutil.copyfileobj(src, dst)
plugin_io.close()
async def load_plugin(self, plugin):
if not (plugin.abs_path / f"{plugin.name}.py").exists():
raise InvalidPluginError(f"{plugin.name}.py not found.")
req_txt = plugin.abs_path / "requirements.txt"
if req_txt.exists():
# Install PIP requirements
venv = hasattr(sys, "real_prefix") or hasattr(sys, "base_prefix") # in a virtual env
user_install = " --user" if not venv else ""
proc = await asyncio.create_subprocess_shell(
f'"{sys.executable}" -m pip install --upgrade{user_install} -r {req_txt} -q -q',
stderr=PIPE,
stdout=PIPE,
)
logger.debug("Downloading requirements for %s.", plugin.ext_string)
stdout, stderr = await proc.communicate()
if stdout:
logger.debug("[stdout]\n%s.", stdout.decode())
if stderr:
logger.debug("[stderr]\n%s.", stderr.decode())
logger.error("Failed to download requirements for %s.", plugin.ext_string, exc_info=True)
raise InvalidPluginError(f"Unable to download requirements: ```\n{stderr.decode()}\n```")
if os.path.exists(USER_SITE):
sys.path.insert(0, USER_SITE)
try:
self.bot.load_extension(plugin.ext_string)
logger.info("Loaded plugin: %s", plugin.ext_string.split(".")[-1])
self.loaded_plugins.add(plugin)
except commands.ExtensionError as exc:
logger.error("Plugin load failure: %s", plugin.ext_string, exc_info=True)
raise InvalidPluginError("Cannot load extension, plugin invalid.") from exc
async def parse_user_input(self, ctx, plugin_name, check_version=False):
if not self.bot.config["enable_plugins"]:
embed = discord.Embed(
description="Plugins are disabled, enable them by setting `ENABLE_PLUGINS=true`",
color=self.bot.main_color,
)
await ctx.send(embed=embed)
return
if not self._ready_event.is_set():
embed = discord.Embed(
description="Plugins are still loading, please try again later.",
color=self.bot.main_color,
)
await ctx.send(embed=embed)
return
if plugin_name in self.registry:
details = self.registry[plugin_name]
user, repo = details["repository"].split("/", maxsplit=1)
branch = details.get("branch")
if check_version:
required_version = details.get("bot_version", False)
if required_version and self.bot.version < parse_version(required_version):
embed = discord.Embed(
description="Your bot's version is too low. "
f"This plugin requires version `{required_version}`.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)
return
plugin = Plugin(user, repo, plugin_name, branch)
else:
try:
plugin = Plugin.from_string(plugin_name)
except InvalidPluginError:
embed = discord.Embed(
description="Invalid plugin name, double check the plugin name "
"or use one of the following formats: "
"username/repo/plugin-name, username/repo/plugin-name@branch, local/plugin-name.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)
return
return plugin
@commands.group(aliases=["plugin"], invoke_without_command=True)
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins(self, ctx):
"""
Manage plugins for Modmail.
"""
await ctx.send_help(ctx.command)
@plugins.command(name="add", aliases=["install", "load"])
@checks.has_permissions(PermissionLevel.OWNER)
@trigger_typing
async def plugins_add(self, ctx, *, plugin_name: str):
"""
Install a new plugin for the bot.
`plugin_name` can be the name of the plugin found in `{prefix}plugin registry`,
or a direct reference to a GitHub hosted plugin (in the format `user/repo/name[@branch]`)
or `local/name` for local plugins.
"""
plugin = await self.parse_user_input(ctx, plugin_name, check_version=True)
if plugin is None:
return
if str(plugin) in self.bot.config["plugins"]:
embed = discord.Embed(description="This plugin is already installed.", color=self.bot.error_color)
return await ctx.send(embed=embed)
if plugin.name in self.bot.cogs:
# another class with the same name
embed = discord.Embed(
description="Cannot install this plugin (dupe cog name).",
color=self.bot.error_color,
)
return await ctx.send(embed=embed)
if plugin.local:
embed = discord.Embed(
description=f"Starting to load local plugin from {plugin.link}...",
color=self.bot.main_color,
)
else:
embed = discord.Embed(
description=f"Starting to download plugin from {plugin.link}...",
color=self.bot.main_color,
)
msg = await ctx.send(embed=embed)
try:
await self.download_plugin(plugin, force=True)
except Exception as e:
logger.warning("Unable to download plugin %s.", plugin, exc_info=True)
embed = discord.Embed(
description=f"Failed to download plugin, check logs for error.\n{type(e)}: {e}",
color=self.bot.error_color,
)
return await msg.edit(embed=embed)
self.bot.config["plugins"].append(str(plugin))
await self.bot.config.update()
if self.bot.config.get("enable_plugins"):
invalidate_caches()
try:
await self.load_plugin(plugin)
except Exception as e:
logger.warning("Unable to load plugin %s.", plugin, exc_info=True)
embed = discord.Embed(
description=f"Failed to download plugin, check logs for error.\n{type(e)}: {e}",
color=self.bot.error_color,
)
else:
embed = discord.Embed(
description="Successfully installed plugin.\n"
"*Friendly reminder, plugins have absolute control over your bot. "
"Please only install plugins from developers you trust.*",
color=self.bot.main_color,
)
else:
embed = discord.Embed(
description="Successfully installed plugin.\n"
"*Friendly reminder, plugins have absolute control over your bot. "
"Please only install plugins from developers you trust.*\n\n"
"This plugin is currently not enabled due to `ENABLE_PLUGINS=false`, "
"to re-enable plugins, remove or change `ENABLE_PLUGINS=true` and restart your bot.",
color=self.bot.main_color,
)
return await msg.edit(embed=embed)
@plugins.command(name="remove", aliases=["del", "delete"])
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_remove(self, ctx, *, plugin_name: str):
"""
Remove an installed plugin of the bot.
`plugin_name` can be the name of the plugin found in `{prefix}plugin registry`, or a direct reference
to a GitHub hosted plugin (in the format `user/repo/name[@branch]`) or `local/name` for local plugins.
"""
plugin = await self.parse_user_input(ctx, plugin_name)
if plugin is None:
return
if str(plugin) not in self.bot.config["plugins"]:
embed = discord.Embed(description="Plugin is not installed.", color=self.bot.error_color)
return await ctx.send(embed=embed)
if self.bot.config.get("enable_plugins"):
try:
self.bot.unload_extension(plugin.ext_string)
self.loaded_plugins.remove(plugin)
except (commands.ExtensionNotLoaded, KeyError):
logger.warning("Plugin was never loaded.")
self.bot.config["plugins"].remove(str(plugin))
await self.bot.config.update()
if not plugin.local:
shutil.rmtree(
plugin.abs_path,
onerror=lambda *args: logger.warning(
"Failed to remove plugin files %s: %s", plugin, str(args[2])
),
)
try:
plugin.abs_path.parent.rmdir()
plugin.abs_path.parent.parent.rmdir()
except OSError:
pass # dir not empty
embed = discord.Embed(
description="The plugin is successfully uninstalled.", color=self.bot.main_color
)
await ctx.send(embed=embed)
async def update_plugin(self, ctx, plugin_name):
logger.debug("Updating %s.", plugin_name)
plugin = await self.parse_user_input(ctx, plugin_name, check_version=True)
if plugin is None:
return
if str(plugin) not in self.bot.config["plugins"]:
embed = discord.Embed(description="Plugin is not installed.", color=self.bot.error_color)
return await ctx.send(embed=embed)
async with ctx.typing():
embed = discord.Embed(
description=f"Successfully updated {plugin.name}.", color=self.bot.main_color
)
await self.download_plugin(plugin, force=True)
if self.bot.config.get("enable_plugins"):
try:
self.bot.unload_extension(plugin.ext_string)
except commands.ExtensionError:
logger.warning("Plugin unload fail.", exc_info=True)
try:
await self.load_plugin(plugin)
except Exception:
embed = discord.Embed(
description=f"Failed to update {plugin.name}. This plugin will now be removed from your bot.",
color=self.bot.error_color,
)
self.bot.config["plugins"].remove(plugin_name)
logger.debug("Failed to update %s. Removed plugin from config.", plugin_name)
else:
logger.debug("Updated %s.", plugin_name)
else:
logger.debug("Updated %s.", plugin_name)
return await ctx.send(embed=embed)
@plugins.command(name="update")
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_update(self, ctx, *, plugin_name: str = None):
"""
Update a plugin for the bot.
`plugin_name` can be the name of the plugin found in `{prefix}plugin registry`, or a direct reference
to a GitHub hosted plugin (in the format `user/repo/name[@branch]`) or `local/name` for local plugins.
To update all plugins, do `{prefix}plugins update`.
"""
if plugin_name is None:
# pylint: disable=redefined-argument-from-local
for plugin_name in list(self.bot.config["plugins"]):
await self.update_plugin(ctx, plugin_name)
else:
await self.update_plugin(ctx, plugin_name)
@plugins.command(name="reset")
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_reset(self, ctx):
"""
Reset all plugins for the bot.
Deletes all cache and plugins from config and unloads from the bot.
"""
logger.warning("Purging plugins.")
for ext in list(self.bot.extensions):
if not ext.startswith("plugins."):
continue
try:
logger.error("Unloading plugin: %s.", ext)
self.bot.unload_extension(ext)
except Exception:
logger.error("Failed to unload plugin: %s.", ext)
self.bot.config["plugins"].clear()
cache_path = Path(__file__).absolute().parent.parent / "temp" / "plugins-cache"
if cache_path.exists():
logger.warning("Removing cache path.")
shutil.rmtree(cache_path)
for entry in os.scandir(Path(__file__).absolute().parent.parent / "plugins"):
if entry.is_dir() and entry.name != "@local":
shutil.rmtree(entry.path)
logger.warning("Removing %s.", entry.name)
embed = discord.Embed(
description="Successfully purged all plugins from the bot.", color=self.bot.main_color
)
return await ctx.send(embed=embed)
@plugins.command(name="loaded", aliases=["enabled", "installed"])
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_loaded(self, ctx):
"""
Show a list of currently loaded plugins.
"""
if not self.bot.config.get("enable_plugins"):
embed = discord.Embed(
description="No plugins are loaded due to `ENABLE_PLUGINS=false`, "
"to re-enable plugins, remove or set `ENABLE_PLUGINS=true` and restart your bot.",
color=self.bot.error_color,
)
return await ctx.send(embed=embed)
if not self._ready_event.is_set():
embed = discord.Embed(
description="Plugins are still loading, please try again later.",
color=self.bot.main_color,
)
return await ctx.send(embed=embed)
if not self.loaded_plugins:
embed = discord.Embed(
description="There are no plugins currently loaded.", color=self.bot.error_color
)
return await ctx.send(embed=embed)
loaded_plugins = map(str, sorted(self.loaded_plugins))
pages = ["```\n"]
for plugin in loaded_plugins:
msg = str(plugin) + "\n"
if len(msg) + len(pages[-1]) + 3 <= 2048:
pages[-1] += msg
else:
pages[-1] += "```"
pages.append(f"```\n{msg}")
if pages[-1][-3:] != "```":
pages[-1] += "```"
embeds = []
for page in pages:
embed = discord.Embed(title="Loaded plugins:", description=page, color=self.bot.main_color)
embeds.append(embed)
paginator = EmbedPaginatorSession(ctx, *embeds)
await paginator.run()
@plugins.group(invoke_without_command=True, name="registry", aliases=["list", "info"])
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_registry(self, ctx, *, plugin_name: typing.Union[int, str] = None):
"""
Shows a list of all approved plugins.
Usage:
`{prefix}plugin registry` Details about all plugins.
`{prefix}plugin registry plugin-name` Details about the indicated plugin.
`{prefix}plugin registry page-number` Jump to a page in the registry.
"""
await self.populate_registry()
embeds = []
registry = sorted(self.registry.items(), key=lambda elem: elem[0])
if isinstance(plugin_name, int):
index = plugin_name - 1
if index < 0:
index = 0
if index >= len(registry):
index = len(registry) - 1
else:
index = next((i for i, (n, _) in enumerate(registry) if plugin_name == n), 0)
if not index and plugin_name is not None:
embed = discord.Embed(
color=self.bot.error_color,
description=f'Could not find a plugin with name "{plugin_name}" within the registry.',
)
matches = get_close_matches(plugin_name, self.registry.keys())
if matches:
embed.add_field(name="Perhaps you meant:", value="\n".join(f"`{m}`" for m in matches))
return await ctx.send(embed=embed)
for name, details in registry:
details = self.registry[name]
user, repo = details["repository"].split("/", maxsplit=1)
branch = details.get("branch")
plugin = Plugin(user, repo, name, branch)
embed = discord.Embed(
color=self.bot.main_color,
description=details["description"],
url=plugin.link,
title=details["repository"],
)
embed.add_field(name="Installation", value=f"```{self.bot.prefix}plugins add {name}```")
embed.set_author(name=details["title"], icon_url=details.get("icon_url"), url=plugin.link)
if details.get("thumbnail_url"):
embed.set_thumbnail(url=details.get("thumbnail_url"))
if details.get("image_url"):
embed.set_image(url=details.get("image_url"))
if plugin in self.loaded_plugins:
embed.set_footer(text="This plugin is currently loaded.")
else:
required_version = details.get("bot_version", False)
if required_version and self.bot.version < parse_version(required_version):
embed.set_footer(
text="Your bot is unable to install this plugin, "
f"minimum required version is v{required_version}."
)
else:
embed.set_footer(text="Your bot is able to install this plugin.")
embeds.append(embed)
paginator = EmbedPaginatorSession(ctx, *embeds)
paginator.current = index
await paginator.run()
@plugins_registry.command(name="compact", aliases=["slim"])
@checks.has_permissions(PermissionLevel.OWNER)
async def plugins_registry_compact(self, ctx):
"""
Shows a compact view of all plugins within the registry.
"""
await self.populate_registry()
registry = sorted(self.registry.items(), key=lambda elem: elem[0])
pages = [""]
for plugin_name, details in registry:
details = self.registry[plugin_name]
user, repo = details["repository"].split("/", maxsplit=1)
branch = details.get("branch")
plugin = Plugin(user, repo, plugin_name, branch)
desc = discord.utils.escape_markdown(details["description"].replace("\n", ""))
name = f"[`{plugin.name}`]({plugin.link})"
fmt = f"{name} - {desc}"
if plugin_name in self.loaded_plugins:
limit = 75 - len(plugin_name) - 4 - 8 + len(name)
if limit < 0:
fmt = plugin.name
limit = 75
fmt = truncate(fmt, limit) + "[loaded]\n"
else:
limit = 75 - len(plugin_name) - 4 + len(name)
if limit < 0:
fmt = plugin.name
limit = 75
fmt = truncate(fmt, limit) + "\n"
if len(fmt) + len(pages[-1]) <= 2048:
pages[-1] += fmt
else:
pages.append(fmt)
embeds = []
for page in pages:
embed = discord.Embed(color=self.bot.main_color, description=page)
embed.set_author(name="Plugin Registry", icon_url=self.bot.user.avatar_url)
embeds.append(embed)
paginator = EmbedPaginatorSession(ctx, *embeds)
await paginator.run()
def setup(bot):
bot.add_cog(Plugins(bot))