hln-a/hlna_bot.py
2023-03-10 13:40:50 +03:00

168 lines
5.9 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import errno
import time
import sys
import os
import telebot
from telebot import types
import subprocess
import yaml
import colorama
import hlna
list_config = hlna.list_config
def start():
if not os.path.exists(f"{hlna.home_dir}/.config/hlna/bot/hlna_bot"):
key_api = input("Укажите ключ бота\n")
os.chdir(f"{hlna.home_dir}/.config/hlna/")
try:
os.mkdir("bot")
except OSError as e:
if e.errno == errno.EEXIST:
print('Каталог создан')
else:
raise
create_config(key_api)
else:
x = input("Уже есть запомненный ключа api. Запустить его? Y/n \n")
if x == "" or x.lower == 'y':
read_config()
elif x == "n":
os.remove(f"{hlna.home_dir}/.config/hlna/bot/hlna_bot")
start()
def check_status(m):
for i in list_config:
data = hlna.read_yaml(i)
hlna.print_line(data)
x = os.system(f"lsof -w -i :{data['Port']}")
n_srv = data['SessionName']
if x != 0:
hlna_bot.send_message(m.chat.id, text=f"Сервер {n_srv} не работает 🛑")
elif x == 0:
hlna_bot.send_message(m.chat.id, text=f"Сервер {n_srv} запустился 🟢")
def read_config():
with open(f"{hlna.home_dir}/.config/hlna/bot/hlna_bot", "r") as yamlfile:
data = yaml.load(yamlfile, Loader=yaml.FullLoader)
return data[0]
def create_config(key_api):
config_hlna_bot = [
{
'key_api': key_api,
}
]
with open(f"{hlna.home_dir}/.config/hlna/bot/hlna_bot", 'w') as yamlfile:
yaml.dump(config_hlna_bot, yamlfile)
print(colorama.Fore.GREEN + "Конфиг бота создан" + colorama.Style.RESET_ALL)
start()
data = read_config()
k_api = data['key_api']
hlna_bot = telebot.TeleBot(f"{k_api}", parse_mode=None)
@hlna_bot.message_handler(commands=["start"])
def m_mainmenu(m):
menu = types.ReplyKeyboardMarkup(resize_keyboard=True)
k_start = types.KeyboardButton("🦖 Старт")
k_stop = types.KeyboardButton("🦇 Выключить")
menu.add(k_start, k_stop)
k_restart = types.KeyboardButton("🦕 Перезагрузить")
k_update = types.KeyboardButton("🔸 Обновить")
menu.add(k_restart, k_update)
k_status = types.KeyboardButton("🧾 Статус")
menu.add(k_status)
k_mods = types.KeyboardButton("⏬ Установка модов")
menu.add(k_mods)
hlna_bot.send_message(m.chat.id, text="Привет, {0.first_name}! Это панель управления сервером".format(m.from_user),
reply_markup=menu)
@hlna_bot.message_handler(content_types=["text"])
def m_chose(m):
if m.text.strip() == '🦖 Старт':
if list_config == []:
menu_install = types.ReplyKeyboardMarkup(resize_keyboard=True)
k_install = types.KeyboardButton("Установить")
k_back = types.KeyboardButton("Назад")
menu_install.add(k_back, k_install)
hlna_bot.send_message(m.chat.id, text="{0.first_name}, ниодна карта не установлена".format(m.from_user),
reply_markup=menu_install)
hlna_bot.register_next_step_handler(m, m_install)
else:
hlna.read_yaml(list_config[-1], flag=True)
menu_start = types.ReplyKeyboardMarkup(resize_keyboard=True)
for i in list_config:
k_mapname = types.KeyboardButton(i)
menu_start.add(k_mapname)
k_back = types.KeyboardButton("Назад")
menu_start.add(k_back)
hlna_bot.send_message(m.chat.id, text="{0.first_name}, выбери карту".format(m.from_user), reply_markup=menu_start)
elif m.text.strip() == '🧾 Статус':
check_status(m)
elif m.text.strip() == 'Назад':
m_mainmenu(m)
for i in list_config:
if m.text.strip() == f'{i}':
hlna.print_line(f"Старт карты {i}")
hlna.start()
# @hlna_bot.message_handler(content_types=["text"])
# def m_mapstart(m):
# hlna.print_line("Старт карты")
# for i in list_config:
# if m.text.strip() == f'{i}':
# hlna.print_line(f"Старт карты {i}")
# hlna.start()
def m_install(m):
if m.text.strip() == 'Установить':
hlna_bot.send_message(m.chat.id, subprocess.check_output(hlna.config(), shell=True))
print("Бот запущен")
hlna_bot.polling(none_stop=True, interval=0, timeout=999)
# subprocess.run(["arkmanager", "start", "--alwaysrestart", "@all"])
# elif m.text.strip() == '🦇 Выключить':
# subprocess.run(["arkmanager", "stop", "@all"])
# elif m.text.strip() == '🦕 Перезагрузить':
# subprocess.run(["arkmanager","restart", "--alwaysrestart", "--warn", "@all"])
# elif (m.text.strip() == '🔸 Обновить'):
# subprocess.run(["arkmanager", "update", "--update-mods", "@all"])
#
#
# elif (m.text.strip() == '⏬ Установка модов'):
# bot.send_message(m.from_user.id, "Введите id модификации")
# bot.register_next_step_handler(m, mods)
# else:
# pass
# else:
# if m.text.strip() == 'Zxx843Aj':
# th = Thread(target=check, args=(m,))
# th.start()
# id.append(m.chat.id)
# bot.send_message(m.chat.id, text="{0.first_name} вы авторизованы, нажмите /start для работы с ботом".format(m.from_user))
# return id
# def mods(m):
# id = m.text.strip()
# subprocess.run(["arkmanager", "installmods", id, "@all"])
# subprocess.run(["arkmanager", "enablemod", id, "@all"])