6
6
import stat
7
7
import subprocess
8
8
import sys
9
+ import json
10
+ from pkg_resources import parse_version
9
11
12
+ import discord
10
13
from discord .ext import commands
11
14
from discord .utils import async_all
12
15
13
16
from core import checks
14
17
from core .models import PermissionLevel
15
18
from core .utils import info , error
19
+ from core .paginator import PaginatorSession
16
20
17
21
logger = logging .getLogger ('Modmail' )
18
22
@@ -33,6 +37,8 @@ class Plugins(commands.Cog):
33
37
def __init__ (self , bot ):
34
38
self .bot = bot
35
39
self .bot .loop .create_task (self .download_initial_plugins ())
40
+ with open ('plugins/registry.json' ) as f :
41
+ self .registry = json .load (f )
36
42
37
43
@staticmethod
38
44
def _asubprocess_run (cmd ):
@@ -128,6 +134,12 @@ async def plugin(self, ctx):
128
134
@checks .has_permissions (PermissionLevel .OWNER )
129
135
async def plugin_add (self , ctx , * , plugin_name : str ):
130
136
"""Add a plugin."""
137
+ if plugin_name in self .registry :
138
+ info = self .registry [plugin_name ]
139
+ plugin_name = info ['repository' ] + '/' + plugin_name
140
+ required_version = info ['bot_version' ]
141
+ if parse_version (self .bot .version ) < parse_version (required_version ):
142
+ return await ctx .send (f"Bot version too low, plugin requires version `{ required_version } `" )
131
143
if plugin_name in self .bot .config .plugins :
132
144
return await ctx .send ('Plugin already installed.' )
133
145
if plugin_name in self .bot .cogs .keys ():
@@ -168,6 +180,9 @@ async def plugin_add(self, ctx, *, plugin_name: str):
168
180
@checks .has_permissions (PermissionLevel .OWNER )
169
181
async def plugin_remove (self , ctx , * , plugin_name : str ):
170
182
"""Remove a plugin."""
183
+ if plugin_name in self .registry :
184
+ info = self .registry [plugin_name ]
185
+ plugin_name = info ['repository' ] + '/' + plugin_name
171
186
if plugin_name in self .bot .config .plugins :
172
187
username , repo , name = self .parse_plugin (plugin_name )
173
188
self .bot .unload_extension (
@@ -203,6 +218,9 @@ def onerror(func, path, exc_info): # pylint: disable=W0613
203
218
@checks .has_permissions (PermissionLevel .OWNER )
204
219
async def plugin_update (self , ctx , * , plugin_name : str ):
205
220
"""Update a plugin."""
221
+ if plugin_name in self .registry :
222
+ info = self .registry [plugin_name ]
223
+ plugin_name = info ['repository' ] + '/' + plugin_name
206
224
if plugin_name not in self .bot .config .plugins :
207
225
return await ctx .send ('Plugin not installed.' )
208
226
@@ -232,16 +250,49 @@ async def plugin_update(self, ctx, *, plugin_name: str):
232
250
except DownloadError as exc :
233
251
await ctx .send (f'Unable to start plugin: `{ exc } `.' )
234
252
235
- @plugin .command (name = 'list' , aliases = [ 'show' , 'view' ] )
253
+ @plugin .command (name = 'enabled' )
236
254
@checks .has_permissions (PermissionLevel .OWNER )
237
- async def plugin_list (self , ctx ):
255
+ async def plugin_enabled (self , ctx ):
238
256
"""Shows a list of currently enabled plugins."""
239
257
if self .bot .config .plugins :
240
258
msg = '```\n ' + '\n ' .join (self .bot .config .plugins ) + '\n ```'
241
259
await ctx .send (msg )
242
260
else :
243
261
await ctx .send ('No plugins installed.' )
244
262
263
+ @plugin .command (name = 'registry' , aliases = ['list' ])
264
+ @checks .has_permissions (PermissionLevel .OWNER )
265
+ async def plugin_registry (self , ctx ):
266
+ """Shows a list of all approved plugins."""
267
+
268
+ embeds = []
269
+
270
+ for name , info in self .registry .items ():
271
+ repo = f"https://github.com/{ info ['repository' ]} "
272
+
273
+ em = discord .Embed (
274
+ color = self .bot .main_color ,
275
+ description = info ['description' ],
276
+ url = repo ,
277
+ title = info ['repository' ]
278
+ )
279
+
280
+ em .add_field (
281
+ name = 'Installation' ,
282
+ value = f'```{ self .bot .prefix } plugins add { name } ```' )
283
+
284
+ em .set_author (name = info ['title' ], icon_url = info .get ('icon_url' ))
285
+ if info .get ('thumbnail_url' ):
286
+ em .set_thumbnail (url = info .get ('thumbnail_url' ))
287
+
288
+ embeds .append (em )
289
+
290
+ paginator = PaginatorSession (ctx , * embeds )
291
+ await paginator .run ()
292
+
293
+
294
+
295
+
245
296
246
297
def setup (bot ):
247
298
bot .add_cog (Plugins (bot ))
0 commit comments