Added request for database.

This commit is contained in:
David Huh 2021-04-20 10:50:37 +02:00
parent bb7c356b39
commit f5a43c897c
3 changed files with 805 additions and 43 deletions

99
bot.js
View File

@ -1,6 +1,8 @@
require('dotenv').config();
const tmi = require('tmi.js');
const https = require('https');
const request = require('request');
const options = {
hostname: 'api.twitch.tv',
port: 443,
@ -13,7 +15,6 @@ const options = {
}
}
// Define configuration options
const opts = {
identity: {
@ -106,47 +107,59 @@ function onMessageHandler(target, context, msg, self) {
});
res.on('end', () => {
// Parse data as JSON to variable stream
stream = JSON.parse(Buffer.concat(data).toString());
// Parse data as JSON to variable stream
stream = JSON.parse(Buffer.concat(data).toString());
// Check if stream is online or not
if (!stream || !stream['stream'] || !stream['stream']['created_at']) {
client.say(target, `You can't do that now!`);
return;
}
const stream_time = stream['stream']['created_at'];
const dateStart = new Date(stream_time);
const dateEnd = new Date();
const timeDifference = Math.abs(dateEnd - dateStart);
streamer = "yiggalow";
stream_id = stream['stream']['_id'];
stream_game = stream['stream']['game'];
timestamp = getTimeInFormat(timeDifference, false);
description = commandName.substr(6, commandName.length - 1);
if (description.trim().length < 3) {
description = "No Description";
}
username = context["username"];
// Post request to database
request.post(
'https://api.plutodev.de/twitch-highlight-marker/addHighlight',
{
json: {
access_key: process.env.ACCESS_KEY,
streamer: streamer,
stream_id: stream_id,
stream_game: stream_game,
timestamp: timestamp,
description: description,
username: username
}
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
// ENGLISH: client.say(target, `Mark has been tracked for the next highlight video. Thank you for your submission ${username}! `);
client.say(target, `Die Markierung wurde für das nächste Highlight Video gespeichert. Danke für deine Hilfe ${username}!`);
console.log(body);
} else {
client.say(target, `Leider ist etwas schief gelaufen. Versuch es bitte später erneut.`);
}
}
);
// Check if stream is online or not
if (!stream || !stream['stream'] || !stream['stream']['created_at']) {
client.say(target, `You can't do that now!`);
return;
}
const stream_time = stream['stream']['created_at'];
const dateStart = new Date(stream_time);
const dateEnd = new Date();
const timeDifference = Math.abs(dateEnd - dateStart);
streamer = "yiggalow";
stream_id = stream['stream']['_id'];
stream_game = stream['stream']['game'];
timestamp = getTimeInFormat(timeDifference, false);
description = commandName.substr(6, commandName.length - 1);
if (description.trim().length < 3) {
description = "No Description";
}
username = context["username"];
const json_for_sql = {
access_key: process.env.ACCESS_KEY,
streamer: streamer,
stream_id: stream_id,
stream_game: stream_game,
timestamp: timestamp,
description: description,
username: username
};
// TODO send API request to database
// ENGLISH: client.say(target, `Mark has been tracked for the next highlight video. Thank you for your submission ${username}! `);
client.say(target, `Die Markierung wurde für das nächste Highlight Video gespeichert. Danke für deine Hilfe ${username}! `);
});
);
})
req.end();
} else {
@ -158,13 +171,13 @@ function onMessageHandler(target, context, msg, self) {
// readable decides if the words "hours", "minutes" and "seconds" should be added
function getTimeInFormat(timeInMillis, readable) {
let hours = Math.floor(timeInMillis / 1000 / 60 / 60);
timeInMillis -= hours * 1000 * 60 * 60
timeInMillis -= hours * 1000 * 60 * 60;
let minutes = Math.floor((timeInMillis) / 1000 / 60);
timeInMillis -= minutes * 1000 * 60
timeInMillis -= minutes * 1000 * 60;
let seconds = Math.floor((timeInMillis) / 1000);
if (readable) {
return hours + " Stunde/n " + minutes + " Minute/n " + seconds + " Sekunde/n"
return hours + " Stunde/n " + minutes + " Minute/n " + seconds + " Sekunde/n";
} else {
return hours + ":" + minutes + ":" + seconds;
}

746
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,5 +16,8 @@
"dotenv": "^8.2.0",
"tmi.js": "^1.8.2",
"twitch-js": "*"
},
"devDependencies": {
"request": "^2.88.2"
}
}