|
| 1 | +#!/bin/bash |
| 2 | +# /usr/libexec/mios/mios-discord-status |
| 3 | +# |
| 4 | +# Self-check for the Hermes-Agent Discord integration. The agent's |
| 5 | +# `discord_send_message` tool quietly returns "channel not configured" |
| 6 | +# or "unauthorized" when something is wrong; this helper exposes the |
| 7 | +# actual state so the operator (and the agent, via terminal:) can |
| 8 | +# diagnose without re-deriving the curl pipeline each time. |
| 9 | +# |
| 10 | +# Checks: |
| 11 | +# 1. /etc/mios/hermes/discord.env present + readable |
| 12 | +# 2. DISCORD_BOT_TOKEN parses, length is sane |
| 13 | +# 3. GET /users/@me -> bot username + id (proves token live) |
| 14 | +# 4. GET /users/@me/guilds -> guild count (proves bot is in a guild) |
| 15 | +# 5. MIOS_DISCORD_DEFAULT_CHANNEL + _GUILD env values reported |
| 16 | +# 6. /var/lib/mios/hermes/channel_directory.json mtime (last refresh) |
| 17 | +# |
| 18 | +# Operator directive 2026-05-17: "Fix discord too" -- the chat above |
| 19 | +# showed the agent couldn't tell whether discord was up. SOUL.md now |
| 20 | +# routes diagnostic questions here. |
| 21 | +# |
| 22 | +# Exit codes: |
| 23 | +# 0 = token works, bot in >=1 guild, default channel/guild set |
| 24 | +# 1 = soft failure (token works but config incomplete) |
| 25 | +# 2 = hard failure (token missing, invalid, or API unreachable) |
| 26 | + |
| 27 | +set -uo pipefail |
| 28 | + |
| 29 | +ENV_FILE="${MIOS_DISCORD_ENV:-/etc/mios/hermes/discord.env}" |
| 30 | +DIRECTORY="${MIOS_HERMES_HOME:-/var/lib/mios/hermes}/channel_directory.json" |
| 31 | +API="https://discord.com/api/v10" |
| 32 | + |
| 33 | +red() { printf '\033[31m%s\033[0m\n' "$*"; } |
| 34 | +green() { printf '\033[32m%s\033[0m\n' "$*"; } |
| 35 | +yellow() { printf '\033[33m%s\033[0m\n' "$*"; } |
| 36 | +hdr() { printf '\n=== %s ===\n' "$*"; } |
| 37 | + |
| 38 | +# --- 1. env file --------------------------------------------------- |
| 39 | +hdr "discord.env" |
| 40 | +if [ ! -r "$ENV_FILE" ]; then |
| 41 | + red " $ENV_FILE not readable by uid $(id -u)" |
| 42 | + exit 2 |
| 43 | +fi |
| 44 | +echo " $(ls -la "$ENV_FILE")" |
| 45 | + |
| 46 | +# Parse via grep (NOT `source`) so comment backticks don't trip bash. |
| 47 | +TOKEN=$(grep -E '^DISCORD_BOT_TOKEN=' "$ENV_FILE" | head -1 | cut -d= -f2-) |
| 48 | +DEFAULT_CHANNEL=$(grep -E '^MIOS_DISCORD_DEFAULT_CHANNEL=' "$ENV_FILE" | head -1 | cut -d= -f2-) |
| 49 | +DEFAULT_GUILD=$(grep -E '^MIOS_DISCORD_DEFAULT_GUILD=' "$ENV_FILE" | head -1 | cut -d= -f2-) |
| 50 | + |
| 51 | +if [ -z "$TOKEN" ]; then |
| 52 | + red " DISCORD_BOT_TOKEN not set in $ENV_FILE" |
| 53 | + exit 2 |
| 54 | +fi |
| 55 | +echo " DISCORD_BOT_TOKEN: present (${#TOKEN} chars)" |
| 56 | +echo " MIOS_DISCORD_DEFAULT_CHANNEL: ${DEFAULT_CHANNEL:-<unset>}" |
| 57 | +echo " MIOS_DISCORD_DEFAULT_GUILD: ${DEFAULT_GUILD:-<unset>}" |
| 58 | + |
| 59 | +# --- 2. token live? ------------------------------------------------ |
| 60 | +hdr "GET $API/users/@me" |
| 61 | +TMP=$(mktemp) |
| 62 | +HTTP=$(curl -sS --max-time 8 -o "$TMP" -w '%{http_code}' \ |
| 63 | + -H "Authorization: Bot $TOKEN" "$API/users/@me" 2>/dev/null || echo "000") |
| 64 | +case "$HTTP" in |
| 65 | + 200) |
| 66 | + USERNAME=$(python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get('username',''))" "$TMP") |
| 67 | + USERID=$(python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get('id',''))" "$TMP") |
| 68 | + green " OK $HTTP -- bot username='$USERNAME' id='$USERID'" |
| 69 | + ;; |
| 70 | + 401) |
| 71 | + red " $HTTP -- token rejected (rotate via Discord Developer Portal -> Bot tab -> Reset Token, paste into $ENV_FILE)" |
| 72 | + rm -f "$TMP"; exit 2 ;; |
| 73 | + 000|"") |
| 74 | + red " $HTTP -- no response (DNS failure? network down? proxy?)" |
| 75 | + rm -f "$TMP"; exit 2 ;; |
| 76 | + *) |
| 77 | + red " $HTTP -- $(head -c 200 "$TMP")" |
| 78 | + rm -f "$TMP"; exit 2 ;; |
| 79 | +esac |
| 80 | +rm -f "$TMP" |
| 81 | + |
| 82 | +# --- 3. guild membership ------------------------------------------ |
| 83 | +hdr "GET $API/users/@me/guilds" |
| 84 | +TMP=$(mktemp) |
| 85 | +HTTP=$(curl -sS --max-time 8 -o "$TMP" -w '%{http_code}' \ |
| 86 | + -H "Authorization: Bot $TOKEN" "$API/users/@me/guilds" 2>/dev/null || echo "000") |
| 87 | +if [ "$HTTP" = "200" ]; then |
| 88 | + COUNT=$(python3 -c "import json,sys; print(len(json.load(open(sys.argv[1]))))" "$TMP") |
| 89 | + green " OK $HTTP -- bot is in $COUNT guild(s):" |
| 90 | + python3 -c " |
| 91 | +import json,sys |
| 92 | +for g in json.load(open(sys.argv[1])): |
| 93 | + print(' -', g.get('name'), '(id=' + g.get('id','?') + ')') |
| 94 | +" "$TMP" |
| 95 | +else |
| 96 | + yellow " $HTTP -- $(head -c 200 "$TMP")" |
| 97 | +fi |
| 98 | +rm -f "$TMP" |
| 99 | + |
| 100 | +# --- 4. channel_directory.json freshness --------------------------- |
| 101 | +hdr "channel_directory.json" |
| 102 | +if [ -r "$DIRECTORY" ]; then |
| 103 | + echo " $(ls -la "$DIRECTORY")" |
| 104 | + AGE_S=$(( $(date +%s) - $(stat -c %Y "$DIRECTORY") )) |
| 105 | + AGE_MIN=$(( AGE_S / 60 )) |
| 106 | + if [ "$AGE_MIN" -gt 1440 ]; then |
| 107 | + yellow " age: ${AGE_MIN}min -- stale (>24h). Restart hermes-agent.service to refresh." |
| 108 | + else |
| 109 | + echo " age: ${AGE_MIN}min" |
| 110 | + fi |
| 111 | + DCOUNT=$(python3 -c "import json; print(len(json.load(open('$DIRECTORY')).get('platforms',{}).get('discord',[])))" 2>/dev/null || echo "?") |
| 112 | + echo " discord entries cached: $DCOUNT" |
| 113 | +else |
| 114 | + yellow " $DIRECTORY not present yet (hermes hasn't fetched the directory yet)" |
| 115 | +fi |
| 116 | + |
| 117 | +# --- 5. default channel/guild consistency -------------------------- |
| 118 | +hdr "default channel/guild check" |
| 119 | +if [ -z "$DEFAULT_CHANNEL" ] || [ -z "$DEFAULT_GUILD" ]; then |
| 120 | + yellow " Default channel/guild not set in $ENV_FILE -- discord_send_message" |
| 121 | + yellow " will require an explicit 'channel' arg every call." |
| 122 | + exit 1 |
| 123 | +fi |
| 124 | +green " default channel=$DEFAULT_CHANNEL guild=$DEFAULT_GUILD" |
| 125 | +echo |
| 126 | +green "All checks passed." |
0 commit comments