From d346216fa2ac17092117ea9d987f16ab2c51f883 Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 22 Oct 2017 21:13:23 -0400 Subject: [PATCH] [V3 Downloader] List all loaded and unloaded cogs (#1019) * Working without core cogs * Working with core cogs * Fix path search logic * Fix docstring * Type fix --- redbot/cogs/downloader/downloader.py | 23 +++++++++++++++++++++++ redbot/core/cog_manager.py | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/redbot/cogs/downloader/downloader.py b/redbot/cogs/downloader/downloader.py index c95b5c9d1..8667b9e9f 100644 --- a/redbot/cogs/downloader/downloader.py +++ b/redbot/cogs/downloader/downloader.py @@ -185,6 +185,29 @@ class Downloader: elif target.is_file(): os.remove(str(target)) + @commands.command() + @checks.is_owner() + async def cogs(self, ctx): + """ + Lists all loaded and available cogs. + """ + loaded = set(self.bot.extensions.keys()) + + all = set(await self.bot.cog_mgr.available_modules()) + + unloaded = all - loaded + + await ctx.send( + box( + "+ Loaded\n{}\n- Unloaded\n{}".format( + ', '.join(sorted(loaded)), ', '.join(sorted(unloaded)) + ), + lang='diff' + ) + ) + + + @commands.group() @checks.is_owner() async def repo(self, ctx): diff --git a/redbot/core/cog_manager.py b/redbot/core/cog_manager.py index 12cccc10b..5bc80741e 100644 --- a/redbot/core/cog_manager.py +++ b/redbot/core/cog_manager.py @@ -4,6 +4,8 @@ from importlib.machinery import ModuleSpec from pathlib import Path from typing import Tuple, Union, List +import redbot.cogs + from . import checks from .config import Config from .i18n import CogI18n @@ -45,14 +47,19 @@ class CogManager: """ conf_paths = await self.conf.paths() other_paths = self._paths + core_paths = await self.core_paths() - all_paths = set(list(conf_paths) + list(other_paths)) + all_paths = set(list(conf_paths) + list(other_paths) + core_paths) paths = [Path(p) for p in all_paths] if self.install_path not in paths: paths.insert(0, await self.install_path()) return tuple(p.resolve() for p in paths if p.is_dir()) + async def core_paths(self) -> List[Path]: + core_paths = [Path(p) for p in redbot.cogs.__path__] + return core_paths + async def install_path(self) -> Path: """Get the install path for 3rd party cogs. @@ -209,6 +216,17 @@ class CogManager: raise RuntimeError("No module by the name of '{}' was found" " in any available path.".format(name)) + async def available_modules(self) -> List[str]: + """Finds the names of all available modules to load. + """ + paths = (await self.install_path(), ) + await self.paths() + paths = [str(p) for p in paths] + + ret = [] + for finder, module_name, _ in pkgutil.iter_modules(paths): + ret.append(module_name) + return ret + @staticmethod def invalidate_caches(): """Re-evaluate modules in the py cache.