game-discord-bot/lib.py

38 lines
No EOL
1.4 KiB
Python

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(game, action : str):
stat = subprocess.call(["systemctl", action, "--quiet", game['systemd']])
return not bool(stat)
def GetGameByGameSlug(slug : str):
for game in config['games']:
if game['slug'] == slug:
return game
return False
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>_"""