We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Cog load with duplicate command reporting wrong error
**Originally posted by Scragly:**

In the `cogs` extension module, there's a `try: except ClientException`. The reason for the error is hardcoded, assuming that it raises only when a setup function does not exist.

[Click here to view the relevant code.](https://gitlab.com/python-discord/projects/bot/blob/184b6d51e44915319e09b1bdf24ee26541391350/bot/cogs/cogs.py#L80-83)

Unfortunately, the same exception will also raise down the stack in `bot.add_cog` when a command within the cog has a name that's already existing in commands, conflicting and raising the exception.

To avoid incorrect errors being reported/logged, and to prevent confusion during debugging, it might be best to simply remove the `except ClientException` block and let it fall down into the catchall `except Exception as e` block which prints the exception details as given.

Feel free to comment better suggestions, of course. 

This will be something for after the migration over to GitHub, hence why it's an Issue, rather than a quick MR.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `bot/cogs/cogs.py`
Content:
```
1 import logging
2 import os
3 
4 from discord import ClientException, Colour, Embed
5 from discord.ext.commands import Bot, Context, group
6 
7 from bot.constants import (
8     Emojis, Roles, URLs,
9 )
10 from bot.decorators import with_role
11 from bot.pagination import LinePaginator
12 
13 log = logging.getLogger(__name__)
14 
15 KEEP_LOADED = ["bot.cogs.cogs", "bot.cogs.modlog"]
16 
17 
18 class Cogs:
19     """
20     Cog management commands
21     """
22 
23     def __init__(self, bot: Bot):
24         self.bot = bot
25         self.cogs = {}
26 
27         # Load up the cog names
28         log.info("Initializing cog names...")
29         for filename in os.listdir("bot/cogs"):
30             if filename.endswith(".py") and "_" not in filename:
31                 if os.path.isfile(f"bot/cogs/{filename}"):
32                     cog = filename[:-3]
33 
34                     self.cogs[cog] = f"bot.cogs.{cog}"
35 
36         # Allow reverse lookups by reversing the pairs
37         self.cogs.update({v: k for k, v in self.cogs.items()})
38 
39     @group(name='cogs', aliases=('c',), invoke_without_command=True)
40     @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)
41     async def cogs_group(self, ctx: Context):
42         """Load, unload, reload, and list active cogs."""
43 
44         await ctx.invoke(self.bot.get_command("help"), "cogs")
45 
46     @cogs_group.command(name='load', aliases=('l',))
47     @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)
48     async def load_command(self, ctx: Context, cog: str):
49         """
50         Load up an unloaded cog, given the module containing it
51 
52         You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the
53         entire module directly.
54         """
55 
56         cog = cog.lower()
57 
58         embed = Embed()
59         embed.colour = Colour.red()
60 
61         embed.set_author(
62             name="Python Bot (Cogs)",
63             url=URLs.gitlab_bot_repo,
64             icon_url=URLs.bot_avatar
65         )
66 
67         if cog in self.cogs:
68             full_cog = self.cogs[cog]
69         elif "." in cog:
70             full_cog = cog
71         else:
72             full_cog = None
73             log.warning(f"{ctx.author} requested we load the '{cog}' cog, but that cog doesn't exist.")
74             embed.description = f"Unknown cog: {cog}"
75 
76         if full_cog:
77             if full_cog not in self.bot.extensions:
78                 try:
79                     self.bot.load_extension(full_cog)
80                 except ClientException:
81                     log.error(f"{ctx.author} requested we load the '{cog}' cog, "
82                               "but that cog doesn't have a 'setup()' function.")
83                     embed.description = f"Invalid cog: {cog}\n\nCog does not have a `setup()` function"
84                 except ImportError:
85                     log.error(f"{ctx.author} requested we load the '{cog}' cog, "
86                               f"but the cog module {full_cog} could not be found!")
87                     embed.description = f"Invalid cog: {cog}\n\nCould not find cog module {full_cog}"
88                 except Exception as e:
89                     log.error(f"{ctx.author} requested we load the '{cog}' cog, "
90                               "but the loading failed with the following error: \n"
91                               f"{e}")
92                     embed.description = f"Failed to load cog: {cog}\n\n```{e}```"
93                 else:
94                     log.debug(f"{ctx.author} requested we load the '{cog}' cog. Cog loaded!")
95                     embed.description = f"Cog loaded: {cog}"
96                     embed.colour = Colour.green()
97             else:
98                 log.warning(f"{ctx.author} requested we load the '{cog}' cog, but the cog was already loaded!")
99                 embed.description = f"Cog {cog} is already loaded"
100 
101         await ctx.send(embed=embed)
102 
103     @cogs_group.command(name='unload', aliases=('ul',))
104     @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)
105     async def unload_command(self, ctx: Context, cog: str):
106         """
107         Unload an already-loaded cog, given the module containing it
108 
109         You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the
110         entire module directly.
111         """
112 
113         cog = cog.lower()
114 
115         embed = Embed()
116         embed.colour = Colour.red()
117 
118         embed.set_author(
119             name="Python Bot (Cogs)",
120             url=URLs.gitlab_bot_repo,
121             icon_url=URLs.bot_avatar
122         )
123 
124         if cog in self.cogs:
125             full_cog = self.cogs[cog]
126         elif "." in cog:
127             full_cog = cog
128         else:
129             full_cog = None
130             log.warning(f"{ctx.author} requested we unload the '{cog}' cog, but that cog doesn't exist.")
131             embed.description = f"Unknown cog: {cog}"
132 
133         if full_cog:
134             if full_cog in KEEP_LOADED:
135                 log.warning(f"{ctx.author} requested we unload `{full_cog}`, that sneaky pete. We said no.")
136                 embed.description = f"You may not unload `{full_cog}`!"
137             elif full_cog in self.bot.extensions:
138                 try:
139                     self.bot.unload_extension(full_cog)
140                 except Exception as e:
141                     log.error(f"{ctx.author} requested we unload the '{cog}' cog, "
142                               "but the unloading failed with the following error: \n"
143                               f"{e}")
144                     embed.description = f"Failed to unload cog: {cog}\n\n```{e}```"
145                 else:
146                     log.debug(f"{ctx.author} requested we unload the '{cog}' cog. Cog unloaded!")
147                     embed.description = f"Cog unloaded: {cog}"
148                     embed.colour = Colour.green()
149             else:
150                 log.warning(f"{ctx.author} requested we unload the '{cog}' cog, but the cog wasn't loaded!")
151                 embed.description = f"Cog {cog} is not loaded"
152 
153         await ctx.send(embed=embed)
154 
155     @cogs_group.command(name='reload', aliases=('r',))
156     @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)
157     async def reload_command(self, ctx: Context, cog: str):
158         """
159         Reload an unloaded cog, given the module containing it
160 
161         You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the
162         entire module directly.
163 
164         If you specify "*" as the cog, every cog currently loaded will be unloaded, and then every cog present in the
165         bot/cogs directory will be loaded.
166         """
167 
168         cog = cog.lower()
169 
170         embed = Embed()
171         embed.colour = Colour.red()
172 
173         embed.set_author(
174             name="Python Bot (Cogs)",
175             url=URLs.gitlab_bot_repo,
176             icon_url=URLs.bot_avatar
177         )
178 
179         if cog == "*":
180             full_cog = cog
181         elif cog in self.cogs:
182             full_cog = self.cogs[cog]
183         elif "." in cog:
184             full_cog = cog
185         else:
186             full_cog = None
187             log.warning(f"{ctx.author} requested we reload the '{cog}' cog, but that cog doesn't exist.")
188             embed.description = f"Unknown cog: {cog}"
189 
190         if full_cog:
191             if full_cog == "*":
192                 all_cogs = [
193                     f"bot.cogs.{fn[:-3]}" for fn in os.listdir("bot/cogs")
194                     if os.path.isfile(f"bot/cogs/{fn}") and fn.endswith(".py") and "_" not in fn
195                 ]
196 
197                 failed_unloads = {}
198                 failed_loads = {}
199 
200                 unloaded = 0
201                 loaded = 0
202 
203                 for loaded_cog in self.bot.extensions.copy().keys():
204                     try:
205                         self.bot.unload_extension(loaded_cog)
206                     except Exception as e:
207                         failed_unloads[loaded_cog] = str(e)
208                     else:
209                         unloaded += 1
210 
211                 for unloaded_cog in all_cogs:
212                     try:
213                         self.bot.load_extension(unloaded_cog)
214                     except Exception as e:
215                         failed_loads[unloaded_cog] = str(e)
216                     else:
217                         loaded += 1
218 
219                 lines = [
220                     "**All cogs reloaded**",
221                     f"**Unloaded**: {unloaded} / **Loaded**: {loaded}"
222                 ]
223 
224                 if failed_unloads:
225                     lines.append("\n**Unload failures**")
226 
227                     for cog, error in failed_unloads:
228                         lines.append(f"`{cog}` {Emojis.white_chevron} `{error}`")
229 
230                 if failed_loads:
231                     lines.append("\n**Load failures**")
232 
233                     for cog, error in failed_loads:
234                         lines.append(f"`{cog}` {Emojis.white_chevron} `{error}`")
235 
236                 log.debug(f"{ctx.author} requested we reload all cogs. Here are the results: \n"
237                           f"{lines}")
238 
239                 return await LinePaginator.paginate(lines, ctx, embed, empty=False)
240 
241             elif full_cog in self.bot.extensions:
242                 try:
243                     self.bot.unload_extension(full_cog)
244                     self.bot.load_extension(full_cog)
245                 except Exception as e:
246                     log.error(f"{ctx.author} requested we reload the '{cog}' cog, "
247                               "but the unloading failed with the following error: \n"
248                               f"{e}")
249                     embed.description = f"Failed to reload cog: {cog}\n\n```{e}```"
250                 else:
251                     log.debug(f"{ctx.author} requested we reload the '{cog}' cog. Cog reloaded!")
252                     embed.description = f"Cog reload: {cog}"
253                     embed.colour = Colour.green()
254             else:
255                 log.warning(f"{ctx.author} requested we reload the '{cog}' cog, but the cog wasn't loaded!")
256                 embed.description = f"Cog {cog} is not loaded"
257 
258         await ctx.send(embed=embed)
259 
260     @cogs_group.command(name='list', aliases=('all',))
261     @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)
262     async def list_command(self, ctx: Context):
263         """
264         Get a list of all cogs, including their loaded status.
265 
266         A red double-chevron indicates that the cog is unloaded. Green indicates that the cog is currently loaded.
267         """
268 
269         embed = Embed()
270         lines = []
271         cogs = {}
272 
273         embed.colour = Colour.blurple()
274         embed.set_author(
275             name="Python Bot (Cogs)",
276             url=URLs.gitlab_bot_repo,
277             icon_url=URLs.bot_avatar
278         )
279 
280         for key, _value in self.cogs.items():
281             if "." not in key:
282                 continue
283 
284             if key in self.bot.extensions:
285                 cogs[key] = True
286             else:
287                 cogs[key] = False
288 
289         for key in self.bot.extensions.keys():
290             if key not in self.cogs:
291                 cogs[key] = True
292 
293         for cog, loaded in sorted(cogs.items(), key=lambda x: x[0]):
294             if cog in self.cogs:
295                 cog = self.cogs[cog]
296 
297             if loaded:
298                 chevron = Emojis.green_chevron
299             else:
300                 chevron = Emojis.red_chevron
301 
302             lines.append(f"{chevron}  {cog}")
303 
304         log.debug(f"{ctx.author} requested a list of all cogs. Returning a paginated list.")
305         await LinePaginator.paginate(lines, ctx, embed, max_size=300, empty=False)
306 
307 
308 def setup(bot):
309     bot.add_cog(Cogs(bot))
310     log.info("Cog loaded: Cogs")
311
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/bot/cogs/cogs.py b/bot/cogs/cogs.py
--- a/bot/cogs/cogs.py
+++ b/bot/cogs/cogs.py
@@ -1,7 +1,7 @@
 import logging
 import os
 
-from discord import ClientException, Colour, Embed
+from discord import Colour, Embed
 from discord.ext.commands import Bot, Context, group
 
 from bot.constants import (
@@ -77,10 +77,6 @@
             if full_cog not in self.bot.extensions:
                 try:
                     self.bot.load_extension(full_cog)
-                except ClientException:
-                    log.error(f"{ctx.author} requested we load the '{cog}' cog, "
-                              "but that cog doesn't have a 'setup()' function.")
-                    embed.description = f"Invalid cog: {cog}\n\nCog does not have a `setup()` function"
                 except ImportError:
                     log.error(f"{ctx.author} requested we load the '{cog}' cog, "
                               f"but the cog module {full_cog} could not be found!")

{"golden_diff": "diff --git a/bot/cogs/cogs.py b/bot/cogs/cogs.py\n--- a/bot/cogs/cogs.py\n+++ b/bot/cogs/cogs.py\n@@ -1,7 +1,7 @@\n import logging\n import os\n \n-from discord import ClientException, Colour, Embed\n+from discord import Colour, Embed\n from discord.ext.commands import Bot, Context, group\n \n from bot.constants import (\n@@ -77,10 +77,6 @@\n             if full_cog not in self.bot.extensions:\n                 try:\n                     self.bot.load_extension(full_cog)\n-                except ClientException:\n-                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n-                              \"but that cog doesn't have a 'setup()' function.\")\n-                    embed.description = f\"Invalid cog: {cog}\\n\\nCog does not have a `setup()` function\"\n                 except ImportError:\n                     log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                               f\"but the cog module {full_cog} could not be found!\")\n", "issue": "Cog load with duplicate command reporting wrong error\n**Originally posted by Scragly:**\n\nIn the `cogs` extension module, there's a `try: except ClientException`. The reason for the error is hardcoded, assuming that it raises only when a setup function does not exist.\n\n[Click here to view the relevant code.](https://gitlab.com/python-discord/projects/bot/blob/184b6d51e44915319e09b1bdf24ee26541391350/bot/cogs/cogs.py#L80-83)\n\nUnfortunately, the same exception will also raise down the stack in `bot.add_cog` when a command within the cog has a name that's already existing in commands, conflicting and raising the exception.\n\nTo avoid incorrect errors being reported/logged, and to prevent confusion during debugging, it might be best to simply remove the `except ClientException` block and let it fall down into the catchall `except Exception as e` block which prints the exception details as given.\n\nFeel free to comment better suggestions, of course. \n\nThis will be something for after the migration over to GitHub, hence why it's an Issue, rather than a quick MR.\n", "before_files": [{"content": "import logging\nimport os\n\nfrom discord import ClientException, Colour, Embed\nfrom discord.ext.commands import Bot, Context, group\n\nfrom bot.constants import (\n    Emojis, Roles, URLs,\n)\nfrom bot.decorators import with_role\nfrom bot.pagination import LinePaginator\n\nlog = logging.getLogger(__name__)\n\nKEEP_LOADED = [\"bot.cogs.cogs\", \"bot.cogs.modlog\"]\n\n\nclass Cogs:\n    \"\"\"\n    Cog management commands\n    \"\"\"\n\n    def __init__(self, bot: Bot):\n        self.bot = bot\n        self.cogs = {}\n\n        # Load up the cog names\n        log.info(\"Initializing cog names...\")\n        for filename in os.listdir(\"bot/cogs\"):\n            if filename.endswith(\".py\") and \"_\" not in filename:\n                if os.path.isfile(f\"bot/cogs/{filename}\"):\n                    cog = filename[:-3]\n\n                    self.cogs[cog] = f\"bot.cogs.{cog}\"\n\n        # Allow reverse lookups by reversing the pairs\n        self.cogs.update({v: k for k, v in self.cogs.items()})\n\n    @group(name='cogs', aliases=('c',), invoke_without_command=True)\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def cogs_group(self, ctx: Context):\n        \"\"\"Load, unload, reload, and list active cogs.\"\"\"\n\n        await ctx.invoke(self.bot.get_command(\"help\"), \"cogs\")\n\n    @cogs_group.command(name='load', aliases=('l',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def load_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Load up an unloaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we load the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog not in self.bot.extensions:\n                try:\n                    self.bot.load_extension(full_cog)\n                except ClientException:\n                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                              \"but that cog doesn't have a 'setup()' function.\")\n                    embed.description = f\"Invalid cog: {cog}\\n\\nCog does not have a `setup()` function\"\n                except ImportError:\n                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                              f\"but the cog module {full_cog} could not be found!\")\n                    embed.description = f\"Invalid cog: {cog}\\n\\nCould not find cog module {full_cog}\"\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                              \"but the loading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to load cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we load the '{cog}' cog. Cog loaded!\")\n                    embed.description = f\"Cog loaded: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we load the '{cog}' cog, but the cog was already loaded!\")\n                embed.description = f\"Cog {cog} is already loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='unload', aliases=('ul',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def unload_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Unload an already-loaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we unload the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog in KEEP_LOADED:\n                log.warning(f\"{ctx.author} requested we unload `{full_cog}`, that sneaky pete. We said no.\")\n                embed.description = f\"You may not unload `{full_cog}`!\"\n            elif full_cog in self.bot.extensions:\n                try:\n                    self.bot.unload_extension(full_cog)\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we unload the '{cog}' cog, \"\n                              \"but the unloading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to unload cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we unload the '{cog}' cog. Cog unloaded!\")\n                    embed.description = f\"Cog unloaded: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we unload the '{cog}' cog, but the cog wasn't loaded!\")\n                embed.description = f\"Cog {cog} is not loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='reload', aliases=('r',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def reload_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Reload an unloaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n\n        If you specify \"*\" as the cog, every cog currently loaded will be unloaded, and then every cog present in the\n        bot/cogs directory will be loaded.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog == \"*\":\n            full_cog = cog\n        elif cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we reload the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog == \"*\":\n                all_cogs = [\n                    f\"bot.cogs.{fn[:-3]}\" for fn in os.listdir(\"bot/cogs\")\n                    if os.path.isfile(f\"bot/cogs/{fn}\") and fn.endswith(\".py\") and \"_\" not in fn\n                ]\n\n                failed_unloads = {}\n                failed_loads = {}\n\n                unloaded = 0\n                loaded = 0\n\n                for loaded_cog in self.bot.extensions.copy().keys():\n                    try:\n                        self.bot.unload_extension(loaded_cog)\n                    except Exception as e:\n                        failed_unloads[loaded_cog] = str(e)\n                    else:\n                        unloaded += 1\n\n                for unloaded_cog in all_cogs:\n                    try:\n                        self.bot.load_extension(unloaded_cog)\n                    except Exception as e:\n                        failed_loads[unloaded_cog] = str(e)\n                    else:\n                        loaded += 1\n\n                lines = [\n                    \"**All cogs reloaded**\",\n                    f\"**Unloaded**: {unloaded} / **Loaded**: {loaded}\"\n                ]\n\n                if failed_unloads:\n                    lines.append(\"\\n**Unload failures**\")\n\n                    for cog, error in failed_unloads:\n                        lines.append(f\"`{cog}` {Emojis.white_chevron} `{error}`\")\n\n                if failed_loads:\n                    lines.append(\"\\n**Load failures**\")\n\n                    for cog, error in failed_loads:\n                        lines.append(f\"`{cog}` {Emojis.white_chevron} `{error}`\")\n\n                log.debug(f\"{ctx.author} requested we reload all cogs. Here are the results: \\n\"\n                          f\"{lines}\")\n\n                return await LinePaginator.paginate(lines, ctx, embed, empty=False)\n\n            elif full_cog in self.bot.extensions:\n                try:\n                    self.bot.unload_extension(full_cog)\n                    self.bot.load_extension(full_cog)\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we reload the '{cog}' cog, \"\n                              \"but the unloading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to reload cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we reload the '{cog}' cog. Cog reloaded!\")\n                    embed.description = f\"Cog reload: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we reload the '{cog}' cog, but the cog wasn't loaded!\")\n                embed.description = f\"Cog {cog} is not loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='list', aliases=('all',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def list_command(self, ctx: Context):\n        \"\"\"\n        Get a list of all cogs, including their loaded status.\n\n        A red double-chevron indicates that the cog is unloaded. Green indicates that the cog is currently loaded.\n        \"\"\"\n\n        embed = Embed()\n        lines = []\n        cogs = {}\n\n        embed.colour = Colour.blurple()\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        for key, _value in self.cogs.items():\n            if \".\" not in key:\n                continue\n\n            if key in self.bot.extensions:\n                cogs[key] = True\n            else:\n                cogs[key] = False\n\n        for key in self.bot.extensions.keys():\n            if key not in self.cogs:\n                cogs[key] = True\n\n        for cog, loaded in sorted(cogs.items(), key=lambda x: x[0]):\n            if cog in self.cogs:\n                cog = self.cogs[cog]\n\n            if loaded:\n                chevron = Emojis.green_chevron\n            else:\n                chevron = Emojis.red_chevron\n\n            lines.append(f\"{chevron}  {cog}\")\n\n        log.debug(f\"{ctx.author} requested a list of all cogs. Returning a paginated list.\")\n        await LinePaginator.paginate(lines, ctx, embed, max_size=300, empty=False)\n\n\ndef setup(bot):\n    bot.add_cog(Cogs(bot))\n    log.info(\"Cog loaded: Cogs\")\n", "path": "bot/cogs/cogs.py"}], "after_files": [{"content": "import logging\nimport os\n\nfrom discord import Colour, Embed\nfrom discord.ext.commands import Bot, Context, group\n\nfrom bot.constants import (\n    Emojis, Roles, URLs,\n)\nfrom bot.decorators import with_role\nfrom bot.pagination import LinePaginator\n\nlog = logging.getLogger(__name__)\n\nKEEP_LOADED = [\"bot.cogs.cogs\", \"bot.cogs.modlog\"]\n\n\nclass Cogs:\n    \"\"\"\n    Cog management commands\n    \"\"\"\n\n    def __init__(self, bot: Bot):\n        self.bot = bot\n        self.cogs = {}\n\n        # Load up the cog names\n        log.info(\"Initializing cog names...\")\n        for filename in os.listdir(\"bot/cogs\"):\n            if filename.endswith(\".py\") and \"_\" not in filename:\n                if os.path.isfile(f\"bot/cogs/{filename}\"):\n                    cog = filename[:-3]\n\n                    self.cogs[cog] = f\"bot.cogs.{cog}\"\n\n        # Allow reverse lookups by reversing the pairs\n        self.cogs.update({v: k for k, v in self.cogs.items()})\n\n    @group(name='cogs', aliases=('c',), invoke_without_command=True)\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def cogs_group(self, ctx: Context):\n        \"\"\"Load, unload, reload, and list active cogs.\"\"\"\n\n        await ctx.invoke(self.bot.get_command(\"help\"), \"cogs\")\n\n    @cogs_group.command(name='load', aliases=('l',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def load_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Load up an unloaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we load the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog not in self.bot.extensions:\n                try:\n                    self.bot.load_extension(full_cog)\n                except ImportError:\n                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                              f\"but the cog module {full_cog} could not be found!\")\n                    embed.description = f\"Invalid cog: {cog}\\n\\nCould not find cog module {full_cog}\"\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we load the '{cog}' cog, \"\n                              \"but the loading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to load cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we load the '{cog}' cog. Cog loaded!\")\n                    embed.description = f\"Cog loaded: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we load the '{cog}' cog, but the cog was already loaded!\")\n                embed.description = f\"Cog {cog} is already loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='unload', aliases=('ul',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def unload_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Unload an already-loaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we unload the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog in KEEP_LOADED:\n                log.warning(f\"{ctx.author} requested we unload `{full_cog}`, that sneaky pete. We said no.\")\n                embed.description = f\"You may not unload `{full_cog}`!\"\n            elif full_cog in self.bot.extensions:\n                try:\n                    self.bot.unload_extension(full_cog)\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we unload the '{cog}' cog, \"\n                              \"but the unloading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to unload cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we unload the '{cog}' cog. Cog unloaded!\")\n                    embed.description = f\"Cog unloaded: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we unload the '{cog}' cog, but the cog wasn't loaded!\")\n                embed.description = f\"Cog {cog} is not loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='reload', aliases=('r',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def reload_command(self, ctx: Context, cog: str):\n        \"\"\"\n        Reload an unloaded cog, given the module containing it\n\n        You can specify the cog name for any cogs that are placed directly within `!cogs`, or specify the\n        entire module directly.\n\n        If you specify \"*\" as the cog, every cog currently loaded will be unloaded, and then every cog present in the\n        bot/cogs directory will be loaded.\n        \"\"\"\n\n        cog = cog.lower()\n\n        embed = Embed()\n        embed.colour = Colour.red()\n\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        if cog == \"*\":\n            full_cog = cog\n        elif cog in self.cogs:\n            full_cog = self.cogs[cog]\n        elif \".\" in cog:\n            full_cog = cog\n        else:\n            full_cog = None\n            log.warning(f\"{ctx.author} requested we reload the '{cog}' cog, but that cog doesn't exist.\")\n            embed.description = f\"Unknown cog: {cog}\"\n\n        if full_cog:\n            if full_cog == \"*\":\n                all_cogs = [\n                    f\"bot.cogs.{fn[:-3]}\" for fn in os.listdir(\"bot/cogs\")\n                    if os.path.isfile(f\"bot/cogs/{fn}\") and fn.endswith(\".py\") and \"_\" not in fn\n                ]\n\n                failed_unloads = {}\n                failed_loads = {}\n\n                unloaded = 0\n                loaded = 0\n\n                for loaded_cog in self.bot.extensions.copy().keys():\n                    try:\n                        self.bot.unload_extension(loaded_cog)\n                    except Exception as e:\n                        failed_unloads[loaded_cog] = str(e)\n                    else:\n                        unloaded += 1\n\n                for unloaded_cog in all_cogs:\n                    try:\n                        self.bot.load_extension(unloaded_cog)\n                    except Exception as e:\n                        failed_loads[unloaded_cog] = str(e)\n                    else:\n                        loaded += 1\n\n                lines = [\n                    \"**All cogs reloaded**\",\n                    f\"**Unloaded**: {unloaded} / **Loaded**: {loaded}\"\n                ]\n\n                if failed_unloads:\n                    lines.append(\"\\n**Unload failures**\")\n\n                    for cog, error in failed_unloads:\n                        lines.append(f\"`{cog}` {Emojis.white_chevron} `{error}`\")\n\n                if failed_loads:\n                    lines.append(\"\\n**Load failures**\")\n\n                    for cog, error in failed_loads:\n                        lines.append(f\"`{cog}` {Emojis.white_chevron} `{error}`\")\n\n                log.debug(f\"{ctx.author} requested we reload all cogs. Here are the results: \\n\"\n                          f\"{lines}\")\n\n                return await LinePaginator.paginate(lines, ctx, embed, empty=False)\n\n            elif full_cog in self.bot.extensions:\n                try:\n                    self.bot.unload_extension(full_cog)\n                    self.bot.load_extension(full_cog)\n                except Exception as e:\n                    log.error(f\"{ctx.author} requested we reload the '{cog}' cog, \"\n                              \"but the unloading failed with the following error: \\n\"\n                              f\"{e}\")\n                    embed.description = f\"Failed to reload cog: {cog}\\n\\n```{e}```\"\n                else:\n                    log.debug(f\"{ctx.author} requested we reload the '{cog}' cog. Cog reloaded!\")\n                    embed.description = f\"Cog reload: {cog}\"\n                    embed.colour = Colour.green()\n            else:\n                log.warning(f\"{ctx.author} requested we reload the '{cog}' cog, but the cog wasn't loaded!\")\n                embed.description = f\"Cog {cog} is not loaded\"\n\n        await ctx.send(embed=embed)\n\n    @cogs_group.command(name='list', aliases=('all',))\n    @with_role(Roles.moderator, Roles.admin, Roles.owner, Roles.devops)\n    async def list_command(self, ctx: Context):\n        \"\"\"\n        Get a list of all cogs, including their loaded status.\n\n        A red double-chevron indicates that the cog is unloaded. Green indicates that the cog is currently loaded.\n        \"\"\"\n\n        embed = Embed()\n        lines = []\n        cogs = {}\n\n        embed.colour = Colour.blurple()\n        embed.set_author(\n            name=\"Python Bot (Cogs)\",\n            url=URLs.gitlab_bot_repo,\n            icon_url=URLs.bot_avatar\n        )\n\n        for key, _value in self.cogs.items():\n            if \".\" not in key:\n                continue\n\n            if key in self.bot.extensions:\n                cogs[key] = True\n            else:\n                cogs[key] = False\n\n        for key in self.bot.extensions.keys():\n            if key not in self.cogs:\n                cogs[key] = True\n\n        for cog, loaded in sorted(cogs.items(), key=lambda x: x[0]):\n            if cog in self.cogs:\n                cog = self.cogs[cog]\n\n            if loaded:\n                chevron = Emojis.green_chevron\n            else:\n                chevron = Emojis.red_chevron\n\n            lines.append(f\"{chevron}  {cog}\")\n\n        log.debug(f\"{ctx.author} requested a list of all cogs. Returning a paginated list.\")\n        await LinePaginator.paginate(lines, ctx, embed, max_size=300, empty=False)\n\n\ndef setup(bot):\n    bot.add_cog(Cogs(bot))\n    log.info(\"Cog loaded: Cogs\")\n", "path": "bot/cogs/cogs.py"}]}