Skip to content

PyDrocsid.config

Config

Global bot configuration

Source code in PyDrocsid/config.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Config:
    """Global bot configuration"""

    # bot information
    NAME: str
    VERSION: str

    # repository information
    REPO_OWNER: str
    REPO_NAME: str
    REPO_LINK: str
    REPO_ICON: str

    # pydrocsid information
    DOCUMENTATION_URL: str
    DISCORD_INVITE: str

    # developers
    AUTHOR: tuple[int, str]
    CONTRIBUTORS: Counter[tuple[int, str]] = Counter(
        {
            Contributor.Defelo: 1000,
            Contributor.TNT2k: 100,
            Contributor.wolflu: 50,
            Contributor.MaxiHuHe04: 10,
            Contributor.ce_phox: 10,
            Contributor.DELTA: 10,
        }
    )

    ROLES: dict[str, tuple[str, bool]]

    # permissions and permission levels
    PERMISSION_LEVELS: Type[BasePermissionLevel]
    DEFAULT_PERMISSION_LEVEL: BasePermissionLevel
    DEFAULT_PERMISSION_OVERRIDES: dict[str, dict[str, BasePermissionLevel]] = {}
    TEAMLER_LEVEL: BasePermissionLevel

    ENABLED_COG_PACKAGES: set[str] = {"PyDrocsid"}

Contributor

Collection of all contributors. Each contributor is a (discord_id, github_id) tuple.

Source code in PyDrocsid/config.py
20
21
22
23
24
25
26
27
28
class Contributor:
    """Collection of all contributors. Each contributor is a (discord_id, github_id) tuple."""

    Defelo = (370876111992913922, "MDQ6VXNlcjQxNzQ3NjA1")
    TNT2k = (212866839083089921, "MDQ6VXNlcjQ0MzQ5NzUw")
    wolflu = (339062431131369472, "MDQ6VXNlcjYwMDQ4NTY1")
    MaxiHuHe04 = (302365095688798209, "MDQ6VXNlcjEyOTEzNTE4")
    ce_phox = (306774624090456075, "MDQ6VXNlcjQwNTE2OTkx")
    DELTA = (158634035180994560, "MDQ6VXNlcjU4OTA2NDM3")

get_subclasses_in_enabled_packages

get_subclasses_in_enabled_packages(base: Type[T]) -> list[Type[T]]

Get all subclasses of a given base class that are defined in an enabled cog package.

Source code in PyDrocsid/config.py
72
73
74
75
76
77
def get_subclasses_in_enabled_packages(base: Type[T]) -> list[Type[T]]:
    """Get all subclasses of a given base class that are defined in an enabled cog package."""

    return [
        cls for cls in base.__subclasses__() if sys.modules[cls.__module__].__package__ in Config.ENABLED_COG_PACKAGES
    ]

load_config_file

load_config_file(path: Path) -> None

Load bot configuration from a config file.

Source code in PyDrocsid/config.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def load_config_file(path: Path) -> None:
    """Load bot configuration from a config file."""

    with path.open() as file:
        config = yaml.safe_load(file)

    Config.NAME = config["name"]
    Config.AUTHOR = getattr(Contributor, config["author"])
    Config.ROLES = {k: (v["name"], v["check_assignable"]) for k, v in config["roles"].items()}

    load_repo(config)
    load_pydrocsid_info(config)
    load_language(config)
    load_permission_levels(config)

load_language

load_language(config: dict[str, Any]) -> None

Load language configuration.

Source code in PyDrocsid/config.py
102
103
104
105
106
107
def load_language(config: dict[str, Any]) -> None:
    """Load language configuration."""

    if (lang := getenv("LANGUAGE", config["default_language"])) not in config["languages"]:
        raise ValueError(f"unknown language: {lang}")
    Translations.LANGUAGE = lang

load_permission_levels

load_permission_levels(config: dict[str, Any]) -> None

Load permission level configuration.

Source code in PyDrocsid/config.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def load_permission_levels(config: dict[str, Any]) -> None:
    """Load permission level configuration."""

    permission_levels: dict[str, PermissionLevel] = {"public": PermissionLevel(0, ["public", "p"], "Public", [], [])}

    # get custom permission levels from config
    for k, v in config["permission_levels"].items():
        if v["level"] <= 0:
            raise ValueError(f"Invalid permission level: {v['level']} ({k})")

        permission_levels[k] = PermissionLevel(
            v["level"], v["aliases"], v["name"], v["if"].get("permissions", []), v["if"].get("roles", [])
        )

    # add owner permission level
    owner_level = max([pl.level for pl in permission_levels.values()], default=0) + 1
    permission_levels["owner"] = PermissionLevel(owner_level, ["owner"], "Owner", [], [])

    # sort permission levels in descending order
    permission_levels = {
        k.upper(): v
        for k, v in sorted(permission_levels.items(), key=lambda pl: pl[1].level, reverse=True)  # type: ignore
    }

    # generate PermissionLevel enum
    Config.PERMISSION_LEVELS = cast(
        Type[BasePermissionLevel], BasePermissionLevel("PermissionLevel", permission_levels)  # type: ignore
    )
    Config.PERMISSION_LEVELS._get_permission_level = classmethod(  # type: ignore
        partial(_get_permission_level, permission_levels)
    )

    Config.DEFAULT_PERMISSION_LEVEL = getattr(Config.PERMISSION_LEVELS, config["default_permission_level"].upper())
    Config.TEAMLER_LEVEL = getattr(Config.PERMISSION_LEVELS, config["teamler_level"].upper())

    # load default permission level overrides
    for cog, overrides in config.get("default_permission_overrides", {}).items():
        for permission, level in overrides.items():
            Config.DEFAULT_PERMISSION_OVERRIDES.setdefault(cog.lower(), {}).setdefault(
                permission.lower(), getattr(Config.PERMISSION_LEVELS, level.upper())
            )

load_pydrocsid_info

load_pydrocsid_info(config: dict[str, Any]) -> None

Load pydrocsid information.

Source code in PyDrocsid/config.py
95
96
97
98
99
def load_pydrocsid_info(config: dict[str, Any]) -> None:
    """Load pydrocsid information."""

    Config.DOCUMENTATION_URL = config["pydrocsid"]["documentation_url"]
    Config.DISCORD_INVITE = config["pydrocsid"]["discord_invite"]

load_repo

load_repo(config: dict[str, Any]) -> None

Load repository configuration.

Source code in PyDrocsid/config.py
86
87
88
89
90
91
92
def load_repo(config: dict[str, Any]) -> None:
    """Load repository configuration."""

    Config.REPO_OWNER = config["repo"]["owner"]
    Config.REPO_NAME = config["repo"]["name"]
    Config.REPO_LINK = f"https://github.com/{Config.REPO_OWNER}/{Config.REPO_NAME}"
    Config.REPO_ICON = config["repo"]["icon"]

load_version

load_version() -> None

Get bot version either from the VERSION file or from git describe and store it in the bot config.

Source code in PyDrocsid/config.py
80
81
82
83
def load_version() -> None:
    """Get bot version either from the VERSION file or from git describe and store it in the bot config."""

    Config.VERSION = getoutput("cat VERSION 2>/dev/null || git describe --tags --always").lstrip("v")