refractor lib code

This commit is contained in:
2022-08-21 22:24:52 +03:00
parent 5a50b2ac98
commit ea3e9dd105
9 changed files with 244 additions and 316 deletions

79
lib/Comands.py Normal file
View File

@@ -0,0 +1,79 @@
import json
from os import walk, path
def check_json():
if not path.isfile('prefix.json'):
with open('prefix.json', 'w+', encoding='utf-8') as f:
f.write("")
async def set_prefix(guildid: int, prefix: str) -> None:
await write_json(guildid, "prefix", prefix)
async def list_files(fold: str = 'audio'):
fl = []
for filenames in walk(fold):
fl.extend(filenames)
break
files = {}
for x in fl[2]:
files[x] = x
return fl[2]
async def read_json(guild: int, param: str):
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
parameter = None
try:
_json = json.load(fp) # Load the custom prefixes
except TypeError:
_json = {}
if guild: # If the guild exists
try:
guild_conf = _json[f"{guild}"]
try:
parameter = guild_conf[f"{param}"]
except:
pass
except:
pass
return parameter
async def write_json(guild: int, param_name: str, param: str or int):
with open('prefix.json', 'r', encoding='utf-8') as f:
try:
_json = json.load(f)
except json.decoder.JSONDecodeError:
_json = {}
try:
_guild = _json[f'{guild}']
except KeyError:
_json.update({f'{guild}': {}})
_guild = _json[f'{guild}']
_guild.update({f'{param_name}': f'{param}'})
with open('prefix.json', 'w', encoding='utf-8') as f:
json.dump(_json, f, indent=4)
async def determine_prefix(bot, msg):
"""
Determite per-server bot prefix
:param bot: Disnake Bot object
:param msg: Disnake msg object
:return: prefix for server, default is $
"""
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
parameter: str
try:
from json import load
_json = load(fp) # Load the custom prefixes
except:
_json = {}
try:
parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up
except:
parameter = '$'
return parameter