-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
160 lines (154 loc) · 4.2 KB
/
setup.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
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
const fs = require('fs');
const readline = require('readline');
const TEMPLATE = {
'URL_SERVER': {
'message': 'Base URL for the FiveM server e.g. http://127.0.0.1:3501 (don\'t end with /)',
'required': true,
},
'SERVER_NAME': {
'message': 'The name of your FiveM server',
'required': true,
},
'SERVER_LOGO': {
'message': 'A logo for your FiveM server',
'required': false,
},
'PERMISSION': {
'message': 'Permission for the +status command',
'required': true,
},
'BOT_TOKEN': {
'message': 'Discord bot token',
'required': true,
},
'CHANNEL_ID': {
'message': 'Channel ID that will be used for updates to be pushed to',
'required': true,
},
'MESSAGE_ID': {
'message': 'Message ID of previous update to edit (not required)',
'required': false,
'default': null
},
'SUGGESTION_CHANNEL': {
'message': 'Channel that will create suggestion embeds in',
'required': false,
},
'BUG_CHANNEL': {
'message': 'Channel that will recieve bug reports',
'required': false
},
'BUG_LOG_CHANNEL': {
'message': 'Channel that will log bug reports',
'required': false,
},
'LOG_CHANNEL': {
'message': 'Channel that will log status changes',
'required': true,
},
'DEBUG': {
'message': 'Disable/Enable debug logs (spammy)',
'required': false
},
'WEBSITE_URL': {
'message': 'Creates a link button for the status embed. Make sure you are using https://[website] or http://[website]',
'required': true
},
'SHOW_PLAYERS': {
'message': 'Choose to either hide or show the online players',
'required': false
},
'RESTART_TIMES': {
'message': 'Displays restart time of your server',
'required': true
}
};
const SAVE_FILE = './config.json';
function loadValue(key) {
return new Promise((resolve, reject) => {
const io = readline.createInterface({
input: process.stdin,
output: process.stdout
});
io.question(`Please enter a value for '${key}'${TEMPLATE[key].required ? '' : ` (Not required defaults to '${TEMPLATE[key].default}')`}\n ${TEMPLATE[key].message}\n> `, (value) => {
io.close();
resolve(value);
});
})
}
exports.createValues = function(keys) {
return new Promise((resolve,reject) => {
var data = {};
if (keys === undefined) {
keys = Object.keys(TEMPLATE);
}
const loop = function(i) {
if (i < keys.length) {
loadValue(keys[i]).then((value) => {
let realValue = value.trim();
if (TEMPLATE[keys[i]].required) {
if (realValue.length > 0) {
data[keys[i]] = realValue;
loop(i + 1);
} else {
console.log('Invalid input');
loop(i);
}
} else {
if (realValue.length > 0) {
data[keys[i]] = realValue;
loop(i+1);
} else {
data[keys[i]] = TEMPLATE[keys[i]].default;
loop(i + 1);
}
}
})
} else {
resolve(data);
}
}
loop(0);
})
}
exports.saveValues = function(values) {
return new Promise((resolve, reject) => {
fs.writeFile(SAVE_FILE, JSON.stringify(values),(err) => {
if (err) return reject(err);
return resolve(true);
})
})
}
exports.loadValues = function() {
return new Promise((resolve, reject) => {
fs.readFile(SAVE_FILE, (err, data) => {
if (err) return reject(err);
var json;
try {
json = JSON.parse(data);
} catch(e) {
console.log('There is a JSON error in your ./config.json file!');
return reject(e);
}
let notFound = new Array();
for (var key in TEMPLATE) {
if (!json.hasOwnProperty(key)) {
notFound.push(key);
}
}
if (notFound.length === 0) {
return resolve(json);
} else {
console.log('Found new configuration values and they have been added!');
exports.createValues(notFound).then((data) => {
for (var key in data) {
json[key] = data[key];
}
exports.saveValues(json).then(() => {
resolve(json);
}).catch(reject);
}).catch(reject);
}
})
});
}