✨ Adding basic bot functionality
This commit is contained in:
parent
acb067f59e
commit
31531dabff
48
bot.js
48
bot.js
|
@ -1,5 +1,18 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const tmi = require('tmi.js');
|
const tmi = require('tmi.js');
|
||||||
|
const https = require('https');
|
||||||
|
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
|
// Define configuration options
|
||||||
const opts = {
|
const opts = {
|
||||||
|
@ -36,11 +49,46 @@ function onMessageHandler(target, context, msg, self) {
|
||||||
const num = rollDice();
|
const num = rollDice();
|
||||||
client.say(target, `You rolled a ${num}`);
|
client.say(target, `You rolled a ${num}`);
|
||||||
console.log(`* Executed ${commandName} command`);
|
console.log(`* Executed ${commandName} command`);
|
||||||
|
} else if (commandName === '!uptime') {
|
||||||
|
let stream;
|
||||||
|
let stream_time;
|
||||||
|
const req = https.request(options, res => {
|
||||||
|
let data = [];
|
||||||
|
|
||||||
|
res.on('data', chunk => {
|
||||||
|
data.push(chunk);
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
stream = JSON.parse(Buffer.concat(data).toString());
|
||||||
|
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)}`);
|
||||||
|
console.log(`* Executed ${commandName} command`);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
req.end();
|
||||||
} else {
|
} else {
|
||||||
console.log(`* Unknown command ${commandName}`);
|
console.log(`* Unknown command ${commandName}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTimeInFormat(timeInMillis) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
return hours + " Stunde/n " + minutes + " Minute/n " + seconds + " Sekunde/n"
|
||||||
|
}
|
||||||
|
|
||||||
// Function called when the "dice" command is issued
|
// Function called when the "dice" command is issued
|
||||||
function rollDice() {
|
function rollDice() {
|
||||||
const sides = 6;
|
const sides = 6;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user