Skip to content

Commit

Permalink
Merge pull request #58 from BlueAtomic/Rewrite-get_Data-function
Browse files Browse the repository at this point in the history
if !config.json, check for .env
  • Loading branch information
zortos293 authored Jul 2, 2023
2 parents 458633c + caa3312 commit 58dfb94
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions utilities/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
from os import getenv
from sys import exit as sysexit

DOTENV_INSTALLED = True

try:
from dotenv import load_dotenv

load_dotenv()
except ModuleNotFoundError:
print('You did not install the dotenv module! You will not be able to use a .env file.')
print("You did not install the dotenv module! .env files can not be used.")
DOTENV_INSTALLED = False
try:
from mcstatus import JavaServer
from mcstatus.pinger import PingResponse
except ModuleNotFoundError:
print('You did not install the mcstatus module! Exiting now...')
sysexit()
sysexit('You did not install the mcstatus module! Exiting now...')


class Colors:
Expand All @@ -34,39 +37,36 @@ def get_data() -> dict:
If you do not have a config.json file, you can use environment variables.
:return: The data from the config.json file.
"""
usejson = True # Set to True to a config.json
if usejson:
try:
with open('config.json', 'r', encoding="UTF-8") as file:
data = load(file)
except FileNotFoundError:
print('config.json not found! Exiting now...')
sysexit()
except decoder.JSONDecodeError:
print('config.json is not valid! Exiting now...')
sysexit()
if not usejson:
try:
data = {
"Token": getenv('TOKEN'),
"Prefix": getenv('PREFIX'),
"Owners": getenv('OWNERS').split(','),
"FeatureGuilds": getenv('FEATURE_GUILDS').split(','),
"Database": {
"Host": getenv('DB_HOST'),
"User": getenv('DB_USER'),
"Password": getenv('DB_PASSWORD'),
"Database": getenv('DB_DATABASE'),
"Port": getenv('DB_PORT')
},
"Logs": {
"JoinWebhook": getenv('LOGS_JOINWEBHOOK'),
"LeaveWebhook": getenv('LOGS_LEAVEWEBHOOK')
try:
with open('config.json', 'r', encoding="UTF-8") as file:
data = load(file)
except FileNotFoundError:
print("No config.json file found. Trying to use dotenv...")
if not DOTENV_INSTALLED:
sysexit('You did not install the dotenv module and no config.json was provided! Exiting now...')
else:
try:
data = {
"Token": getenv('TOKEN'),
"Prefix": getenv('PREFIX'),
"Owners": getenv('OWNERS').split(','),
"FeatureGuilds": getenv('FEATURE_GUILDS').split(','),
"Database": {
"Host": getenv('DB_HOST'),
"User": getenv('DB_USER'),
"Password": getenv('DB_PASSWORD'),
"Database": getenv('DB_DATABASE'),
"Port": getenv('DB_PORT')
},
"Logs": {
"JoinWebhook": getenv('LOGS_JOINWEBHOOK'),
"LeaveWebhook": getenv('LOGS_LEAVEWEBHOOK')
}
}
}
except AttributeError:
print('You did not fill out the environment variables! Exiting now...')
sysexit()
except AttributeError:
sysexit('One or more environment variables could be found. Exiting now...')
except decoder.JSONDecodeError:
sysexit('config.json is not valid! Exiting now...')
return data


Expand Down

0 comments on commit 58dfb94

Please sign in to comment.