196 lines
6.8 KiB
JavaScript
196 lines
6.8 KiB
JavaScript
require('dotenv').config();
|
|
const tmi = require('tmi.js');
|
|
const https = require('https');
|
|
const request = require('request');
|
|
|
|
const options = {
|
|
hostname: 'api.twitch.tv',
|
|
port: 443,
|
|
path: '/kraken/streams/149689994',
|
|
method: 'GET',
|
|
headers: {
|
|
"client-id": "gp762nuuoqcoxypju8c569th9wz7q5",
|
|
"Authorization": ("Bearer " + process.env.API_BEARER),
|
|
"Accept": "application/vnd.twitchtv.v5+json"
|
|
}
|
|
}
|
|
|
|
// Define configuration options
|
|
const opts = {
|
|
identity: {
|
|
username: "HighlightMarkerBot",
|
|
password: process.env.IRC_OAUTH
|
|
},
|
|
channels: [
|
|
"yiggalow"
|
|
]
|
|
};
|
|
|
|
// Create a client with our options
|
|
const client = new tmi.client(opts);
|
|
|
|
// Register our event handlers (defined below)
|
|
client.on('message', onMessageHandler);
|
|
client.on('connected', onConnectedHandler);
|
|
|
|
// Connect to Twitch:
|
|
client.connect();
|
|
|
|
// Called every time a message comes in
|
|
function onMessageHandler(target, context, msg, self) {
|
|
if (self) {
|
|
return;
|
|
} // Ignore messages from the bot
|
|
|
|
// Remove whitespace from chat message
|
|
const commandName = msg.trim();
|
|
|
|
// If the command is known, let's execute it
|
|
if (commandName === '!dice') {
|
|
console.log(`* Executed ${commandName} command`);
|
|
|
|
const num = rollDice();
|
|
client.say(target, `You rolled a ${num}`);
|
|
} else if (commandName === '!uptime') {
|
|
console.log(`* Executed ${commandName} command`);
|
|
|
|
let stream;
|
|
let stream_time;
|
|
|
|
// Send request to Twitch API with client-id and oauth token
|
|
const req = https.request(options, res => {
|
|
let data = [];
|
|
|
|
res.on('data', chunk => {
|
|
// Saving data of response to variable data
|
|
data.push(chunk);
|
|
});
|
|
|
|
res.on('end', () => {
|
|
// 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, `Uptime: not available`);
|
|
return;
|
|
}
|
|
|
|
stream_time = stream['stream']['created_at'];
|
|
const dateStart = new Date(stream_time);
|
|
const dateEnd = new Date();
|
|
const timeDifference = Math.abs(dateEnd - dateStart);
|
|
|
|
client.say(target, `Uptime: ${getTimeInFormat(timeDifference, true)}`);
|
|
});
|
|
})
|
|
req.end();
|
|
} else if (commandName.startsWith('!mark')) { // Description comes after a whitespace e.g.: "!mark DESCRIPTION"
|
|
console.log(`* Executed ${commandName} command`);
|
|
|
|
let streamer;
|
|
let stream;
|
|
let stream_id;
|
|
let stream_game;
|
|
let description;
|
|
let username;
|
|
let timestamp;
|
|
|
|
|
|
// Send request to Twitch API with client-id and oauth token
|
|
const req = https.request(options, res => {
|
|
let data = [];
|
|
|
|
res.on('data', chunk => {
|
|
// Saving data of response to variable data
|
|
data.push(chunk);
|
|
});
|
|
|
|
res.on('end', () => {
|
|
// 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.`);
|
|
}
|
|
}
|
|
);
|
|
|
|
}
|
|
);
|
|
})
|
|
req.end();
|
|
} else {
|
|
console.log(`* Unknown command ${commandName}`);
|
|
}
|
|
}
|
|
|
|
// Function for formatting time from millis to normal
|
|
// 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;
|
|
let minutes = Math.floor((timeInMillis) / 1000 / 60);
|
|
timeInMillis -= minutes * 1000 * 60;
|
|
let seconds = Math.floor((timeInMillis) / 1000);
|
|
|
|
if (readable) {
|
|
return hours + " Stunde/n " + minutes + " Minute/n " + seconds + " Sekunde/n";
|
|
} else {
|
|
return hours + ":" + minutes + ":" + seconds;
|
|
}
|
|
}
|
|
|
|
// Function called when the "dice" command is issued
|
|
function rollDice() {
|
|
const sides = 6;
|
|
return Math.floor(Math.random() * sides) + 1;
|
|
}
|
|
|
|
// Called every time the bot connects to Twitch chat
|
|
function onConnectedHandler(addr, port) {
|
|
console.log(`* Connected to ${addr}:${port}`);
|
|
}
|