-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.py
43 lines (41 loc) · 2.08 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
42
43
import logging, json
logger = logging.getLogger(__name__)
class Settings:
""" Loads data from settings.txt into the bot """
def __init__(self, bot):
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.txt", "r") as f:
settings = f.read()
data = json.loads(settings)
bot.set_settings(data['Host'],
data['Port'],
data['Channel'],
data['Nickname'],
data['Authentication'],
data["AllowedRanks"],
data["AllowedPeople"],
data['MonthsPerChance'])
logger.debug("Settings loaded into Bot.")
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.txt', 'w') as f:
standard_dict = {
"Host": "irc.chat.twitch.tv",
"Port": 6667,
"Channel": "#<channel>",
"Nickname": "<name>",
"Authentication": "oauth:<auth>",
"AllowedRanks": ["broadcaster", "moderator"],
"AllowedPeople": [],
"MonthsPerChance": 6,
}
f.write(json.dumps(standard_dict, indent=4, separators=(',', ': ')))
raise ValueError("Please fix your settings.txt file that was just generated.")