parent
05058d6ee6
commit
329ab82a1e
@ -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)
|
@ -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()
|
Loading…
Reference in new issue