mirror of
https://github.com/b1og3n/ASFTGCONTROLBOT.git
synced 2026-08-01 17:57:52 +00:00
refactor(bot): modularize bot code
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import html
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from bot.api.asf import get_asf_status
|
||||
from bot.constants import GAMES
|
||||
|
||||
|
||||
def get_currency_name(currency_id: int) -> str:
|
||||
if currency_id == 1:
|
||||
return "USD"
|
||||
if currency_id == 3:
|
||||
return "EUR"
|
||||
if currency_id == 5:
|
||||
return "RUB"
|
||||
if currency_id == 18:
|
||||
return "UAH"
|
||||
if currency_id == 6:
|
||||
return "PLN"
|
||||
if currency_id == 0:
|
||||
return ""
|
||||
return "???"
|
||||
|
||||
|
||||
def get_inventory_icon(desc: dict) -> str:
|
||||
tags = desc.get("tags", [])
|
||||
for tag in tags:
|
||||
category = tag.get("category", "")
|
||||
name = tag.get("localized_tag_name", "")
|
||||
if category == "item_class":
|
||||
if "Trading Card" in name:
|
||||
return "🃏"
|
||||
if "Emoticon" in name:
|
||||
return "😀"
|
||||
if "Profile Background" in name:
|
||||
return "🖼"
|
||||
if "Booster Pack" in name:
|
||||
return "📦"
|
||||
if "Steam Gems" in name:
|
||||
return "💎"
|
||||
if "Gift" in name:
|
||||
return "🎁"
|
||||
item_type = desc.get("type", "").lower()
|
||||
if "knife" in item_type:
|
||||
return "🔪"
|
||||
if "pistol" in item_type:
|
||||
return "🔫"
|
||||
if "rifle" in item_type:
|
||||
return "🎯"
|
||||
if "graffiti" in item_type:
|
||||
return "🎨"
|
||||
if "music kit" in item_type:
|
||||
return "🎵"
|
||||
return "📦"
|
||||
|
||||
|
||||
def get_uptime(start_time_str: str) -> str:
|
||||
start_time_str = start_time_str[:26] + "Z"
|
||||
start_time = datetime.fromisoformat(start_time_str.replace("Z", "+00:00"))
|
||||
now = datetime.now(timezone.utc)
|
||||
delta = now - start_time
|
||||
days = delta.days
|
||||
hours, remainder = divmod(delta.seconds, 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
return f"{days}д {hours}ч {minutes}м {seconds}с"
|
||||
|
||||
|
||||
def format_asf(data: dict) -> str:
|
||||
result = data.get("Result", {})
|
||||
current_version = result.get("Version")
|
||||
latest_version = result.get("LatestVersion", current_version)
|
||||
lines = [f"Версия ASF: {current_version}"]
|
||||
if current_version != latest_version:
|
||||
lines.append(f"Доступно обновление: {latest_version}")
|
||||
memory_kb = result.get("MemoryUsage", 0)
|
||||
memory_mb = memory_kb / 1024
|
||||
lines.append(f"Используется памяти: {memory_mb:.2f} MB")
|
||||
lines.append(f"Работает: {get_uptime(result.get('ProcessStartTime'))}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def get_bot_status_icon(bot: dict) -> str:
|
||||
online = bot.get("IsConnectedAndLoggedOn", False)
|
||||
farming = bot.get("CardsFarmer", {}).get("NowFarming", False)
|
||||
idle_games = bot.get("BotConfig", {}).get("GamesPlayedWhileIdle", [])
|
||||
loaded = bool(bot.get("BotName")) and bool(bot.get("SteamID"))
|
||||
if not loaded:
|
||||
return "🔄"
|
||||
if farming:
|
||||
return "🎴"
|
||||
if idle_games and online:
|
||||
return "⌚"
|
||||
if online:
|
||||
return "🟢"
|
||||
return "🔴"
|
||||
|
||||
|
||||
def format_bot_ui(bot: dict) -> str:
|
||||
online = get_bot_status_icon(bot)
|
||||
now_farming = bot.get("CardsFarmer", {}).get("NowFarming")
|
||||
status_line = "🃏 Идет фарм карточек" if now_farming else "🃏 Карточки не фармится"
|
||||
time = bot.get("CardsFarmer", {}).get("TimeRemaining", "00:00:00")
|
||||
games_to_farm = len(bot.get("CardsFarmer", {}).get("GamesToFarm", []))
|
||||
twofa = "есть" if bot.get("HasMobileAuthenticator") else "нет"
|
||||
balance = bot.get("WalletBalance", 0) / 100
|
||||
currency = get_currency_name(bot.get("WalletCurrency", 0))
|
||||
redeem = bot.get("GamesToRedeemInBackgroundCount", 0)
|
||||
steam_id = bot.get("SteamID")
|
||||
nickname = html.escape(str(bot.get("Nickname") or ""))
|
||||
profile_url = f"https://steamcommunity.com/profiles/{steam_id}"
|
||||
text = (
|
||||
f"{online} {bot.get('BotName') or 'Загрузка...'}"
|
||||
f'(<a href="{profile_url}">{nickname}</a>)\n'
|
||||
f"{status_line}\n"
|
||||
)
|
||||
idle_games = bot.get("BotConfig", {}).get("GamesPlayedWhileIdle", [])
|
||||
if idle_games:
|
||||
names = [GAMES.get(game, str(game)) for game in idle_games]
|
||||
if bot.get("IsConnectedAndLoggedOn"):
|
||||
idle_text = f"⌚ Накрутка часов: включена ({', '.join(names)})"
|
||||
else:
|
||||
idle_text = (
|
||||
f"⌚ Накрутка часов: включена ({', '.join(names)}) "
|
||||
"применится после запуска"
|
||||
)
|
||||
text += f"{idle_text}\n"
|
||||
else:
|
||||
text += "⌚ Накрутка часов: выключена\n"
|
||||
text += (
|
||||
f"🆔 : <code>{steam_id}</code>\n"
|
||||
f"🔐 2FA: {twofa}\n\n"
|
||||
f"💰 Баланс: {balance:.2f} {currency}\n"
|
||||
)
|
||||
if redeem > 0:
|
||||
text += f"\nКлючей в очереди: {redeem}\n"
|
||||
if now_farming:
|
||||
text += f"\nОсталось: {time}\n"
|
||||
if games_to_farm > 0:
|
||||
text += f"В очереди игр: {games_to_farm}\n"
|
||||
return text
|
||||
|
||||
|
||||
def get_asf_status_text() -> str:
|
||||
response = get_asf_status()
|
||||
if response.status_code != 200:
|
||||
return f"Ошибка API: {response.status_code}"
|
||||
data = response.json()
|
||||
if not data.get("Success"):
|
||||
return "ASF вернул ошибку"
|
||||
return f"<b>💻 Панель управления ASF</b>\n\n{format_asf(data)}"
|
||||
|
||||
|
||||
def get_farm_summary(bots: dict) -> str:
|
||||
total_games = 0
|
||||
total_time_seconds = 0
|
||||
for bot in bots.values():
|
||||
farmer = bot.get("CardsFarmer", {})
|
||||
if farmer.get("NowFarming"):
|
||||
games = farmer.get("CurrentGamesFarming", [])
|
||||
total_games += len(games)
|
||||
time_str = farmer.get("TimeRemaining", "00:00:00")
|
||||
hours, minutes, seconds = map(int, time_str.split(":"))
|
||||
total_time_seconds += hours * 3600 + minutes * 60 + seconds
|
||||
if total_games == 0:
|
||||
return "На аккаунтах нечего не фармиться"
|
||||
hours = total_time_seconds // 3600
|
||||
minutes = (total_time_seconds % 3600) // 60
|
||||
return (
|
||||
f"Игр фармится: {total_games}\n"
|
||||
f"Осталось времени: {hours}ч {minutes}м\n"
|
||||
f"Карт осталось: ~{total_games * 3}"
|
||||
)
|
||||
|
||||
|
||||
def format_bot(bot_data: dict) -> str:
|
||||
return json.dumps(bot_data, indent=2, ensure_ascii=False)
|
||||
@@ -0,0 +1,192 @@
|
||||
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
|
||||
from bot.ui.formatters import get_bot_status_icon
|
||||
|
||||
|
||||
def main_keyboard() -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text="🆙 Обновить", callback_data="refresh")],
|
||||
[InlineKeyboardButton(text="🤖 Боты", callback_data="bots")],
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="🔑 Активация ключей на всех аккаунтах",
|
||||
callback_data="redeem_keys",
|
||||
)
|
||||
],
|
||||
[InlineKeyboardButton(text="📁 Плагины", callback_data="plugins")],
|
||||
[InlineKeyboardButton(text="💻 Консоль", callback_data="console")],
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="♻ Перезапустить ASF",
|
||||
callback_data="restart_asf_confirm",
|
||||
)
|
||||
],
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def back_keyboard(callback_data: str = "back") -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=callback_data)]
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def games_keyboard(bot_name: str, is_enabled: bool) -> InlineKeyboardMarkup:
|
||||
text = "Остановить накрутку CS2" if is_enabled else "⌚ Накрутить часы CS2"
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text=text, callback_data=f"farm|{bot_name}|730")],
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=f"bot_{bot_name}")],
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def bots_keyboard(bots: dict) -> InlineKeyboardMarkup:
|
||||
keyboard = []
|
||||
for name, bot in bots.items():
|
||||
status = get_bot_status_icon(bot)
|
||||
loaded = bool(bot.get("BotName")) and bool(bot.get("SteamID"))
|
||||
callback_data = f"bot_{name}" if loaded else "bot_loading"
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text=f"{status} {name}", callback_data=callback_data)]
|
||||
)
|
||||
keyboard.append([InlineKeyboardButton(text="🔙 Назад", callback_data="back")])
|
||||
return InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def bot_details_keyboard(
|
||||
bot_name: str, has_mobile_authenticator: bool
|
||||
) -> InlineKeyboardMarkup:
|
||||
keyboard = []
|
||||
if has_mobile_authenticator:
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="🔐 2FA", callback_data=f"2fa_{bot_name}")]
|
||||
)
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="⌚ Накрутка часов", callback_data=f"games_{bot_name}")]
|
||||
)
|
||||
keyboard.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="🔑 Активировать ключ",
|
||||
callback_data=f"redeem_bot_{bot_name}",
|
||||
)
|
||||
]
|
||||
)
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="📦 Инвентарь", callback_data=f"inventory_{bot_name}")]
|
||||
)
|
||||
keyboard.append([InlineKeyboardButton(text="🔙 Назад", callback_data="bots")])
|
||||
return InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def twofa_keyboard(bot_name: str, has_confirmations: bool) -> InlineKeyboardMarkup:
|
||||
keyboard = []
|
||||
if has_confirmations:
|
||||
keyboard.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="Подтверждения",
|
||||
callback_data=f"confirm_list_{bot_name}",
|
||||
)
|
||||
]
|
||||
)
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=f"bot_{bot_name}")]
|
||||
)
|
||||
return InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def confirmations_keyboard(bot_name: str) -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="Подтвердить всё",
|
||||
callback_data=f"confirm_all_{bot_name}",
|
||||
)
|
||||
],
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=f"2fa_{bot_name}")],
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def confirm_action_keyboard(confirm_data: str, cancel_data: str) -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text="Да", callback_data=confirm_data)],
|
||||
[InlineKeyboardButton(text="Нет", callback_data=cancel_data)],
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def inventory_menu_keyboard(
|
||||
bot_name: str, cs2_count: int, steam_count: int
|
||||
) -> InlineKeyboardMarkup:
|
||||
keyboard = []
|
||||
if cs2_count:
|
||||
keyboard.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text=f"CS2 ({cs2_count})",
|
||||
callback_data=f"inv_cs2_{bot_name}",
|
||||
)
|
||||
]
|
||||
)
|
||||
if steam_count:
|
||||
keyboard.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text=f"Steam ({steam_count})",
|
||||
callback_data=f"inv_steam_{bot_name}",
|
||||
)
|
||||
]
|
||||
)
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=f"bot_{bot_name}")]
|
||||
)
|
||||
return InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def inventory_page_keyboard(
|
||||
inventory_type: str, bot_name: str, page: int, total_pages: int
|
||||
) -> InlineKeyboardMarkup:
|
||||
nav_buttons = []
|
||||
if page > 0:
|
||||
nav_buttons.append(
|
||||
InlineKeyboardButton(
|
||||
text="◀",
|
||||
callback_data=f"invpage|{inventory_type}|{bot_name}|{page - 1}",
|
||||
)
|
||||
)
|
||||
if page < total_pages - 1:
|
||||
nav_buttons.append(
|
||||
InlineKeyboardButton(
|
||||
text="▶",
|
||||
callback_data=f"invpage|{inventory_type}|{bot_name}|{page + 1}",
|
||||
)
|
||||
)
|
||||
keyboard = []
|
||||
if nav_buttons:
|
||||
keyboard.append(nav_buttons)
|
||||
keyboard.append(
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data=f"inventory_{bot_name}")]
|
||||
)
|
||||
return InlineKeyboardMarkup(inline_keyboard=keyboard)
|
||||
|
||||
|
||||
def console_keyboard() -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text="🔙 Назад", callback_data="console_exit")]
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def delete_message_keyboard() -> InlineKeyboardMarkup:
|
||||
return InlineKeyboardMarkup(
|
||||
inline_keyboard=[[InlineKeyboardButton(text="Удалить", callback_data="delete_msg")]]
|
||||
)
|
||||
Reference in New Issue
Block a user