74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
"""
|
|
lib.Commands
|
|
~~~~~~~~~~~~~~
|
|
Some prepare for commands
|
|
"""
|
|
from json import load, decoder, dump, JSONDecodeError
|
|
from os import getenv
|
|
|
|
from disnake.ext import commands
|
|
|
|
|
|
def read_json(guild: int, _param: str):
|
|
"""
|
|
Reads Json file to determite config strings
|
|
:param guild: ID of Guild
|
|
:param _param: Parameter in json file
|
|
:return: value of parameter.
|
|
"""
|
|
parameter = None
|
|
with open(getenv('CONF_FILE'), encoding='utf-8') as f: # Open the JSON
|
|
try:
|
|
_json = load(f) # Load the custom prefixes
|
|
except decoder.JSONDecodeError:
|
|
_json = {}
|
|
if guild: # If the guild exists
|
|
try:
|
|
guild_conf = _json[f"{guild}"]
|
|
try:
|
|
parameter = guild_conf[f"{_param}"]
|
|
except KeyError:
|
|
pass
|
|
except KeyError:
|
|
pass
|
|
return parameter
|
|
|
|
|
|
async def write_json(guild: int, param_name: str, param: str or int):
|
|
print(type(param))
|
|
with open(getenv('CONF_FILE'), encoding='utf-8') as f:
|
|
try:
|
|
_json = load(f)
|
|
except decoder.JSONDecodeError:
|
|
_json = {}
|
|
try:
|
|
_guild = _json[f'{guild}']
|
|
except KeyError:
|
|
_json.update({f'{guild}': {}})
|
|
_guild = _json[f'{guild}']
|
|
_guild.update({f'{param_name}': param})
|
|
_json.update({f'{guild}': _guild})
|
|
with open(getenv('CONF_FILE'), 'w', encoding='utf-8') as f:
|
|
dump(_json, f, indent=4)
|
|
|
|
|
|
def determine_prefix(bot: commands.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(getenv('CONF_FILE'), encoding='utf-8') as f: # Open the JSON
|
|
try:
|
|
_json = load(f) # Load the custom prefixes
|
|
except JSONDecodeError:
|
|
_json = {}
|
|
try:
|
|
parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up
|
|
|
|
except KeyError:
|
|
pass
|
|
return parameter
|