game-discord-bot/lib.py

33 lines
1.2 KiB
Python
Raw Normal View History

2024-01-01 13:10:24 +01:00
from config import config
import subprocess
def BuildGameList():
retlist = "List of currently manageable games:\n\n"
gamearray = []
for game in config['games']:
line = f"**{game['displayname']}**\n- Status: **{CheckSystemdService(game['systemd'])}**\n- Slug: `{game['slug']}`"
gamearray.append(line)
retlist = retlist + "\n".join(gamearray)
return retlist
def BuildSingleGameStatus(game):
return f"**{game['displayname']}**\n- Status: **{CheckSystemdService(game['systemd'])}**\n- Slug: `{game['slug']}`"
def CheckSystemdService(service : str):
stat = subprocess.call(["systemctl", "is-active", "--quiet", service])
return "🟢 UP" if not bool(stat) else "🔴 down"
def HandleSystemdService(service : str, action : str):
stat = subprocess.call(["systemctl", action, "--quiet", service])
return not bool(stat)
def BuildHelpList():
return """Bot has the following commands:
`help`: Get this help message
`info`: List all available games, their status and their slug
`start <slug>`: Start game with slug _<slug>_
`stop <slug>`: Stop game with slug _<slug>_
`restart <slug>`: Stop game with slug _<slug>_
`status <slug>`: Show status for game with slug _<slug>_
"""