game-discord-bot/lib.py

38 lines
1.4 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"
2024-01-01 15:08:48 +01:00
def HandleSystemdService(game, action : str):
stat = subprocess.call(["systemctl", action, "--quiet", game['systemd']])
2024-01-01 13:10:24 +01:00
return not bool(stat)
2024-01-01 15:08:48 +01:00
def GetGameByGameSlug(slug : str):
for game in config['games']:
if game['slug'] == slug:
return game
return False
2024-01-01 13:10:24 +01:00
def BuildHelpList():
return """Bot has the following commands:
`help`: Get this help message
2024-01-01 13:12:26 +01:00
`info`: List all available games, their status and their _<slug>_
2024-01-01 13:10:24 +01:00
`start <slug>`: Start game with slug _<slug>_
`stop <slug>`: Stop game with slug _<slug>_
`restart <slug>`: Stop game with slug _<slug>_
2024-01-01 13:12:26 +01:00
`status <slug>`: Show status for game with slug _<slug>_"""