69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
# modules/common/boot_notice.py
|
|
import os
|
|
import discord
|
|
import aiohttp
|
|
import xml.etree.ElementTree as ET
|
|
from modules.common.settings import cfg
|
|
|
|
async def _fetch_latest_subject_sha(rss_url: str) -> tuple[str | None, str | None]:
|
|
"""Best-effort: read latest commit subject + short sha from a Gitea RSS feed."""
|
|
try:
|
|
timeout = aiohttp.ClientTimeout(total=8)
|
|
async with aiohttp.ClientSession(timeout=timeout) as sess:
|
|
async with sess.get(rss_url) as resp:
|
|
if resp.status != 200:
|
|
return None, None
|
|
text = await resp.text()
|
|
root = ET.fromstring(text)
|
|
item = root.find('./channel/item')
|
|
if item is None:
|
|
return None, None
|
|
title = (item.findtext('title') or '').strip()
|
|
link = (item.findtext('link') or '').strip()
|
|
sha = link.rsplit('/commit/', 1)[-1][:7] if '/commit/' in link else None
|
|
return (title or None), sha
|
|
except Exception:
|
|
return None, None
|
|
|
|
async def post_boot_notice(bot):
|
|
"""
|
|
Posts a boot status message to the configured modlog channel.
|
|
Primary source: SHAI_BOOT_* env vars set by the wrapper.
|
|
Fallback: if absent, and SHAI_REPO_RSS is set, show the latest commit subject.
|
|
"""
|
|
status = os.getenv("SHAI_BOOT_STATUS", "").strip() # 'fetched_new' | 'cached_no_update' | 'cache_only_error' | ''
|
|
desc = os.getenv("SHAI_BOOT_DESC", "").strip()
|
|
old_v = os.getenv("SHAI_BOOT_OLD", "").strip()
|
|
new_v = os.getenv("SHAI_BOOT_NEW", "").strip()
|
|
|
|
line = None
|
|
if status == "fetched_new":
|
|
line = f"Successfully fetched, cached, and booted new version: v{old_v or '0.0.0.0'} → v{new_v}"
|
|
elif status == "cached_no_update":
|
|
line = f"Successfully booted from cached version: v{new_v}. No new update found"
|
|
elif status == "cache_only_error":
|
|
line = f"Successfully booted from cached version: v{new_v}. Program repository not accessible!"
|
|
|
|
if not line:
|
|
return # nothing to say
|
|
|
|
# Read modlog channel from ENV/INI via helper
|
|
modlog_channel_id = cfg(bot).int('modlog_channel_id', 0)
|
|
if not modlog_channel_id:
|
|
return
|
|
|
|
# Find channel across guilds
|
|
ch = None
|
|
for g in bot.guilds:
|
|
ch = g.get_channel(modlog_channel_id)
|
|
if ch:
|
|
break
|
|
if not ch:
|
|
return
|
|
|
|
try:
|
|
msg = line if not desc else f"{line}\n_{desc}_"
|
|
await ch.send(msg, allowed_mentions=discord.AllowedMentions.none())
|
|
except Exception:
|
|
pass
|