forked from RFBomb/Twitch-watcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserSettings.js
109 lines (93 loc) · 3.57 KB
/
UserSettings.js
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
const inquirer = require('inquirer');
var fs = require('fs');
const configPath = './config.json'
async function CheckConfigFileExists(){
try {
if (fs.existsSync(configPath)) {
try {
let configFile = JSON.parse(fs.readFileSync(configPath, 'utf8'))
JSON.stringify(configFile)
//return true
} catch (e) {
//unable to read file -> Delete the file
console.log('Invalid JSON File -> Deleting File');
fs.unlinkSync(configPath);
return false
}
return true
}else{
//console.log('Config File NOT Found.')
return false
}
} catch (err) {
console.log('🤬 Error: ', err);
console.log('Please visit my discord channel to solve this problem: https://discord.gg/s8AH4aZ');
}
}
async function ReadConfigSetting(SettingName) {
try {
if ( CheckConfigFileExists() ) {
let configFile = JSON.parse(fs.readFileSync(configPath, 'utf8'))
var OUTPUT = configFile[SettingName]
return OUTPUT
}else{
return ''
}
} catch (err) {
console.log('🤬 Error: ', err);
console.log('Please visit my discord channel to solve this problem: https://discord.gg/s8AH4aZ');
}
}
async function WriteConfigSetting(SettingName,NewValue) {
try {
var configFile
if ( await CheckConfigFileExists() ) {
// Read Existing File
configFile = JSON.parse(fs.readFileSync(configPath, 'utf8'));
}else {
// Create Empty Array
configFile = JSON.parse('{}');
}
//Update the file with new value
console.log('Updating Setting: "' + SettingName + '" = "' + NewValue + '"');
configFile[SettingName] = NewValue;
//Save the updated json
fs.writeFileSync(configPath, JSON.stringify(configFile), function(err) {
if (err) { console.log(err); }});
} catch (err) {
console.log('🤬 Error: ', err);
console.log('Please visit my discord channel to solve this problem: https://discord.gg/s8AH4aZ');
}
}
async function WriteNewConfig(JSON_Object ) {
try {
fs.writeFileSync(configPath, JSON.stringify(JSON_Object), function(err) {
if (err) { console.log(err); }});
SetupConfigFile();
} catch (err) {
console.log('🤬 Error: ', err);
console.log('Please visit my discord channel to solve this problem: https://discord.gg/s8AH4aZ');
}
}
async function SetupConfigFile() {
//console.log('Creating config.json Structure');
//await WriteConfigSetting('token','1') // Taken care of during 'readLoginData' routine
//await WriteConfigSetting('exec','2')
await WriteConfigSetting('ApiAuthToken','')
await WriteConfigSetting('streamersUrl','')
await WriteConfigSetting('ChannelName_1','')
await WriteConfigSetting('ChannelName_2','')
await WriteConfigSetting('ChannelName_3','')
await WriteConfigSetting('IgnoreRandomChannels','')
await WriteConfigSetting('Game','')
await WriteConfigSetting('minWatching','15')
await WriteConfigSetting('maxWatching','30')
}
// Export all functions that are allowed to be public
module.exports = {
WriteNewConfig,
CheckConfigFileExists,
WriteConfigSetting,
ReadConfigSetting,
SetupConfigFile
};