Files
discord_bot/lib/Comands.py
2022-08-22 21:50:15 +03:00

81 lines
2.0 KiB
Python

import json
from os import walk
"""
Some prepare for commands
"""
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) -> int or str:
"""
Reads Json file to determite config strings
:param guild: ID of Guild
:param _param: Parameter in json file
:return: value of parameter
"""
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
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:
parameter = None
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)
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 $
"""
parameter = '$'
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
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:
pass
return parameter