Skip to content

Instantly share code, notes, and snippets.

@d0p3t
Created August 9, 2017 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0p3t/3b763f561fd4ae83f46ef7cb78e72bad to your computer and use it in GitHub Desktop.
Save d0p3t/3b763f561fd4ae83f46ef7cb78e72bad to your computer and use it in GitHub Desktop.
const tmi = require('tmi.js');
const discord = require('discord.js');
const config = require('./config/config.js'); // see https://github.com/d0p3t/d0p3tbot/blob/master/config/config_sample.js for an example
// Initialize tmi twitch bot and connect
const client = new tmi.client(config.tmi);
client.connect();
// Initialize discord bot and connect
const discordClient = new Discord.Client();
discordClient.login(config.discord.token);
var discordStreamingChecks = 50; // 5*50 = every 250 minutes checks. change this to check again later (like 96 for every 8 hours)
var embed, data, arr, discordChannel;
setInterval(function(){
client.api({
url: 'https://api.twitch.tv/kraken/streams/' + config.defaults.username,
method: 'GET',
headers: {
'Client-ID': config.tmi.options.clientId
}
}, function(err, res, body) {
if(err) return;
if(body.stream != null) {
logger.info('[Discord] Stream is online. Checking for last announcement...');
if(discordStreamingChecks === 0) discordStreamingChecks = 50;
if(discordStreamingChecks === 50) {
logger.info('[Discord] Triggering new announcement...');
var embed = new Discord.RichEmbed()
.setAuthor(config.defaults.username + " is LIVE on Twitch.TV!",body.stream.channel.logo)
.setTitle("Watch now! " + body.stream.channel.url)
.addField("Now Playing", body.stream.channel.game)
.setColor(0xFFA500)
.addField("Stream Title", body.stream.channel.status)
.setThumbnail(body.stream.preview.medium)
.addField("Followers", body.stream.channel.followers, true)
.addField("Total Views", body.stream.channel.views, true)
.addField("Current Viewers", body.stream.viewers, true)
.setFooter("Stream went live on: " + body.stream.created_at)
var data = { status: "idle", afk: false, game: { name: body.stream.channel.game, url: body.stream.channel.url } }
discordClient.user.setPresence(data);
var arr = discordClient.channels;
var discordChannel = arr.find(o => o.id === 'discordchannelid'); // ID of channel to send to, easiest to go into discord and go to settings of the text channel, it will show the ID there
discordChannel.send({embed});
}
discordStreamingChecks--;
}
else {
data = { status: "online", afk: false, game: { name: null } }
discordClient.user.setPresence(data);
discordStreamingChecks = 50;
}
});
}, 300000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment