📋 Getting Information About Other Modules
Your module might need to interact with or get information about other modules currently loaded in the bot. The ModuleLoader
, accessible via self.loader
(if the use_loader
permission is granted), provides methods for this.
Getting Loaded Modules Info (loaded_modules
Property)
The BaseModule class provides a convenient property loaded_modules which is a shortcut to self.loader.get_modules_info().
self.loaded_modules
: Returns a dictionary where keys are the internal directory names of the currently loaded modules, and values are their correspondingModuleInfo
objects (base.module.ModuleInfo
).
Example Usage:
# Inside a BaseModule or ModuleExtension method
def list_loaded_modules(self):
"""Logs the names and versions of all currently loaded modules."""
if not self.loader: # Check if loader access is available
self.logger.error("Cannot list modules: Loader access not granted.")
return
loaded: dict[str, ModuleInfo] = self.loaded_modules # Use the property
if not loaded:
self.logger.info("No other modules are currently loaded.")
return
self.logger.info("Currently loaded modules:")
for internal_name, info in loaded.items():
# info is a ModuleInfo dataclass instance
self.logger.info(
f"- {info.name} (v{info.version}) "
f"[Internal: {internal_name}, Author: {info.author}]"
)
async def check_for_core_module(self, message: Message):
"""Checks if a module named 'Core' is loaded."""
if not self.loader:
await message.reply("Error: Loader access required.")
return
core_loaded = False
for info in self.loaded_modules.values():
if info.name.lower() == "core":
core_loaded = True
break
if core_loaded:
await message.reply("The Core module is currently loaded.")
else:
await message.reply("The Core module is not loaded.")
Other Informational Methods (via self.loader
)
get_all_modules_info() -> dict[str, ModuleInfo]
: Similar toloaded_modules
, but includes info for all modules found in the modules/ directory, even those that failed to load or have auto_load: false. Useful for management UIs.get_module(name: str) -> Optional[BaseModule]
: Get the actual running instance of another loaded module by its internal directory name. Use with caution, as directly calling methods on other modules can create tight coupling.get_module_info(name: str) -> Optional[ModuleInfo]
: GetModuleInfo
for a specific module (by internal name), whether loaded or not.get_module_help(name: str) -> Optional[Union[HelpPage, str]]
: Get thehelp
page/string for a specific loaded module.get_module_perms(name: str) -> list[Permissions]
: Get the declared permissions for a specific loaded module.get_int_name(name: str) -> Optional[str]
: Convert a user-friendly name (frominfo.name
) back to its internal directory name.
Using these methods allows modules to be aware of their environment and potentially adapt their behavior based on which other modules are active. Remember to request use_loader
permission first.