Files
ASF-Control-Bot/bot/services/bots.py
T

58 lines
1.8 KiB
Python

import asyncio
from bot.api.asf import get_all_bots, get_bot, save_bot_config, send_command
def is_bot_playing(bot_name: str) -> bool:
response = get_all_bots()
if response.status_code != 200:
return False
data = response.json()
bot = data.get("Result", {}).get(bot_name, {})
return bool(bot.get("PlayingBlocked", False)) or bool(bot.get("CurrentGamesPlayed", []))
def is_idle_enabled(bot_name: str, app_id: int) -> bool:
response = get_bot(bot_name)
if response.status_code != 200:
return False
data = response.json()
bot = data.get("Result", {}).get(bot_name, {})
config = bot.get("BotConfig", {})
return app_id in config.get("GamesPlayedWhileIdle", [])
def toggle_idle_game(config: dict, app_id: int) -> tuple[dict, bool]:
idle_games = config.get("GamesPlayedWhileIdle", [])
if app_id in idle_games:
idle_games.remove(app_id)
enabled = False
else:
idle_games.append(app_id)
enabled = True
config["GamesPlayedWhileIdle"] = idle_games
return config, enabled
async def reload_bot(bot_name: str) -> None:
try:
send_command(f"!reload {bot_name}")
except Exception:
pass
async def update_idle_game(bot_name: str, app_id: int) -> tuple[bool, str, bool]:
response = get_bot(bot_name)
if response.status_code != 200:
return False, "Ошибка API", False
data = response.json()
bot_data = data.get("Result", {}).get(bot_name, {})
config = bot_data.get("BotConfig", {})
config, enabled = toggle_idle_game(config, app_id)
save = save_bot_config(bot_name, config)
if save.status_code != 200:
return False, "Ошибка сохранения", enabled
asyncio.create_task(reload_bot(bot_name))
action = "Idle включен" if enabled else "Idle выключен"
return True, action, enabled