87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
"""
|
|
integral_lib.Commands
|
|
~~~~~~~~~~~~~~
|
|
Some prepare for commands
|
|
|
|
|
|
"""
|
|
from json import load, decoder, dump, JSONDecodeError
|
|
from os import getenv
|
|
|
|
from disnake.ext import commands
|
|
|
|
|
|
async 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):
|
|
"""
|
|
A function to write JSON data to a file after updating or adding a parameter value.
|
|
|
|
Parameters:
|
|
|
|
- guild: an integer representing the guild ID
|
|
- param_name: a string representing the parameter name
|
|
- param: a string or integer representing the parameter value
|
|
|
|
Returns:
|
|
This function does not return anything.
|
|
"""
|
|
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}': f'{param}'})
|
|
with open(getenv('CONF_FILE'), 'w', encoding='utf-8') as f:
|
|
dump(_json, f, indent=4)
|
|
|
|
|
|
def determine_prefix(bot: commands.Bot, msg):
|
|
"""
|
|
Determine the per-server bot prefix based on the given message object.
|
|
|
|
: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
|