Skip to content

Instantly share code, notes, and snippets.

@AlcaDesign
Last active February 24, 2019 05:07
Show Gist options
  • Save AlcaDesign/55f59772a478685d326262c223555e5e to your computer and use it in GitHub Desktop.
Save AlcaDesign/55f59772a478685d326262c223555e5e to your computer and use it in GitHub Desktop.
'use strict';
const tmi = require('tmi.js'),
timerMessages = [],
config = {
identity: {
username: '',
password: ''
},
channel: 'punisher_biz'
};
let client = new tmi.client({
identity: config.identity,
connection: {
reconnect: true
},
channels: [config.channel]
});
function isLive(channel) {
if(arguments.length < 2) {
throw new Error('Not enough arguments');
}
else if(typeof channel !== 'string') {
throw new TypeError('channel is not a string');
}
return new Promise((resolve, reject) => {
client.api({ // This is only how I would do it based on how you're doing it
url: `https://api.twitch.tv/kraken/streams?channel=${channel}`,
json: true, // Not necessary past [email protected] for NodeJS tmi.js
}, (err, response, body) => {
if(err) {
return reject(err);
}
resolve(body.hasOwnProperty('streams') && body.streams.length);
});
});
}
function createTimerMessage(time, cb) {
function createInterval() {
cleared = true;
intervalID = setInterval(wrapped, time);
return intervalID;
}
if(arguments.length < 1) {
throw new Error('Not enough arguments');
}
else if(typeof time !== 'number') {
throw new TypeError('time is not a number');
}
else if(typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
else if(time < 10000) {
throw new Error(`Interval time is probably too short! (${time/1000} second(s))`);
}
let args = [].slice.call(arguments, 2),
wrapped = () => {
if(client.readyState() === 'OPEN') {
cb();
}
},
intervalID = createInterval(),
cleared = false,
ret = {
time: () => time,
cb: () => cb,
id: () => intervalID,
state: () => !cleared,
changeTime: _time => {
time = _time;
this.restart();
return ret;
},
changeCB: _cb => {
cb = _cb;
this.restart();
return ret;
},
restart: () => {
this.stop();
createInterval();
return ret;
},
run: () => {
if(this.state()) {
this.restart();
}
wrapped();
return ret;
},
stop: () => {
clearInterval(intervalID);
intervalID = undefined;
cleared = true;
return ret;
},
};
return ret;
}
let followTimer = createTimerMessage(60*60*1000, () => { // Every hour
isLive(config.channel)
.then(live => {
console.log('Status:', live);
if(live) {
client.say(config.channel, 'Bitte nicht vergessen mir zu folgen, Danke <3');
}
})
.catch(e => console.log(e));
});
timerMessages.push(followTimer);
client.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment