forked from tomaarsen/TwitchMarkovChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.py
94 lines (80 loc) · 4.51 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
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
import json, os, logging
logger = logging.getLogger(__name__)
class Settings:
""" Loads data from settings.txt into the bot """
PATH = os.path.join(os.getcwd(), "settings.txt")
def __init__(self, bot):
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()
data = json.loads(settings)
# "BannedWords" is only a key in the settings in older versions.
# We moved to a separate file for blacklisted words.
if "BannedWords" in data:
logger.info("Updating Blacklist system to new version...")
try:
with open("blacklist.txt", "r+") as f:
logger.info("Moving Banned Words to the blacklist.txt file...")
# Read the data, and split by word or phrase, then add BannedWords
banned_list = f.read().split("\n") + data["BannedWords"]
# Remove duplicates and sort by length, longest to shortest
banned_list = sorted(list(set(banned_list)), key=lambda x: len(x), reverse=True)
# Clear file, and then write in the new data
f.seek(0)
f.truncate(0)
f.write("\n".join(banned_list))
logger.info("Moved Banned Words to the blacklist.txt file.")
except FileNotFoundError:
with open("blacklist.txt", "w") as f:
logger.info("Moving Banned Words to a new blacklist.txt file...")
# Remove duplicates and sort by length, longest to shortest
banned_list = sorted(list(set(data["BannedWords"])), key=lambda x: len(x), reverse=True)
f.write("\n".join(banned_list))
logger.info("Moved Banned Words to a new blacklist.txt file.")
# Remove BannedWords list from data dictionary, and then write it to the settings file
del data["BannedWords"]
with open(Settings.PATH, "w") as f:
f.write(json.dumps(data, indent=4, separators=(",", ": ")))
logger.info("Updated Blacklist system to new version.")
bot.set_settings(data["Host"],
data["Port"],
data["Channel"],
data["Nickname"],
data["Authentication"],
data["DeniedUsers"],
data["Cooldown"],
data["KeyLength"],
data["MaxSentenceWordAmount"])
except ValueError:
logger.error("Error in settings file.")
raise ValueError("Error in settings file.")
except FileNotFoundError:
Settings.write_default_settings_file()
raise ValueError("Please fix your settings.txt file that was just generated.")
@staticmethod
def write_default_settings_file():
# If the file is missing, create a standardised settings.txt file
# With all parameters required.
with open(Settings.PATH, "w") as f:
standard_dict = {
"Host": "irc.chat.twitch.tv",
"Port": 6667,
"Channel": "#<channel>",
"Nickname": "<name>",
"Authentication": "oauth:<auth>",
"DeniedUsers": ["StreamElements", "Nightbot", "Moobot", "Marbiebot"],
"Cooldown": 20,
"KeyLength": 2,
"MaxSentenceWordAmount": 25
}
f.write(json.dumps(standard_dict, indent=4, separators=(",", ": ")))
@staticmethod
def update_cooldown(cooldown):
with open(Settings.PATH, "r") as f:
settings = f.read()
data = json.loads(settings)
data["Cooldown"] = cooldown
with open(Settings.PATH, "w") as f:
f.write(json.dumps(data, indent=4, separators=(",", ": ")))