Browse Source

index: add timers, catch promise errors, add /viewers, other fixes

master
vampirefrog 8 months ago
parent
commit
0d03141ac0
  1. 175
      index.js

175
index.js

@ -19,7 +19,9 @@ const
fetch = require('node-fetch')
;
var config = require('./config');
var configFile = process.argv[2] || './config';
console.log('Loading config from', configFile);
var config = require(configFile);
var songProviders = [
new YouTube(),
@ -108,10 +110,12 @@ say.getInstalledVoices((err, voices) => {
var voices = JSON.parse(fs.readFileSync('./voices.json'));
var vampiVoices = [];
var vampiVoices = {};
fetch('http://tts.vampi.tech/voices').then((response) => { return response.json(); }).then((voiceData) => {
vampiVoices = voiceData.voices;
for(let v of voiceData.voices) {
vampiVoices[v.toLowerCase()] = v;
}
});
function getResponsiveVoiceData(voice) {
@ -347,12 +351,12 @@ app.get('/speech/:platform/:username', (req, res, next) => {
var pitch = user.pitch || 0.5;
if(pitch < 0) pitch = 0;
if(pitch > 1) pitch = 1;
var voice = user.voice || 'Microsoft David';
voice = voice.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ');
var voice = user.voice || (config.voices && config.voices.default) || 'Microsoft David';
voice = voice.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ').toLowerCase();
if(vampiVoices.indexOf(voice) !== -1) {
if(voice in vampiVoices) {
var urlData = {
voice: voice,
voice: vampiVoices[voice],
rate: rate * 2.0,
text: req.query.text
};
@ -466,6 +470,56 @@ app.get('/speech/:platform/:username', (req, res, next) => {
;
});
app.get('/viewers', (req, res, name) => {
let ret = {};
let promises = [];
for(let client of chatClients) {
for(let channel of client.channels) {
promises.push(channel.getNumViewers());
}
}
Promise.all(promises).then((responses) => {
console.log('responses', responses);
for(let r of responses) {
if(!r) continue;
if(!(r.platform in ret)) {
ret[r.platform] = 0;
}
ret[r.platform] += r.viewers;
}
res.send(JSON.stringify(ret));
}, (err) => {
console.error(err);
});
});
function sendSticker(channel, q) {
var url = 'https://vampi.tech/wp-admin/admin-ajax.php?'+util.buildQueryString({ action: 'sticker_search', q: q||''});
https.get(url, {
headers: { referer: 'https://vampi.tech/' }
}, (r) => {
if(r.statusCode !== 200) {
res.send('Error '+r.statusCode);
} else {
var resultJson = '';
r.on('data', (chunk) => {
resultJson += chunk;
});
r.on('end', () => {
var result;
try {
result = JSON.parse(resultJson);
if(result && result.results && result.results[0]) {
channel.sendChat(':emote/mine/dlive/'+result.results[0].id+':').catch(err => console.error(err));
}
} catch(e) {
}
});
}
});
}
function handleChat(data, channel) {
let text = data.text || data.message;
@ -487,24 +541,17 @@ function handleChat(data, channel) {
response = response[Math.floor(Math.random() * response.length)];
}
channel.sendChat(response.replace('%s', params))
.then((msg) => {
console.log('Sent message', msg);
})
.catch((err) => {
console.error(err);
})
;
channel.sendChat(response.replace('%s', params)).catch(err => console.error(err));
} else if(cmd == '!voice') {
if(params) {
var voice = params;
voice = voice.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, ' ');
if(vampiVoices.indexOf(voice) !== -1 || localVoices.indexOf(voice) !== -1) {
if(voice.toLowerCase() in vampiVoices || localVoices.indexOf(voice) !== -1) {
db.run(`UPDATE users SET voice = ? WHERE id = ?`, [voice, user.id], (err) => {
if(err) {
channel.sendChat(`Could not change voice for ${data.displayName}: ${err}`);
channel.sendChat(`Could not change voice for ${data.displayName}: ${err}`).catch(err => console.error(err));
} else {
channel.sendChat(`Voice for ${data.displayName} changed to ${voice}, rate is ${user.rate}, pitch is ${user.pitch}.`);
channel.sendChat(`Voice for ${data.displayName} changed to ${voice}, rate is ${user.rate}, pitch is ${user.pitch}.`).catch(err => console.error(err));
}
});
} else {
@ -512,76 +559,54 @@ function handleChat(data, channel) {
if(voiceData) {
db.run(`UPDATE users SET voice = ? WHERE id = ?`, [voice, user.id], (err) => {
if(err) {
channel.sendChat(`Could not change voice for ${data.displayName}: ${err}`);
channel.sendChat(`Could not change voice for ${data.displayName}: ${err}`).catch(err => console.error(err));
} else {
channel.sendChat(`Voice for ${data.displayName} changed to ${voice}, rate is ${user.rate}, pitch is ${user.pitch}.`);
channel.sendChat(`Voice for ${data.displayName} changed to ${voice}, rate is ${user.rate}, pitch is ${user.pitch}.`).catch(err => console.error(err));
}
});
} else {
channel.sendChat(`Invalid voice ${voice}. Select a voice from https://vampi.tech/tts`);
channel.sendChat(`Invalid voice ${voice}. Select a voice from https://vampi.tech/tts`).catch(err => console.error(err));
}
}
} else {
channel.sendChat(`Voice for ${data.displayName} is ${user.voice}, rate is ${user.rate}, pitch is ${user.pitch}. Select a voice from https://vampi.tech/tts`);
channel.sendChat(`Voice for ${data.displayName} is ${user.voice}, rate is ${user.rate}, pitch is ${user.pitch}. Select a voice from https://vampi.tech/tts`).catch(err => console.error(err));
}
} else if(cmd == '!pitch') {
if(params) {
let pitch = parseFloat(params);
if(typeof pitch === 'NaN' || pitch < 0.0 || pitch > 1.0) {
channel.sendChat(`Could not change pitch for ${data.displayName}: Invalid number. Specify a value between 0.0 and 1.0.`);
channel.sendChat(`Could not change pitch for ${data.displayName}: Invalid number. Specify a value between 0.0 and 1.0.`).catch(err => console.error(err));
} else {
db.run(`UPDATE users SET pitch = ? WHERE id = ?`, [pitch, user.id], (err) => {
if(err) {
channel.sendChat(`Could not change pitch for ${data.displayName}: ${err}`);
channel.sendChat(`Could not change pitch for ${data.displayName}: ${err}`).catch(err => console.error(err));
} else {
channel.sendChat(`Pitch for ${data.displayName} changed to ${pitch}`);
channel.sendChat(`Pitch for ${data.displayName} changed to ${pitch}`).catch(err => console.error(err));
}
});
}
} else {
channel.sendChat(`Pitch for ${data.displayName} is ${user.pitch}`);
channel.sendChat(`Pitch for ${data.displayName} is ${user.pitch}`).catch(err => console.error(err));
}
} else if(cmd == '!rate') {
if(params) {
let rate = parseFloat(params);
if(typeof rate === 'NaN' || rate < 0.0 || rate > 1.0) {
channel.sendChat(`Could not change rate for ${data.displayName}: Invalid number. Specify a value between 0.0 and 1.0.`);
channel.sendChat(`Could not change rate for ${data.displayName}: Invalid number. Specify a value between 0.0 and 1.0.`).catch(err => console.error(err));
} else {
db.run(`UPDATE users SET rate = ? WHERE id = ?`, [rate, user.id], (err) => {
if(err) {
channel.sendChat(`Could not change rate for ${data.displayName}: ${err}`);
channel.sendChat(`Could not change rate for ${data.displayName}: ${err}`).catch(err => console.error(err));
} else {
channel.sendChat(`Rate for ${data.displayName} changed to ${rate}`);
channel.sendChat(`Rate for ${data.displayName} changed to ${rate}`).catch(err => console.error(err));
}
});
}
} else {
channel.sendChat(`Rate for ${data.displayName} is ${user.rate}`);
channel.sendChat(`Rate for ${data.displayName} is ${user.rate}`).catch(err => console.error(err));
}
} else if(cmd == '!sticker') {
var url = 'https://vampi.tech/wp-admin/admin-ajax.php?'+util.buildQueryString({ action: 'sticker_search', q: params||''});
https.get(url, {
headers: { referer: 'https://vampi.tech/' }
}, (r) => {
if(r.statusCode !== 200) {
res.send('Error '+r.statusCode);
} else {
var resultJson = '';
r.on('data', (chunk) => {
resultJson += chunk;
});
r.on('end', () => {
var result;
try {
result = JSON.parse(resultJson);
if(result && result.results && result.results[0]) {
channel.sendChat(':emote/mine/dlive/'+result.results[0].id+':');
}
} catch(e) {
}
});
}
});
sendSticker(channel, params);
} else {
handleUrls(user, data, channel);
}
@ -676,7 +701,7 @@ function insertUrl(data, user, url, chatId, text, info) {
console.error('Error inserting chat URL chat_id=${chatId} url=${url}: ${err}');
});
console.log(`Harvested URL ${url} ${info.title}`, util.secondsToTime(info.duration));
console.log(`Harvested URL ${url}`, info.title||'', info.duration ? util.secondsToTime(info.duration) : '');
serverWS.clients.forEach(function(client) {
client.send(JSON.stringify({
type: 'url',
@ -756,6 +781,7 @@ function handleUrls(user, data, channel) {
provider.getInfo(url, (err, info) => {
if(err) {
console.error('Could not read info from', url, err);
insertUrl(data, user, url, chatId, text);
return;
}
@ -765,7 +791,7 @@ function handleUrls(user, data, channel) {
insertUrl(data, user, canonicalUrl, chatId, text, info);
if(u == 0)
channel.sendChat(info.title + (info.duration ? ' ' + util.secondsToTime(info.duration) : ''));
channel.sendChat(info.title + (info.duration ? ' ' + util.secondsToTime(info.duration) : '')).catch(err => console.error(err));
});
} else {
insertUrl(data, user, url, chatId, text);
@ -798,7 +824,7 @@ for(let i in config.streamers) {
channel.addListener('joined', (event) => {
if(channelConfig.hello)
channel.sendChat(channelConfig.hello);
channel.sendChat(channelConfig.hello).catch(err => console.error(err));
});
channel.addListener('message', (data) => {
@ -812,6 +838,8 @@ for(let i in config.streamers) {
wsClient.send(JSON.stringify(data));
});
});
}, (err) => {
console.error('Could not join channel', channelConfig.displayName||channelConfig.username, err);
});
}
});
@ -819,6 +847,38 @@ for(let i in config.streamers) {
chatClients.push(client);
}
if(config.timers) {
for(let t of config.timers) {
if(!t.interval)
continue;
setTimeout(() => {
setInterval(() => {
for(let client of chatClients) {
for(let channel of client.channels) {
if(t.sticker) {
if(client.getPlatformName() != 'dlive')
continue;
sendSticker(channel, t.search);
} else {
if(t.platforms && t.platforms.indexOf(client.getPlatformName()) === -1)
continue;
var message = t.message;
if(typeof message != 'string') {
message = message[Math.floor(Math.random() * message.length)];
}
channel.sendChat(message).catch(err => console.error(err));
}
}
}
}, t.interval);
}, t.delay || 0);
}
}
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
@ -836,7 +896,8 @@ process.on("SIGINT", function() {
for(let i in chatClients) {
var joinedChannels = chatClients[i].channels;
for(let j in joinedChannels) {
joinedChannels[j].sendChat(joinedChannels[j].goodbye);
if(joinedChannels[j].goodbye)
joinedChannels[j].sendChat(joinedChannels[j].goodbye).catch(err => console.error(err));
}
}

Loading…
Cancel
Save