Skip to content

Commit

Permalink
settings: move packages to settings (Ballsdex-Team#472)
Browse files Browse the repository at this point in the history
* Move PACKAGES to Settings

* Switch to absolute path

* Address changes

---------

Co-authored-by: Auguste Charpentier <[email protected]>
  • Loading branch information
Dotsian and laggron42 authored Dec 3, 2024
1 parent 0cf496a commit 2078b26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ballsdex/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
log = logging.getLogger("ballsdex.core.bot")
http_counter = Histogram("discord_http_requests", "HTTP requests", ["key", "code"])

PACKAGES = ["config", "players", "countryballs", "info", "admin", "trade", "balls"]


def owner_check(ctx: commands.Context[BallsDexBot]):
return ctx.bot.is_owner(ctx.author)
Expand Down Expand Up @@ -304,13 +302,15 @@ async def on_ready(self):
await self.add_cog(Dev())

loaded_packages = []
for package in PACKAGES:
for package in settings.packages:
package_name = package.replace("ballsdex.packages.", "")

try:
await self.load_extension("ballsdex.packages." + package)
await self.load_extension(package)
except Exception:
log.error(f"Failed to load package {package}", exc_info=True)
log.error(f"Failed to load package {package_name}", exc_info=True)
else:
loaded_packages.append(package)
loaded_packages.append(package_name)
if loaded_packages:
log.info(f"Packages loaded: {', '.join(loaded_packages)}")
else:
Expand All @@ -327,7 +327,7 @@ async def on_ready(self):
else:
log.info("No command to sync.")

if "admin" in PACKAGES:
if "ballsdex.packages.admin" in settings.packages:
for guild_id in settings.admin_guild_ids:
guild = self.get_guild(guild_id)
if not guild:
Expand Down
39 changes: 39 additions & 0 deletions ballsdex/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Settings:
List of roles that have full access to the /admin command
admin_role_ids: list[int]
List of roles that have partial access to the /admin command (only blacklist and guilds)
packages: list[str]
List of packages the bot will load upon startup
spawn_manager: str
Python path to a class implementing `BaseSpawnManager`, handling cooldowns and anti-cheat
"""
Expand Down Expand Up @@ -91,6 +93,8 @@ class Settings:
team_owners: bool = False
co_owners: list[int] = field(default_factory=list)

packages: list[str] = field(default_factory=list)

# metrics and prometheus
prometheus_enabled: bool = False
prometheus_host: str = "0.0.0.0"
Expand Down Expand Up @@ -139,6 +143,16 @@ def read_settings(path: "Path"):
settings.max_attack_bonus = content.get("max-attack-bonus", 20)
settings.max_health_bonus = content.get("max-health-bonus", 20)

settings.packages = content.get("packages") or [
"ballsdex.packages.admin",
"ballsdex.packages.balls",
"ballsdex.packages.config",
"ballsdex.packages.countryballs",
"ballsdex.packages.info",
"ballsdex.packages.players",
"ballsdex.packages.trade",
]

settings.spawn_manager = content.get(
"spawn-manager", "ballsdex.packages.countryballs.spawn.SpawnManager"
)
Expand Down Expand Up @@ -227,6 +241,16 @@ def write_default_settings(path: "Path"):
# a list of IDs that must be considered owners in addition to the application/team owner
co-owners:
# list of packages that will be loaded
packages:
- ballsdex.packages.admin
- ballsdex.packages.balls
- ballsdex.packages.config
- ballsdex.packages.countryballs
- ballsdex.packages.info
- ballsdex.packages.players
- ballsdex.packages.trade
# prometheus metrics collection, leave disabled if you don't know what this is
prometheus:
enabled: false
Expand All @@ -247,6 +271,7 @@ def update_settings(path: "Path"):
add_max_attack = "max-attack-bonus" not in content
add_max_health = "max-health-bonus" not in content
add_plural_collectible = "plural-collectible-name" not in content
add_packages = "packages:" not in content
add_spawn_manager = "spawn-manager" not in content

for line in content.splitlines():
Expand Down Expand Up @@ -296,6 +321,19 @@ def update_settings(path: "Path"):
plural-collectible-name: countryballs
"""

if add_packages:
content += """
# list of packages that will be loaded
packages:
- ballsdex.packages.admin
- ballsdex.packages.balls
- ballsdex.packages.config
- ballsdex.packages.countryballs
- ballsdex.packages.info
- ballsdex.packages.players
- ballsdex.packages.trade
"""

if add_spawn_manager:
content += """
# define a custom spawn manager implementation
Expand All @@ -310,6 +348,7 @@ def update_settings(path: "Path"):
add_max_attack,
add_max_health,
add_plural_collectible,
add_packages,
add_spawn_manager,
)
):
Expand Down

0 comments on commit 2078b26

Please sign in to comment.