Once an extension is loaded there is no way to pick up code changes without restarting the entire bot. load_extension and unload_extension already exist in bot.py, but there is no reload_extension that does both atomically and re-imports the underlying Python module.
The idea is to add bot.reload_extension(name) which unloads the running extension, re-imports its module with importlib.reload, re-instantiates the extension, and loads it back in. This makes the development loop significantly tighter without requiring a full bot restart.
Proposed API
bot.reload_extension("moderation")
# Or trigger it from inside a command
@bot.command()
async def reload(ctx, name: str):
bot.reload_extension(name)
await ctx.reply(f"Reloaded {name}.")
The critical detail is that importlib.reload only re-executes the module, it does not re-create the extension object, so reload_extension must also instantiate a fresh Extension from the reloaded module before calling load_extension. This means the framework needs a way to locate the extension's module (the mapping from extension name to module path should be stored at load time). State held inside the old extension instance is lost on reload, which is expected behaviour but should be documented clearly.
Once an extension is loaded there is no way to pick up code changes without restarting the entire bot.
load_extensionandunload_extensionalready exist inbot.py, but there is noreload_extensionthat does both atomically and re-imports the underlying Python module.The idea is to add
bot.reload_extension(name)which unloads the running extension, re-imports its module withimportlib.reload, re-instantiates the extension, and loads it back in. This makes the development loop significantly tighter without requiring a full bot restart.Proposed API
The critical detail is that
importlib.reloadonly re-executes the module, it does not re-create the extension object, soreload_extensionmust also instantiate a freshExtensionfrom the reloaded module before callingload_extension. This means the framework needs a way to locate the extension's module (the mapping from extension name to module path should be stored at load time). State held inside the old extension instance is lost on reload, which is expected behaviour but should be documented clearly.