diff --git a/eve_orehold_watcher.py b/eve_orehold_watcher.py index 7a89cc2..930c7ad 100644 --- a/eve_orehold_watcher.py +++ b/eve_orehold_watcher.py @@ -204,6 +204,40 @@ def _discord(cp, title, message): print(f"[discord] FAILED: {e}") +def _discord_live(cp, key, title, message): + """Post-or-EDIT a single 'live' message so status updates IN PLACE (no spam). + Stores the message id per key in .live_. Skips when muted.""" + if bot_muted(cp): + return + webhook = cp.get("discord", "webhook", fallback="").strip() or DEFAULT_WEBHOOK + if not webhook: + return + idfile = os.path.join(HERE, f".live_{key}") + body = json.dumps({"content": f"**{title}**\n{message}"}).encode("utf-8") + hdr = {"Content-Type": "application/json", "User-Agent": "eve-watcher"} + mid = None + try: + mid = (open(idfile).read().strip() or None) + except Exception: + pass + if mid: # try to edit the existing message + try: + urllib.request.urlopen(urllib.request.Request( + f"{webhook}/messages/{mid}", data=body, headers=hdr, method="PATCH"), + timeout=10).read() + return + except Exception: + mid = None # message gone -> repost below + try: + resp = json.loads(urllib.request.urlopen(urllib.request.Request( + f"{webhook}?wait=true", data=body, headers=hdr, method="POST"), + timeout=10).read()) + with open(idfile, "w") as f: + f.write(str(resp.get("id", ""))) + except Exception as e: + print(f"[discord-live] {e}") + + def _ntfy(cp, title, message, priority, tags): server = cp.get("ntfy", "server", fallback="https://ntfy.sh").rstrip("/") topic = cp.get("ntfy", "topic", fallback="").strip() @@ -391,8 +425,8 @@ def run_ocr(cp): tail = f"filling {rate_m3min:,.0f} m³/min · full " else: tail = "not growing yet — lasers/drones idle?" - _discord(cp, "⛏️ Ore hold", - f"{pct:.0f}% ({cur:,}/{cap:,} m³) · {tail}") + _discord_live(cp, "hold", "⛏️ Ore hold", + f"{pct:.0f}% ({cur:,}/{cap:,} m³) · {tail}") last_status = time.time() was_mining = mining if pct < reset_pct: