import asyncio

from discord.ext import commands

from .utils import checks


class CogManagement:
    def __init__(self, bot):
        self.bot = bot

    async def reload_ext_helper(self, ext):
        try:
            self.bot.unload_extension('cogs.{}'.format(ext))
            self.bot.load_extension('cogs.{}'.format(ext))
        except:
            await self.bot.say("Couldn't reload cog {}.".format(ext))

    @commands.command(name='cogs', hidden=True)
    @checks.is_owner()
    async def list_exts(self):
        exts = sorted(self.bot.extensions.keys())
        message = '\n'.join(['```', 'Loaded extensions:', *exts, '```'])
        await self.bot.say(message)

    @commands.group(name='reload', hidden=True, invoke_without_command=True)
    @checks.is_owner()
    async def reload_ext(self, ext):
        mod = 'cogs.' + ext
        if mod not in self.bot.extensions:
            await self.bot.say('Cog {} is not loaded.'.format(ext))
            return
        try:
            await self.reload_ext_helper(ext)
            await self.bot.say('Reloaded cog {}.'.format(ext))
        except Exception as e:
            await self.bot.say("Couldn't reload cog {}.".format(ext))
            print(e)

    @reload_ext.command(name='all')
    @checks.is_owner()
    async def reload_all_exts(self):
        exts = [e.split('.')[1] for e in self.bot.extensions.keys()]
        await asyncio.gather(*[self.reload_ext_helper(ext) for ext in exts])
        await self.bot.say('Reloaded all cogs.')

    @commands.command(name='load', hidden=True)
    @checks.is_owner()
    async def load_ext(self, ext):
        mod = 'cogs.' + ext
        if mod in self.bot.extensions:
            await self.bot.say('Cog {} is already loaded.'.format(ext))
            return
        try:
            self.bot.load_extension(mod)
            await self.bot.say('Loaded cog {}.'.format(ext))
        except Exception as e:
            await self.bot.say("Couldn't load cog {}.".format(ext))
            print(e)

    @commands.command(name='unload', hidden=True)
    @checks.is_owner()
    async def unload_ext(self, ext):
        mod = 'cogs.' + ext
        if mod not in self.bot.extensions:
            await self.bot.say('Cog {} is not loaded.'.format(ext))
            return
        try:
            self.bot.unload_extension(mod)
            await self.bot.say('Unloaded cog {}.'.format(ext))
        except Exception as e:
            await self.bot.say("Couldn't unload cog {}.".format(ext))
            print(e)


def setup(bot):
    bot.add_cog(CogManagement(bot))

@@ -794,38 +794,69 @@
 eload_ext(self, 
+*
 ext
+s
 ):%0A
+        for ext in exts:%0A    
         mod = 'c
@@ -859,32 +859,36 @@
 = 'cogs.' + ext%0A
+    
         if mod n
@@ -906,32 +906,36 @@
 bot.extensions:%0A
+    
             awai
@@ -990,32 +990,36 @@
 t))%0A            
+    
 return%0A        t
@@ -1009,37 +1009,45 @@
  return%0A        
+  
+  
 try:%0A
+    
             awai
@@ -1080,32 +1080,36 @@
 xt)%0A            
+    
 await self.bot.s
@@ -1135,32 +1135,36 @@
 .'.format(ext))%0A
+    
         except E
@@ -1182,32 +1182,36 @@
  e:%0A            
+    
 await self.bot.s
@@ -1244,32 +1244,36 @@
 .%22.format(ext))%0A
+    
             prin
@@ -1662,38 +1662,69 @@
  load_ext(self, 
+*
 ext
+s
 ):%0A
+        for ext in exts:%0A    
         mod = 'c
@@ -1739,24 +1739,28 @@
 ext%0A        
+   
+ 
 if mod in se
@@ -1770,32 +1770,36 @@
 bot.extensions:%0A
+    
             awai
@@ -1858,39 +1858,47 @@
 t))%0A            
+    
 return%0A
+    
         try:%0A   
@@ -1898,32 +1898,36 @@
 ry:%0A            
+    
 self.bot.load_ex
@@ -1931,32 +1931,36 @@
 _extension(mod)%0A
+    
             awai
@@ -1996,32 +1996,36 @@
 .'.format(ext))%0A
+    
         except E
@@ -2043,32 +2043,36 @@
  e:%0A            
+    
 await self.bot.s
@@ -2103,32 +2103,36 @@
 .%22.format(ext))%0A
+    
             prin
@@ -2237,30 +2237,61 @@
 d_ext(self, 
+*
 ext
+s
 ):%0A
+        for ext in exts:%0A    
         mod 
@@ -2298,32 +2298,36 @@
 = 'cogs.' + ext%0A
+    
         if mod n
@@ -2357,32 +2357,36 @@
 ns:%0A            
+ 
+   
 await self.bot.s
@@ -2429,32 +2429,36 @@
 t))%0A            
+   
+ 
 return%0A        t
@@ -2448,37 +2448,45 @@
  return%0A        
+    
 try:%0A
+    
             self
@@ -2504,32 +2504,36 @@
 _extension(mod)%0A
+    
             awai
@@ -2579,32 +2579,36 @@
 t(ext))%0A        
+    
 except Exception
@@ -2606,32 +2606,36 @@
 Exception as e:%0A
+    
             awai
@@ -2680,32 +2680,36 @@
 .%22.format(ext))%0A
+    
             prin

allow cog loading commands to accept multiple cog names