forked from sass/tipibot
Refator eco code. Split into modules
This commit is contained in:
40
bot.py
40
bot.py
@@ -244,7 +244,6 @@ async def _award_exp(interaction: discord.Interaction, amount: int) -> None:
|
||||
pass
|
||||
|
||||
|
||||
@tree.interaction_check
|
||||
async def _log_command(interaction: discord.Interaction) -> bool:
|
||||
"""Log every slash command invocation and enforce allowed-channel restriction."""
|
||||
if interaction.command:
|
||||
@@ -287,6 +286,11 @@ async def _log_command(interaction: discord.Interaction) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
# CommandTree.interaction_check is a method meant to be overridden, not a
|
||||
# decorator - `@tree.interaction_check` silently registers nothing.
|
||||
tree.interaction_check = _log_command
|
||||
|
||||
|
||||
def _load_bday_log() -> dict:
|
||||
try:
|
||||
return json.loads(_BDAY_LOG.read_text(encoding="utf-8"))
|
||||
@@ -337,6 +341,8 @@ async def before_birthday_daily():
|
||||
# ---------------------------------------------------------------------------
|
||||
_presence_index = 0
|
||||
_economy_count: int = 0
|
||||
_economy_count_fetched: float = 0.0
|
||||
_ECONOMY_COUNT_TTL = 300 # seconds; the player count changes rarely
|
||||
_PRESENCES: list = [
|
||||
lambda g: discord.Activity(
|
||||
type=discord.ActivityType.watching,
|
||||
@@ -358,12 +364,14 @@ _PRESENCES: list = [
|
||||
|
||||
@tasks.loop(seconds=20)
|
||||
async def _rotate_presence() -> None:
|
||||
global _presence_index, _economy_count
|
||||
global _presence_index, _economy_count, _economy_count_fetched
|
||||
guild = bot.get_guild(config.GUILD_ID)
|
||||
try:
|
||||
_economy_count = await pb_client.count_records()
|
||||
except Exception as e:
|
||||
log.warning("Presence: failed to fetch economy count: %s", e)
|
||||
if time.monotonic() - _economy_count_fetched > _ECONOMY_COUNT_TTL:
|
||||
try:
|
||||
_economy_count = await pb_client.count_records()
|
||||
_economy_count_fetched = time.monotonic()
|
||||
except Exception as e:
|
||||
log.warning("Presence: failed to fetch economy count: %s", e)
|
||||
activity = _PRESENCES[_presence_index % len(_PRESENCES)](guild)
|
||||
await bot.change_presence(status=discord.Status.online, activity=activity)
|
||||
_presence_index += 1
|
||||
@@ -382,6 +390,7 @@ async def on_ready():
|
||||
"""Load sheet data and sync slash commands on startup."""
|
||||
log.info("Logged in as %s (ID: %s)", bot.user, bot.user.id)
|
||||
economy.set_house(bot.user.id)
|
||||
_log_config_gaps()
|
||||
|
||||
# PocketBase silently drops writes to fields missing from the collection
|
||||
# schema, so surface any drift loudly instead of letting features no-op.
|
||||
@@ -882,6 +891,25 @@ async def on_app_command_error(interaction: discord.Interaction, error: app_comm
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
def _log_config_gaps() -> None:
|
||||
"""One startup line per unset optional config, so 'why doesn't X work on
|
||||
prod' starts with a log grep instead of guesswork."""
|
||||
gaps: list[str] = []
|
||||
if IS_DEV_PROFILE:
|
||||
if not config.SHEET_ID:
|
||||
gaps.append("SHEET_ID not set - member sync/birthdays have no sheet")
|
||||
if not config.BIRTHDAY_CHANNEL_ID:
|
||||
gaps.append("BIRTHDAY_CHANNEL_ID not set - birthday pings disabled")
|
||||
if not config.PB_ADMIN_EMAIL or not config.PB_ADMIN_PASSWORD:
|
||||
gaps.append("PB_ADMIN_EMAIL/PB_ADMIN_PASSWORD not set - economy database writes will fail")
|
||||
if not config.BOT_ADMIN_ROLES:
|
||||
gaps.append("DISCORD_ADMIN_ROLES not set - role-based bot-admin checks disabled")
|
||||
for gap in gaps:
|
||||
log.warning("Config: %s", gap)
|
||||
if not gaps:
|
||||
log.info("Config: all expected settings present for profile '%s'", config.BOT_PROFILE)
|
||||
|
||||
|
||||
def _log_sync_result(member: discord.Member, result: SyncResult):
|
||||
if result.nickname_changed:
|
||||
log.info(" → Nickname set for %s", member)
|
||||
|
||||
Reference in New Issue
Block a user