discord-ext-autoreload
A discord.py extension that allows automatic hot-reloading of extensions
When it comes to bot development, reloading extensions at runtime is quite a common practice. However, currently there is no way of automatically hot reloading extensions. This simple extension allows you to conveniently auto-reload extensions.
The features of this extension are:
- Easy to setup and use
- Proper exceptions handling during auto-reload
- Minimal interface matching that of discord.py
- Compatible with both discord.py v1.7 and v2.0
Installation
This extension can easily be installed using pip:
pip install discord-ext-autoreload
Usage
The Reloader class is used for setting up automatic reloads.
import discord
from discord.ext import commands, autoreload
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
reloader = autoreload.Reloader(ext_directory="cogs")
if __name__ == "__main__":
reloader.start(bot)
bot.run()
For discord.py 2.0
In discord.py 2.0 and higher, you would need to call the reloader.start()
method in an async context. The most convenient way of doing this is by
overriding the Bot.setup_hook() method.
class Bot(commands.Bot):
async def setup_hook(self) -> None:
reloader.start(self)
And done! The Reloader class will start watching the provided ext_directory for
changes in loaded extensions and as soon as an extension is updated, It will automatically
be reloaded.
Stopping the reloader
Sometimes, you might want to stop the reloader. You can use the Reloader.stop() method
to easily do so. For example:
@bot.command()
async def togglereload(ctx):
if reloader.stopped:
reloader.start(bot)
await ctx.send("Enabled")
else:
reloader.stop()
await ctx.send("Disabled")
Error Handling
When auto reloading fails for some reason, the error is properly handled and propagated
to Reloader.on_error. This function by default logs the error but can be overridden to
implement custom functionality.
import traceback
class Reloader(autoreload.Reloader):
async def on_error(self, extension: str):
print(f"Extension {extension!r} failed to auto reload")
traceback.print_exc()
Tracking reloads
When an extension is reloaded, the Reloader.on_reload method is called. This method can
be implemented by a subclass to easily track successful automatic reloads.
Excluding extensions from reloading
Sometimes, you want certain extensions to not be subject of automatic reloading. Fortunately,
this package allows you to exclude certain extensions by passing the exclude_exts keyword
argument in Reloader class.
reloader = Reloader(ext_directory="cogs", exclude_exts=["cogs.some_extension"])
Metadata
Release files for discord-ext-autoreload 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| discord_ext_autoreload-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for discord_ext_autoreload-0.1.2-py3-none-any.whl |
Release files / discord_ext_autoreload-0.1.2-py3-none-any.whl
| Download URL | discord_ext_autoreload-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ac3b7478508ad656d64c9e9c2a4cf7435703a3faf1b6e80b3019e07353db9a75
|
|
BLAKE2b-256 checksum How to use checksums |
1d72d5f808a6554e0d3e6dd17b197db45fbfe4efea910a11b23e35ba3b59a533
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Security for discord-ext-autoreload 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.