#Load new extensions without restart?
1 messages · Page 1 of 1 (latest)
The built-in module pkgutil, specifically its iter_modules function, makes finding modules, which include extensions, easy.
https://docs.python.org/3/library/pkgutil.html#pkgutil.iter_modules
Gathering all extensions in a specific folder (in this example, "exts") is easy:
from pkgutil import iter_modules
EXTENSIONS = [m.name for m in iter_modules(["exts"], prefix="exts.")]
This iterates through all Python modules in that directory, and puts each module/file name with the prefix "ext." (to make the entry "ext.name") into a list. This is the format interactions.py expects while loading extensions, and so we can use this new list to easily load them:
for extension in EXTENSIONS:
bot.load_extension(extension)
Can you show the folder structure you have?
Loading extensions uses the same system as importing modules, so If you were to load just twitch.py, it would be bot.load_extension("Cogs.Twitch.twitch"). What iter_modules and the for loop do is take every python file in the folder(s) you pass, and turns it into a list of paths that can be imported. The prefix would be the two folders that the python files are in (so "Cogs.Twitch.")
I would use on_start for things that only need to happen once during the lifetime of the bot, and the extension load event for things that are needed to prepare the extension
Typically, on_start is what you should use instead of on_ready, because the ready event can fire multiple times
i think i may add a note that extension load is usually not an event you should use
extension load's more of an event useful when making your own... not-just-Extension-extensions (ie prefixed commands)
usually Startup or async def async_start(self) is better for doing an async thing for an extension (but both of those only work on startup), or just using asyncio.create_loop if you can guarentee an event loop exists beforehand (which it will if the bots running)
see #1129588591035682906
well, you're iterating through Cogs.Twitch... but using a prefix of ext
youre making Cogs.Twitch.X into ext.X
the string in the list and the prefix should be exactly the same (though the prefix should have a dot at the end)