1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
import jsonschema
import os
import re
import yaml
import sys
import discord
import logging
from colorlog import ColoredFormatter
log_level = logging.DEBUG
log_format = (
" %(log_color)s%(levelname)-8s%(reset)s |"
" %(log_color)s%(message)s%(reset)s"
)
logging.root.setLevel(log_level)
formatter = ColoredFormatter(log_format)
stream = logging.StreamHandler()
stream.setLevel(log_level)
stream.setFormatter(formatter)
LOG = logging.getLogger("pythonConfig")
LOG.setLevel(log_level)
LOG.addHandler(stream)
TOKEN = None
NAMING_SCHEME = None
BOT_COLOR = None
SQLITE_NAME = "disarchive"
DB_NAME = None
DB_ENGINE = None
DB_HOST = None
DB_PORT = None
DB_USER = None
DB_PASSWORD = None
schema = {
"type": "object",
"properties": {
"general": {
"type": "object",
"properties": {
"token": {"type": "string"},
"bot_color": {"type": "string", "default": "#fc5f4e"},
"naming_scheme": {
"enum": ["random", "timestamp", "id", "original"]
},
},
"required": ["token"],
},
"sqlite": {
"type": "object",
"properties": {
"name": {"type": "string", "default": "disarchive"},
},
"required": ["name"],
},
"mysql": {
"type": "object",
"properties": {
"name": {"type": "string", "default": "disarchive"},
"host": {"type": "string", "default": "localhost"},
"port": {"type": "integer", "default": 3306},
"user": {"type": "string"},
"password": {"type": "string"},
},
"required": [
"name",
"host",
"port",
"user",
"password",
],
},
"postgresql": {
"type": "object",
"properties": {
"name": {"type": "string", "default": "disarchive"},
"host": {"type": "string", "default": "localhost"},
"port": {"type": "integer", "default": 5432},
"user": {"type": "string"},
"password": {"type": "string"},
},
"required": [
"name",
"host",
"port",
"user",
"password",
],
},
},
"required": ["general"],
}
# Load config file or alert user if not found
def load_config():
# create images directory if it doesn't exist
if not os.path.exists("images"):
os.makedirs("images")
if os.path.exists("/.dockerenv"):
file_path = "/config/config.yaml"
else:
file_path = "config.yaml"
try:
with open(file_path, "r") as f:
file_contents = f.read()
validate_config(file_contents)
except FileNotFoundError:
sys.exit(
LOG.critical(
"config.yaml file not found. Please use the template to"
" configure the bot."
)
)
# Validate the config file against the schema
def validate_config(file_contents):
global TOKEN, NAMING_SCHEME, BOT_COLOR, SQLITE_NAME, DB_NAME, DB_ENGINE, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD
config = yaml.safe_load(file_contents)
try:
jsonschema.validate(config, schema)
except jsonschema.ValidationError as e:
sys.exit(LOG.critical(f"Error in config.yaml file: {e.message}"))
# Make sure "bot_color" is a valid hex color
hex_pattern_one = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
hex_pattern_two = "^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
# Check if the bot_color is a valid hex color
if "bot_color" in config["general"]:
if not bool(
re.match(hex_pattern_one, config["general"]["bot_color"])
) and not bool(
re.match(hex_pattern_two, config["general"]["bot_color"])
):
LOG.warn(
"bot_color is not a valid hex color... defaulting to #26dfc9"
)
else:
BOT_COLOR = discord.Color(
int((config["general"]["bot_color"]).replace("#", ""), 16)
)
# Naming scheme
if "naming_scheme" in config["general"]:
NAMING_SCHEME = config["general"]["naming_scheme"]
else:
LOG.info("No naming scheme specified... defaulting to random")
NAMING_SCHEME = "random"
# Assign database variables
if "sqlite" in config:
DB_ENGINE = "sqlite"
if "name" in config["sqlite"]:
SQLITE_NAME = config["sqlite"]["name"]
elif "mysql" in config:
DB_ENGINE = "mysql"
DB_NAME = config["mysql"]["name"]
DB_HOST = config["mysql"]["host"]
DB_PORT = config["mysql"]["port"]
DB_USER = config["mysql"]["user"]
DB_PASSWORD = config["mysql"]["password"]
elif "postgresql" in config:
DB_ENGINE = "postgresql"
DB_NAME = config["postgresql"]["name"]
DB_HOST = config["postgresql"]["host"]
DB_PORT = config["postgresql"]["port"]
DB_USER = config["postgresql"]["user"]
DB_PASSWORD = config["postgresql"]["password"]
else:
LOG.warn("No database engine specified. Defaulting to SQLite.")
DB_ENGINE = "sqlite"
TOKEN = config["general"]["token"]
|