[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
This commit is contained in:
Will 2017-10-22 21:13:23 -04:00 committed by GitHub
parent 3febc94871
commit d346216fa2
2 changed files with 42 additions and 1 deletions

View File

@ -185,6 +185,29 @@ class Downloader:
elif target.is_file(): elif target.is_file():
os.remove(str(target)) 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() @commands.group()
@checks.is_owner() @checks.is_owner()
async def repo(self, ctx): async def repo(self, ctx):

View File

@ -4,6 +4,8 @@ from importlib.machinery import ModuleSpec
from pathlib import Path from pathlib import Path
from typing import Tuple, Union, List from typing import Tuple, Union, List
import redbot.cogs
from . import checks from . import checks
from .config import Config from .config import Config
from .i18n import CogI18n from .i18n import CogI18n
@ -45,14 +47,19 @@ class CogManager:
""" """
conf_paths = await self.conf.paths() conf_paths = await self.conf.paths()
other_paths = self._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] paths = [Path(p) for p in all_paths]
if self.install_path not in paths: if self.install_path not in paths:
paths.insert(0, await self.install_path()) paths.insert(0, await self.install_path())
return tuple(p.resolve() for p in paths if p.is_dir()) 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: async def install_path(self) -> Path:
"""Get the install path for 3rd party cogs. """Get the install path for 3rd party cogs.
@ -209,6 +216,17 @@ class CogManager:
raise RuntimeError("No module by the name of '{}' was found" raise RuntimeError("No module by the name of '{}' was found"
" in any available path.".format(name)) " 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 @staticmethod
def invalidate_caches(): def invalidate_caches():
"""Re-evaluate modules in the py cache. """Re-evaluate modules in the py cache.