-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.py
41 lines (35 loc) · 1.78 KB
/
Settings.py
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
import logging, json, os
logger = logging.getLogger(__name__)
class Settings:
""" Loads data from settings.txt into the bot """
PATH = os.path.join(os.getcwd(), "settings.txt")
def get_settings(self):
logger.debug("Loading settings.txt file...")
try:
# Try to load the file using json.
# And pass the data to the Bot class instance if this succeeds.
with open(Settings.PATH, "r") as f:
settings = f.read()
settings_dict = json.loads(settings)
logger.debug("Settings loaded into Bot.")
return [settings_dict[key] for key in settings_dict]
except ValueError:
logger.error("Error in settings file.")
raise ValueError("Error in settings file.")
except FileNotFoundError:
# If the file is missing, create a standardised settings.txt file
# With all parameters required.
logger.error("Please fix your settings.txt file that was just generated.")
with open(Settings.PATH, 'w') as f:
standard_dict = {
"Host": "irc.chat.twitch.tv",
"Port": 6667,
"Channel": "#<channel>",
"Nickname": "<name>",
"Authentication": "oauth:<auth>",
"MaxCharacters": 150,
"Cooldown": 30,
"BannedWords": []
}
f.write(json.dumps(standard_dict, indent=4, separators=(',', ': ')))
raise ValueError("Please fix your settings.txt file that was just generated.")