This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
BWTAqua/functions.py

126 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
from os import makedirs, stat
from gzip import open as gzipopen
from shutil import copyfileobj
from time import sleep
from ujson import loads, dumps
from modules.colors import *
from datetime import datetime
users_path = "users/"
logs_folder = "logs/"
def jsonSave(filename, value):
with open(filename, "w", encoding="utf-8") as f:
f.write(dumps(value, indent=4, ensure_ascii=False))
def jsonLoad(filename):
with open(filename, "r", encoding="utf-8") as f:
value = loads(f.read())
return value
config = jsonLoad(f"config.json")
log_size = config["log_size"]
owner_id = config["owner_id"]
def nowtime():
return f'{BBLACK}[{CYAN}{datetime.now().strftime("%H:%M:%S")}{BBLACK}]{RESET}'
def checkSize():
global logs_folder, log_size
i = 0
while i < 2:
try:
log = stat(logs_folder + "latest.log")
if (log.st_size / 1024) > log_size:
with open(logs_folder + "latest.log", "rb") as f_in:
with gzipopen(
f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip',
"wb",
) as f_out:
copyfileobj(f_in, f_out)
open(logs_folder + "latest.log", "w").close()
i = 2
except FileNotFoundError:
try:
log = open(logs_folder + "latest.log", "a")
open(logs_folder + "latest.log", "a").close()
except:
try:
makedirs(logs_folder, exist_ok=True)
log = open(logs_folder + "latest.log", "a")
open(logs_folder + "latest.log", "a").close()
except:
pass
i += 1
def appendLog(message):
global logs_folder
checkSize()
try:
log = open(logs_folder + "latest.log", "a")
open(logs_folder + "latest.log", "a").close()
except:
try:
makedirs(logs_folder, exist_ok=True)
log = open(logs_folder + "latest.log", "a")
open(logs_folder + "latest.log", "a").close()
except:
sleep(2)
print("Log file could not be created")
return
print(message, flush=True)
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] {message}\n')
log.close()
def string(key: str, *args: str, userlocale="uk"):
locales = jsonLoad("strings.json")
strings = locales[userlocale]
string = strings
for dict_key in args:
string = string[dict_key]
return string[key]
def userSet(userid, key: str, value):
database = jsonLoad("data/database.json")
if str(userid) not in database:
database[str(userid)] = {}
database[str(userid)][key] = value
jsonSave("data/database.json", database)
def userReset(userid, key: str):
database = jsonLoad("data/database.json")
del database[str(userid)][key]
jsonSave("data/database.json", database)
def userGet(userid, key: str):
try:
return jsonLoad("data/database.json")[str(userid)][key]
except KeyError:
return None
except FileNotFoundError:
return None