From 329ab82a1e684301017ca51163e11e50acb08ab5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 13 Nov 2024 11:56:01 +0300 Subject: [PATCH] Init --- db.py | 20 +++++++++++++++++ online.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 db.py create mode 100644 online.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..79f8408 --- /dev/null +++ b/db.py @@ -0,0 +1,20 @@ +import os +import json + +if not os.path.exists('db.json'): + db = {"token": "NULL", "RCON_HOST": "NULL", "RCON_PORT": "NULL", "RCON_PASS": "NULL"} + js = json.dumps(db, indent=2) + with open("db.json", "w") as outfile: + outfile.write(js) + print('Created new db.json') + + +def read(file = 'db.json'): + with open(file, "r", encoding="utf-8") as openfile: + db = json.load(openfile) + return db + +def write(db, file = 'db.json'): + js = json.dumps(db, indent=2, ensure_ascii=False) + with open(file, "w", encoding="utf-8") as outfile: + outfile.write(js) diff --git a/online.py b/online.py new file mode 100644 index 0000000..e3d8873 --- /dev/null +++ b/online.py @@ -0,0 +1,64 @@ +from mctools import RCONClient +import telebot + +from db import * + +db = read() +TOKEN = db['token'] +RCON_HOST = db['RCON_HOST'] +RCON_PORT = db['RCON_PORT'] +RCON_PASS = db['RCON_PASS'] + +RCON = RCONClient(RCON_HOST, port=RCON_PORT) +RCON.login(RCON_PASS) + +bot = telebot.TeleBot(TOKEN) + +import re +def clear_line(text): + ansi_escape = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]') + return ansi_escape.sub('', text) + +@bot.message_handler(commands=['online']) +def check_online(message): + global RCON + if RCON.is_authenticated() == False: + try: + RCON.stop() + except: + pass + RCON = RCONClient(RCON_HOST, port=RCON_PORT) + RCON.login(RCON_PASS) + resp = clear_line(RCON.command('list')).replace(',','') + resp_split = resp.split() + + pl_list = resp_split[8:] + maxp = resp_split[3] + onp = 0 + + players = '' + first = True + for i in pl_list: + if '[СКРЫТ]' in i: + continue + elif first == True: + players += i + first = False + else: + players += ' ; ' + i + onp += 1 + + bot.reply_to(message, f"""🟢 Игроки онлайн >> {onp}/{maxp} + +{players}""") + + +# while True: + # try: + # bot.infinity_polling() + # except KeyboardInterrupt: + # exit() + # except: + # pass + +bot.infinity_polling()