blob: afc5738397553ee54a0c604559ac576958013fbd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
from discord.ext.commands import Context
import importlib
import inspect
import pkgutil
from typing import Iterator, NoReturn
from bot import exts
def unqualify(name: str) -> str:
"""Return an unqualified name given a qualified module/package `name`."""
return name.rsplit(".", maxsplit=1)[-1]
def walk_extensions() -> Iterator[str]:
"""Yield extension names from the bot.exts subpackage."""
def on_error(name: str) -> NoReturn:
raise ImportError(name=name) # pragma: no cover
for module in pkgutil.walk_packages(exts.__path__, f"{exts.__name__}.", onerror=on_error):
if unqualify(module.name).startswith("_"):
# Ignore module/package names starting with an underscore.
continue
if module.ispkg:
imported = importlib.import_module(module.name)
if not inspect.isfunction(getattr(imported, "setup", None)):
# If it lacks a setup function, it's not an extension.
continue
yield module.name
async def invoke_help_command(ctx: Context, *commands: str) -> None:
"""Invoke the help command or default help command if help extensions is not loaded."""
if 'bot.exts.evergreen.help' in ctx.bot.extensions:
help_command = ctx.bot.get_command('help')
await ctx.invoke(help_command, *commands)
return
await ctx.send_help(''.join(commands))
EXTENSIONS = frozenset(walk_extensions())
|