forked from Fiddlekins/podbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
226 lines (209 loc) · 6.43 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
const fs = require('fs-extra');
const path = require('path');
const inquirer = require('inquirer');
const minimist = require('minimist');
const log = require('./js/log.js');
const outputFormats = require('./js/outputFormats.js');
const { makeRelativePathsAbsolute } = require('./js/utils.js');
const Podbot = require('./js/Podbot.js');
const args = minimist(process.argv.slice(2), {
boolean: ['env-config']
});
const configPath = path.join(__dirname, 'config.json');
async function promptConfigCreation() {
const questions = [];
questions.push({
type: 'input',
name: 'token',
message: 'Input bot token:'
});
questions.push({
type: 'input',
name: 'podcastPath',
message: 'Input path to directory podbot will save podcasts to:',
default: `.${path.sep}podcasts`
});
questions.push({
type: 'input',
name: 'controllerRoles',
message: 'Input comma separated names of roles that podbot will listen to:',
default: 'podhandler'
});
questions.push({
type: 'input',
name: 'controllerUsers',
message: 'Input comma separated user IDs of users that podbot will listen to:',
default: ''
});
questions.push({
type: 'input',
name: 'commandPrefix',
message: 'Input string podbot will recognise as the command prefix:',
default: '/'
});
questions.push({
type: 'input',
name: 'timeout',
message: 'Specify how long podbot wait before attempting to restart after crashing in ms. (Be wary of rate limits):',
default: 10000,
validate: (input, answers) => {
const parsedInput = parseInt(input, 10);
return !isNaN(parsedInput) && parsedInput > 0;
}
});
questions.push({
type: 'confirm',
name: 'sanitizeLogs',
message: 'Should logs have folder paths sanitized:',
default: false
});
questions.push({
type: 'list',
name: 'outputFormat',
message: 'Select format to output audio during post processing:',
choices: Object.values(outputFormats),
default: outputFormats.WAV
});
questions.push({
type: 'list',
name: 'presenceType',
message: 'Set the way the activity message behaves:',
choices: [
{
name: "public (The voice channel names will be listed in the activity message, across all servers the bot is in)",
short: "public",
value: "public"
},
{
name: "private (A count of the number of actively recorded podcasts will be displayed)",
short: "private",
value: "private"
},
{
name: "custom (Pick a custom activity to display)",
short: "custom",
value: "custom"
}
]
});
const answers = await inquirer.prompt(questions);
const config = {
podbot: {
token: answers['token'].toString(),
podcastPath: answers['podcastPath'].toString(),
controllers: {
roles: answers['controllerRoles'].toString().split(',').filter(role => role.length > 0),
users: answers['controllerUsers'].toString().split(',').filter(role => role.length > 0)
},
commandPrefix: answers['commandPrefix'].toString(),
presence: {
type: answers['presenceType'].toString()
},
postProcess: {
format: answers['outputFormat'].toString()
}
},
timeout: parseInt(answers['timeout'], 10),
sanitizeLogs: !!answers['sanitizeLogs']
};
if (answers['presenceType'] === 'custom') {
const answers2 = await inquirer.prompt([
{
type: 'input',
name: 'presenceCustomNone',
message: 'Custom activity when podbot is not recording any podcasts:',
default: 'you'
},
{
type: 'input',
name: 'presenceCustomSingle',
message: 'Custom activity when podbot is recording one podcast:',
default: 'the neighbour'
},
{
type: 'input',
name: 'presenceCustomMultiple',
message: 'Custom activity when podbot is recording multiple podcasts:',
default: 'many things'
}
]);
config.podbot.presence.activity = {
none: answers2['presenceCustomNone'],
single: answers2['presenceCustomSingle'],
multiple: answers2['presenceCustomMultiple']
}
}
await fs.writeJson(configPath, config, { spaces: '\t' });
return config;
}
function run(config) {
let podbot = new Podbot(config.podbot);
let timeout = null;
const uncrash = () => {
if (timeout === null) {
log.warn('Destroying podbot');
podbot.destroy().then(() => {
timeout = setTimeout(() => {
log.warn('Recreating podbot');
timeout = null;
podbot = new Podbot(config.podbot);
}, config.timeout);
})
}
};
process.on('unhandledRejection', (err) => {
log.error(`Uncaught Promise Rejection: \n${err.stack || err}`);
uncrash();
});
process.on('uncaughtException', (err) => {
log.error(`Uncaught Exception: \n${err.stack || err}`);
uncrash();
});
}
async function init() {
let config;
try {
if (args["env-config"]) {
config = {
podbot: {
token: process.env.POD_TOKEN.trim(),
podcastPath: process.env.POD_PODCAST_PATH || `.${path.sep}podcasts`,
controllers: {
roles: (process.env.POD_ROLES || 'podhandler').split(','),
users: (process.env.POD_USERS || '').split(',')
},
commandPrefix: process.env.POD_PREFIX || '/',
presence: {
type: process.env.POD_PRESENCE_TYPE || 'public',
activity: {
none: process.env.POD_PRESENCE_ACTIVITY_NONE,
single: process.env.POD_PRESENCE_ACTIVITY_SINGLE,
multiple: process.env.POD_PRESENCE_ACTIVITY_MULTIPLE
}
},
postProcess: {
format: process.env.POD_OUTPUT_FORMAT || outputFormats.WAV
}
},
timeout: parseInt(process.env.POD_TIMEOUT) || 10000,
sanitizeLogs: process.env.POD_SANITIZE_LOGS === 'true'
}
} else {
config = await fs.readJson(configPath);
}
} catch (err) {
// Don't care about err, it just means there isn't a valid config file available
config = await promptConfigCreation();
}
makeRelativePathsAbsolute(config);
log.sanitize = config.sanitizeLogs;
run(config);
}
var http = require('http');
var port = process.env.PORT || 8080;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('{"status":"OK"}');
}).listen(port, '0.0.0.0');
init();