Draft
Conversation
Contributor
|
As far as i understand, with current implementation it is not possible to create arbitrarily-nested sub-plugins (sub-sub-plugins, sub-sub-sub-plugins, etc.)? While this is not something that is absolutely necessary, it will be a good feature to have, for sub-sub-commands especially. I imagine such an implementation will remove the separate SubPlugin class and instead rework "main" Plugin so that it can act as either a "root" or a "sub-" one; due to that, it'll likely have to always use command placeholder classes. Apart from that, good job overall (haven't done a "real" review yet)! Really looking forward for this to be merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
SubPlugins 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
pingin 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_placeholdersis 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__.pyand bothfoo.pyandbar.pyhouse a sub-plugin, callingplugin.gather_from_package()(name pending) would automatically look for sub-plugins insidefoo.pyandbar.pyand register them.