1
1
import importlib
2
+ import json
2
3
import logging
3
4
import os
5
+ import random
4
6
import shutil
5
7
import site
6
8
import stat
7
9
import subprocess
8
10
import sys
9
- import json
10
- from pkg_resources import parse_version
11
11
from difflib import get_close_matches
12
- import random
13
12
14
13
import discord
15
14
from discord .ext import commands
16
15
from discord .utils import async_all
17
16
17
+ from pkg_resources import parse_version
18
+
18
19
from core import checks
19
20
from core .models import PermissionLevel
20
- from core .utils import info , error
21
21
from core .paginator import PaginatorSession
22
+ from core .utils import info , error
22
23
23
24
logger = logging .getLogger ('Modmail' )
24
25
@@ -146,37 +147,37 @@ async def plugin_add(self, ctx, *, plugin_name: str):
146
147
"""Add a plugin."""
147
148
148
149
if plugin_name in self .registry :
149
- info = self .registry [plugin_name ]
150
- plugin_name = info ['repository' ] + '/' + plugin_name
151
- required_version = info ['bot_version' ]
150
+ details = self .registry [plugin_name ]
151
+ plugin_name = details ['repository' ] + '/' + plugin_name
152
+ required_version = details ['bot_version' ]
152
153
153
154
if parse_version (self .bot .version ) < parse_version (required_version ):
154
- em = discord .Embed (
155
+ embed = discord .Embed (
155
156
description = f'Your bot\' s version is too low. This plugin requires version `{ required_version } `.' ,
156
157
color = self .bot .main_color
157
158
)
158
- return await ctx .send (embed = em )
159
+ return await ctx .send (embed = embed )
159
160
160
161
if plugin_name in self .bot .config .plugins :
161
- em = discord .Embed (
162
+ embed = discord .Embed (
162
163
description = 'This plugin is already installed.' ,
163
164
color = self .bot .main_color
164
165
)
165
- return await ctx .send (embed = em )
166
+ return await ctx .send (embed = embed )
166
167
167
168
if plugin_name in self .bot .cogs .keys ():
168
169
# another class with the same name
169
- em = discord .Embed (
170
+ embed = discord .Embed (
170
171
description = 'There\' s another cog installed with the same name.' ,
171
172
color = self .bot .main_color
172
173
)
173
- return await ctx .send (embed = em )
174
+ return await ctx .send (embed = embed )
174
175
175
- em = discord .Embed (
176
+ embed = discord .Embed (
176
177
description = 'Downloading this plugin...' ,
177
178
color = self .bot .main_color
178
179
)
179
- message = await ctx .send (embed = em )
180
+ message = await ctx .send (embed = embed )
180
181
181
182
async with ctx .typing ():
182
183
if len (plugin_name .split ('/' )) >= 3 :
@@ -185,50 +186,50 @@ async def plugin_add(self, ctx, *, plugin_name: str):
185
186
try :
186
187
await self .download_plugin_repo (* parsed_plugin [:- 1 ])
187
188
except DownloadError as exc :
188
- em = discord .Embed (
189
+ embed = discord .Embed (
189
190
description = f'Unable to fetch this plugin from Github: { exc } .' ,
190
191
color = self .bot .main_color
191
192
)
192
- return await ctx .send (embed = em )
193
+ return await ctx .send (embed = embed )
193
194
194
195
importlib .invalidate_caches ()
195
196
196
197
try :
197
198
await self .load_plugin (* parsed_plugin )
198
199
except DownloadError as exc :
199
- em = discord .Embed (
200
+ embed = discord .Embed (
200
201
description = f'Unable to load this plugin: { exc } .' ,
201
202
color = self .bot .main_color
202
203
)
203
- return await ctx .send (embed = em )
204
+ return await ctx .send (embed = embed )
204
205
205
206
# if it makes it here, it has passed all checks and should
206
207
# be entered into the config
207
208
208
209
self .bot .config .plugins .append (plugin_name )
209
210
await self .bot .config .update ()
210
211
211
- em = discord .Embed (
212
+ embed = discord .Embed (
212
213
description = 'The plugin is installed.\n '
213
214
'*Please note: any plugin that you install is of your OWN RISK*' ,
214
215
color = self .bot .main_color
215
216
)
216
- await message .edit (embed = em )
217
+ await message .edit (embed = embed )
217
218
else :
218
- em = discord .Embed (
219
+ embed = discord .Embed (
219
220
description = 'Invalid plugin name format: use username/repo/plugin.' ,
220
221
color = self .bot .main_color
221
222
)
222
- await message .edit (embed = em )
223
+ await message .edit (embed = embed )
223
224
224
225
@plugin .command (name = 'remove' , aliases = ['del' , 'delete' , 'rm' ])
225
226
@checks .has_permissions (PermissionLevel .OWNER )
226
227
async def plugin_remove (self , ctx , * , plugin_name : str ):
227
228
"""Remove a plugin."""
228
229
229
230
if plugin_name in self .registry :
230
- info = self .registry [plugin_name ]
231
- plugin_name = info ['repository' ] + '/' + plugin_name
231
+ details = self .registry [plugin_name ]
232
+ plugin_name = details ['repository' ] + '/' + plugin_name
232
233
233
234
if plugin_name in self .bot .config .plugins :
234
235
try :
@@ -241,6 +242,7 @@ async def plugin_remove(self, ctx, *, plugin_name: str):
241
242
self .bot .config .plugins .remove (plugin_name )
242
243
243
244
try :
245
+ # BUG: Local variables 'username' and 'repo' might be referenced before assignment
244
246
if not any (i .startswith (f'{ username } /{ repo } ' )
245
247
for i in self .bot .config .plugins ):
246
248
# if there are no more of such repos, delete the folder
@@ -258,33 +260,33 @@ def onerror(func, path, exc_info): # pylint: disable=W0613
258
260
259
261
await self .bot .config .update ()
260
262
261
- em = discord .Embed (
263
+ embed = discord .Embed (
262
264
description = 'The plugin is uninstalled and all its data is erased.' ,
263
265
color = self .bot .main_color
264
266
)
265
- await ctx .send (embed = em )
267
+ await ctx .send (embed = embed )
266
268
else :
267
- em = discord .Embed (
269
+ embed = discord .Embed (
268
270
description = 'That plugin is not installed.' ,
269
271
color = self .bot .main_color
270
272
)
271
- await ctx .send (embed = em )
273
+ await ctx .send (embed = embed )
272
274
273
275
@plugin .command (name = 'update' )
274
276
@checks .has_permissions (PermissionLevel .OWNER )
275
277
async def plugin_update (self , ctx , * , plugin_name : str ):
276
278
"""Update a plugin."""
277
279
278
280
if plugin_name in self .registry :
279
- info = self .registry [plugin_name ]
280
- plugin_name = info ['repository' ] + '/' + plugin_name
281
+ details = self .registry [plugin_name ]
282
+ plugin_name = details ['repository' ] + '/' + plugin_name
281
283
282
284
if plugin_name not in self .bot .config .plugins :
283
- em = discord .Embed (
285
+ embed = discord .Embed (
284
286
description = 'That plugin is not installed.' ,
285
287
color = self .bot .main_color
286
288
)
287
- return await ctx .send (embed = em )
289
+ return await ctx .send (embed = embed )
288
290
289
291
async with ctx .typing ():
290
292
username , repo , name = self .parse_plugin (plugin_name )
@@ -295,20 +297,20 @@ async def plugin_update(self, ctx, *, plugin_name: str):
295
297
except subprocess .CalledProcessError as exc :
296
298
err = exc .stderr .decode ('utf8' ).strip ()
297
299
298
- em = discord .Embed (
300
+ embed = discord .Embed (
299
301
description = f'An error occured while updating: { err } .' ,
300
302
color = self .bot .main_color
301
303
)
302
- await ctx .send (embed = em )
304
+ await ctx .send (embed = embed )
303
305
304
306
else :
305
307
output = cmd .stdout .decode ('utf8' ).strip ()
306
308
307
- em = discord .Embed (
309
+ embed = discord .Embed (
308
310
description = f'```\n { output } \n ```' ,
309
311
color = self .bot .main_color
310
312
)
311
- await ctx .send (embed = em )
313
+ await ctx .send (embed = embed )
312
314
313
315
if output != 'Already up to date.' :
314
316
# repo was updated locally, now perform the cog reload
@@ -331,17 +333,17 @@ async def plugin_enabled(self, ctx):
331
333
332
334
if self .bot .config .plugins :
333
335
msg = '```\n ' + '\n ' .join (self .bot .config .plugins ) + '\n ```'
334
- em = discord .Embed (
336
+ embed = discord .Embed (
335
337
description = msg ,
336
338
color = self .bot .main_color
337
339
)
338
- await ctx .send (embed = em )
340
+ await ctx .send (embed = embed )
339
341
else :
340
- em = discord .Embed (
342
+ embed = discord .Embed (
341
343
description = 'There are no plugins installed.' ,
342
344
color = self .bot .main_color
343
345
)
344
- await ctx .send (embed = em )
346
+ await ctx .send (embed = embed )
345
347
346
348
@plugin .group (invoke_without_command = True , name = 'registry' , aliases = ['list' ])
347
349
@checks .has_permissions (PermissionLevel .OWNER )
@@ -366,40 +368,40 @@ def find_index(name):
366
368
if plugin_name in self .registry :
367
369
index = find_index (plugin_name )
368
370
elif plugin_name is not None :
369
- em = discord .Embed (
371
+ embed = discord .Embed (
370
372
color = discord .Color .red (),
371
373
description = f'Could not find a plugin with name "{ plugin_name } " within the registry.'
372
374
)
373
375
374
376
matches = get_close_matches (plugin_name , self .registry .keys ())
375
377
376
378
if matches :
377
- em .add_field (name = 'Perhaps you meant' , value = '\n ' .join (f'`{ m } `' for m in matches ))
379
+ embed .add_field (name = 'Perhaps you meant' , value = '\n ' .join (f'`{ m } `' for m in matches ))
378
380
379
- return await ctx .send (embed = em )
381
+ return await ctx .send (embed = embed )
380
382
381
- for name , info in registry :
382
- repo = f"https://github.com/{ info ['repository' ]} "
383
+ for name , details in registry :
384
+ repo = f"https://github.com/{ details ['repository' ]} "
383
385
url = f"{ repo } /tree/master/{ name } "
384
386
385
- em = discord .Embed (
387
+ embed = discord .Embed (
386
388
color = self .bot .main_color ,
387
- description = info ['description' ],
389
+ description = details ['description' ],
388
390
url = repo ,
389
- title = info ['repository' ]
391
+ title = details ['repository' ]
390
392
)
391
393
392
- em .add_field (
394
+ embed .add_field (
393
395
name = 'Installation' ,
394
396
value = f'```{ self .bot .prefix } plugins add { name } ```' )
395
397
396
- em .set_author (name = info ['title' ], icon_url = info .get ('icon_url' ), url = url )
397
- if info .get ('thumbnail_url' ):
398
- em .set_thumbnail (url = info .get ('thumbnail_url' ))
399
- if info .get ('image_url' ):
400
- em .set_image (url = info .get ('image_url' ))
398
+ embed .set_author (name = details ['title' ], icon_url = details .get ('icon_url' ), url = url )
399
+ if details .get ('thumbnail_url' ):
400
+ embed .set_thumbnail (url = details .get ('thumbnail_url' ))
401
+ if details .get ('image_url' ):
402
+ embed .set_image (url = details .get ('image_url' ))
401
403
402
- embeds .append (em )
404
+ embeds .append (embed )
403
405
404
406
paginator = PaginatorSession (ctx , * embeds )
405
407
paginator .current = index
@@ -416,10 +418,10 @@ async def plugin_registry_compact(self, ctx):
416
418
417
419
pages = ['' ]
418
420
419
- for name , info in registry :
420
- repo = f"https://github.com/{ info ['repository' ]} "
421
+ for name , details in registry :
422
+ repo = f"https://github.com/{ details ['repository' ]} "
421
423
url = f"{ repo } /tree/master/{ name } "
422
- desc = info ['description' ].replace ('\n ' , '' )
424
+ desc = details ['description' ].replace ('\n ' , '' )
423
425
fmt = f"[`{ name } `]({ url } ) - { desc } "
424
426
length = len (fmt ) - len (url ) - 4
425
427
fmt = fmt [:75 + len (url )].strip () + '...' if length > 75 else fmt
@@ -431,12 +433,12 @@ async def plugin_registry_compact(self, ctx):
431
433
embeds = []
432
434
433
435
for page in pages :
434
- em = discord .Embed (
436
+ embed = discord .Embed (
435
437
color = self .bot .main_color ,
436
438
description = page ,
437
439
)
438
- em .set_author (name = 'Plugin Registry' , icon_url = self .bot .user .avatar_url )
439
- embeds .append (em )
440
+ embed .set_author (name = 'Plugin Registry' , icon_url = self .bot .user .avatar_url )
441
+ embeds .append (embed )
440
442
441
443
paginator = PaginatorSession (ctx , * embeds )
442
444
await paginator .run ()
0 commit comments