keep reloading cogs if one fails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
leanderandClaude Opus 5.5 committed 2026-11-03 01:42:21 +01:00
1 parent a395df5a47
commit c3c1f7c012
1 file changed
+51 -12
+51 -12
View File
@@ -192,16 +192,10 @@ class DevTools(commands.Cog):
ctx (commands.Context): Context object ctx (commands.Context): Context object
""" """
# Unloading or reloading a cog that isn't loaded raises an error, so those are loaded instead extensions = [f"cogs.{file[:-3]}" for file in listdir("./src/cogs") if file.endswith(".py")]
for file in listdir("./src/cogs"): failed = await self.reload_extensions(extensions)
if file.endswith(".py"):
name = f"cogs.{file[:-3]}"
if name in self.bot.extensions:
await self.bot.reload_extension(name)
else:
await self.bot.load_extension(name)
embed = discord.Embed(color=ctx.me.color, description="Reloaded all cogs") embed = discord.Embed(color=ctx.me.color, description=self.reload_summary("Reloaded all cogs", failed))
await ctx.reply(embed=embed) await ctx.reply(embed=embed)
@cogs.command(name="reloadall", description="Last inn alle cogs på nytt") @cogs.command(name="reloadall", description="Last inn alle cogs på nytt")
@@ -215,12 +209,57 @@ class DevTools(commands.Cog):
""" """
# Only loaded cogs can be reloaded. Copied to a list since reloading modifies bot.extensions # Only loaded cogs can be reloaded. Copied to a list since reloading modifies bot.extensions
for name in list(self.bot.extensions): failed = await self.reload_extensions(list(self.bot.extensions))
await self.bot.reload_extension(name)
embed = discord.Embed(color=ctx.me.color, description="Reloaded all previously enabled cogs") embed = discord.Embed(
color=ctx.me.color, description=self.reload_summary("Reloaded all previously enabled cogs", failed)
)
await ctx.reply(embed=embed) await ctx.reply(embed=embed)
async def reload_extensions(self, extensions: list[str]) -> dict[str, Exception]:
"""
Reloads the given extensions, loading the ones that aren't loaded yet
Parameters
----------
extensions (list[str]): Names of the extensions to reload
Returns
----------
(dict[str, Exception]): The extensions that failed and why
"""
failed = {}
for extension in extensions:
try:
if extension in self.bot.extensions:
await self.bot.reload_extension(extension)
else:
await self.bot.load_extension(extension)
except commands.ExtensionError as e:
self.bot.logger.error(f"Failed to reload {extension}: {e}")
failed[extension] = e
return failed
def reload_summary(self, message: str, failed: dict[str, Exception]) -> str:
"""
Builds the reply for the reload commands
Parameters
----------
message (str): The message to show when everything was reloaded
failed (dict[str, Exception]): The extensions that failed and why
Returns
----------
(str): The reply text
"""
if not failed:
return message
failures = "\n".join(f"* `{extension}`: {type(e.__cause__ or e).__name__}" for extension, e in failed.items())
return f"{message}, except:\n{failures}"
async def setup(bot: commands.Bot): async def setup(bot: commands.Bot):
""" """