feat!: implement support for sub-plugins #13
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a multitude of things related to more conveniently splitting functionality over different files. Please refer to the sections below. To make all of this possible, plugins were first refactored by splitting typing and logical utilities into separate files, and extracting some behaviour into separate base classes. Therefore, this is technically a breaking change. However, the existing public API should remain unchanged.
Note that method/function names are not yet final, and may change before this PR is merged.
If you have any feedback or suggestions, please let me know!
Sub-plugins
SubPlugin
s add support for splitting a plugin across multiple files. On a very basic level, working with sub-plugins is done as follows:Command Placeholders
Command placeholders allow you to separate a single command over multiple files. For this PR (and the foreseeable future), this feature only supports Slash Commands. If there is sufficient demand to do the same for context commands, I will look into implementing this, too.
For most purposes, using command placeholders is very simple. Say we wish to create a slash command
ping
in our main plugin file, and use a sub-plugin to define a subcommandpong
. This is achieved as follows:The order in which these are defined and loaded does not matter, as long as the sub plugin is loaded before the plugin is loaded into the bot. Command placeholders are merged into their target parent commands when
Plugin.merge_placeholders
is called, which is done automatically when the plugin is loaded.To-do
A couple points remain to be done:
If a plugin that has sub-plugins is unloaded, the sub-plugins should be unloaded along with it. At the moment, this is not yet the case.
Lastly, I am considering implementing a sort of magic function to automatically load all modules in a package, find the sub-plugins inside the module, and register them to the plugin. For example, given a file structure like
where the main plugin is inside
__init__.py
and bothfoo.py
andbar.py
house a sub-plugin, callingplugin.gather_from_package()
(name pending) would automatically look for sub-plugins insidefoo.py
andbar.py
and register them.