11import importlib
2+ import json
23import logging
34import os
5+ import random
46import shutil
57import site
68import stat
79import subprocess
810import sys
9- import json
10- from pkg_resources import parse_version
1111from difflib import get_close_matches
12- import random
1312
1413import discord
1514from discord .ext import commands
1615from discord .utils import async_all
1716
17+ from pkg_resources import parse_version
18+
1819from core import checks
1920from core .models import PermissionLevel
20- from core .utils import info , error
2121from core .paginator import PaginatorSession
22+ from core .utils import info , error
2223
2324logger = logging .getLogger ('Modmail' )
2425
@@ -146,37 +147,37 @@ async def plugin_add(self, ctx, *, plugin_name: str):
146147 """Add a plugin."""
147148
148149 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' ]
152153
153154 if parse_version (self .bot .version ) < parse_version (required_version ):
154- em = discord .Embed (
155+ embed = discord .Embed (
155156 description = f'Your bot\' s version is too low. This plugin requires version `{ required_version } `.' ,
156157 color = self .bot .main_color
157158 )
158- return await ctx .send (embed = em )
159+ return await ctx .send (embed = embed )
159160
160161 if plugin_name in self .bot .config .plugins :
161- em = discord .Embed (
162+ embed = discord .Embed (
162163 description = 'This plugin is already installed.' ,
163164 color = self .bot .main_color
164165 )
165- return await ctx .send (embed = em )
166+ return await ctx .send (embed = embed )
166167
167168 if plugin_name in self .bot .cogs .keys ():
168169 # another class with the same name
169- em = discord .Embed (
170+ embed = discord .Embed (
170171 description = 'There\' s another cog installed with the same name.' ,
171172 color = self .bot .main_color
172173 )
173- return await ctx .send (embed = em )
174+ return await ctx .send (embed = embed )
174175
175- em = discord .Embed (
176+ embed = discord .Embed (
176177 description = 'Downloading this plugin...' ,
177178 color = self .bot .main_color
178179 )
179- message = await ctx .send (embed = em )
180+ message = await ctx .send (embed = embed )
180181
181182 async with ctx .typing ():
182183 if len (plugin_name .split ('/' )) >= 3 :
@@ -185,50 +186,50 @@ async def plugin_add(self, ctx, *, plugin_name: str):
185186 try :
186187 await self .download_plugin_repo (* parsed_plugin [:- 1 ])
187188 except DownloadError as exc :
188- em = discord .Embed (
189+ embed = discord .Embed (
189190 description = f'Unable to fetch this plugin from Github: { exc } .' ,
190191 color = self .bot .main_color
191192 )
192- return await ctx .send (embed = em )
193+ return await ctx .send (embed = embed )
193194
194195 importlib .invalidate_caches ()
195196
196197 try :
197198 await self .load_plugin (* parsed_plugin )
198199 except DownloadError as exc :
199- em = discord .Embed (
200+ embed = discord .Embed (
200201 description = f'Unable to load this plugin: { exc } .' ,
201202 color = self .bot .main_color
202203 )
203- return await ctx .send (embed = em )
204+ return await ctx .send (embed = embed )
204205
205206 # if it makes it here, it has passed all checks and should
206207 # be entered into the config
207208
208209 self .bot .config .plugins .append (plugin_name )
209210 await self .bot .config .update ()
210211
211- em = discord .Embed (
212+ embed = discord .Embed (
212213 description = 'The plugin is installed.\n '
213214 '*Please note: any plugin that you install is of your OWN RISK*' ,
214215 color = self .bot .main_color
215216 )
216- await message .edit (embed = em )
217+ await message .edit (embed = embed )
217218 else :
218- em = discord .Embed (
219+ embed = discord .Embed (
219220 description = 'Invalid plugin name format: use username/repo/plugin.' ,
220221 color = self .bot .main_color
221222 )
222- await message .edit (embed = em )
223+ await message .edit (embed = embed )
223224
224225 @plugin .command (name = 'remove' , aliases = ['del' , 'delete' , 'rm' ])
225226 @checks .has_permissions (PermissionLevel .OWNER )
226227 async def plugin_remove (self , ctx , * , plugin_name : str ):
227228 """Remove a plugin."""
228229
229230 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
232233
233234 if plugin_name in self .bot .config .plugins :
234235 try :
@@ -241,6 +242,7 @@ async def plugin_remove(self, ctx, *, plugin_name: str):
241242 self .bot .config .plugins .remove (plugin_name )
242243
243244 try :
245+ # BUG: Local variables 'username' and 'repo' might be referenced before assignment
244246 if not any (i .startswith (f'{ username } /{ repo } ' )
245247 for i in self .bot .config .plugins ):
246248 # if there are no more of such repos, delete the folder
@@ -258,33 +260,33 @@ def onerror(func, path, exc_info): # pylint: disable=W0613
258260
259261 await self .bot .config .update ()
260262
261- em = discord .Embed (
263+ embed = discord .Embed (
262264 description = 'The plugin is uninstalled and all its data is erased.' ,
263265 color = self .bot .main_color
264266 )
265- await ctx .send (embed = em )
267+ await ctx .send (embed = embed )
266268 else :
267- em = discord .Embed (
269+ embed = discord .Embed (
268270 description = 'That plugin is not installed.' ,
269271 color = self .bot .main_color
270272 )
271- await ctx .send (embed = em )
273+ await ctx .send (embed = embed )
272274
273275 @plugin .command (name = 'update' )
274276 @checks .has_permissions (PermissionLevel .OWNER )
275277 async def plugin_update (self , ctx , * , plugin_name : str ):
276278 """Update a plugin."""
277279
278280 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
281283
282284 if plugin_name not in self .bot .config .plugins :
283- em = discord .Embed (
285+ embed = discord .Embed (
284286 description = 'That plugin is not installed.' ,
285287 color = self .bot .main_color
286288 )
287- return await ctx .send (embed = em )
289+ return await ctx .send (embed = embed )
288290
289291 async with ctx .typing ():
290292 username , repo , name = self .parse_plugin (plugin_name )
@@ -295,20 +297,20 @@ async def plugin_update(self, ctx, *, plugin_name: str):
295297 except subprocess .CalledProcessError as exc :
296298 err = exc .stderr .decode ('utf8' ).strip ()
297299
298- em = discord .Embed (
300+ embed = discord .Embed (
299301 description = f'An error occured while updating: { err } .' ,
300302 color = self .bot .main_color
301303 )
302- await ctx .send (embed = em )
304+ await ctx .send (embed = embed )
303305
304306 else :
305307 output = cmd .stdout .decode ('utf8' ).strip ()
306308
307- em = discord .Embed (
309+ embed = discord .Embed (
308310 description = f'```\n { output } \n ```' ,
309311 color = self .bot .main_color
310312 )
311- await ctx .send (embed = em )
313+ await ctx .send (embed = embed )
312314
313315 if output != 'Already up to date.' :
314316 # repo was updated locally, now perform the cog reload
@@ -331,17 +333,17 @@ async def plugin_enabled(self, ctx):
331333
332334 if self .bot .config .plugins :
333335 msg = '```\n ' + '\n ' .join (self .bot .config .plugins ) + '\n ```'
334- em = discord .Embed (
336+ embed = discord .Embed (
335337 description = msg ,
336338 color = self .bot .main_color
337339 )
338- await ctx .send (embed = em )
340+ await ctx .send (embed = embed )
339341 else :
340- em = discord .Embed (
342+ embed = discord .Embed (
341343 description = 'There are no plugins installed.' ,
342344 color = self .bot .main_color
343345 )
344- await ctx .send (embed = em )
346+ await ctx .send (embed = embed )
345347
346348 @plugin .group (invoke_without_command = True , name = 'registry' , aliases = ['list' ])
347349 @checks .has_permissions (PermissionLevel .OWNER )
@@ -366,40 +368,40 @@ def find_index(name):
366368 if plugin_name in self .registry :
367369 index = find_index (plugin_name )
368370 elif plugin_name is not None :
369- em = discord .Embed (
371+ embed = discord .Embed (
370372 color = discord .Color .red (),
371373 description = f'Could not find a plugin with name "{ plugin_name } " within the registry.'
372374 )
373375
374376 matches = get_close_matches (plugin_name , self .registry .keys ())
375377
376378 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 ))
378380
379- return await ctx .send (embed = em )
381+ return await ctx .send (embed = embed )
380382
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' ]} "
383385 url = f"{ repo } /tree/master/{ name } "
384386
385- em = discord .Embed (
387+ embed = discord .Embed (
386388 color = self .bot .main_color ,
387- description = info ['description' ],
389+ description = details ['description' ],
388390 url = repo ,
389- title = info ['repository' ]
391+ title = details ['repository' ]
390392 )
391393
392- em .add_field (
394+ embed .add_field (
393395 name = 'Installation' ,
394396 value = f'```{ self .bot .prefix } plugins add { name } ```' )
395397
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' ))
401403
402- embeds .append (em )
404+ embeds .append (embed )
403405
404406 paginator = PaginatorSession (ctx , * embeds )
405407 paginator .current = index
@@ -416,10 +418,10 @@ async def plugin_registry_compact(self, ctx):
416418
417419 pages = ['' ]
418420
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' ]} "
421423 url = f"{ repo } /tree/master/{ name } "
422- desc = info ['description' ].replace ('\n ' , '' )
424+ desc = details ['description' ].replace ('\n ' , '' )
423425 fmt = f"[`{ name } `]({ url } ) - { desc } "
424426 length = len (fmt ) - len (url ) - 4
425427 fmt = fmt [:75 + len (url )].strip () + '...' if length > 75 else fmt
@@ -431,12 +433,12 @@ async def plugin_registry_compact(self, ctx):
431433 embeds = []
432434
433435 for page in pages :
434- em = discord .Embed (
436+ embed = discord .Embed (
435437 color = self .bot .main_color ,
436438 description = page ,
437439 )
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 )
440442
441443 paginator = PaginatorSession (ctx , * embeds )
442444 await paginator .run ()
0 commit comments