Перенос конфигов в файл и добавление статуса оффлайн

main
none 8 months ago
parent a5a386ec2a
commit 7af9ba6966

20
db.py

@ -0,0 +1,20 @@
import os
import json
if not os.path.exists('config.json'):
db = {}
js = json.dumps(db, indent=2)
with open("config.json", "w") as outfile:
outfile.write(js)
print('Created new config.json')
def read(file = 'config.json'):
with open(file, "r", encoding="utf-8") as openfile:
db = json.load(openfile)
return db
def write(db, file = 'config.json'):
js = json.dumps(db, indent=2, ensure_ascii=False)
with open(file, "w", encoding="utf-8") as outfile:
outfile.write(js)

@ -1,21 +1,27 @@
from mctools import PINGClient from mctools import PINGClient
ping = PINGClient('135.181.170.94', 25630)
import telebot import telebot
API_TOKEN = '6142777478:AAHTyrHPhr8j0jWoSEIyPvpvmozVp6axLKE'
from db import *
API_TOKEN = read()['token']
bot = telebot.TeleBot(API_TOKEN) bot = telebot.TeleBot(API_TOKEN)
ping = PINGClient('CoolFunZone.aternos.me', 36413)
stats = ping.get_stats() stats = ping.get_stats()
print(stats) print(stats)
@bot.message_handler(commands=['online']) @bot.message_handler(commands=['online'])
def send_welcome(message): def check_online(message):
stats = ping.get_stats() try:
stats = ping.get_stats()
except:
bot.reply_to(message, "🔴 Сервер оффлайн")
return 0
maxp = stats['players']['max'] maxp = stats['players']['max']
onp= stats['players']['online'] onp = stats['players']['online']
try: try:
first = True first = True
for i in stats['players']['sample']: for i in stats['players']['sample']:
@ -26,9 +32,9 @@ def send_welcome(message):
pp = pp+ ' ; ' +i[0][:i[0].find('[')] pp = pp+ ' ; ' +i[0][:i[0].find('[')]
except: except:
pp = '' pp = ''
bot.reply_to(message, f"""🟢 Игроки онлайн >> {onp}/{maxp} bot.reply_to(message, f"""🟢 Игроки онлайн >> {onp}/{maxp}
{pp}""") {pp}""")

Loading…
Cancel
Save