Created
December 6, 2015 14:13
-
-
Save markhc/1208bf8809d423016ca6 to your computer and use it in GitHub Desktop.
TwitchChat Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var irc = require("tmi.js"); | |
var mysql = require('mysql'); | |
var database_options = { | |
host : 'localhost', | |
user : 'root', | |
password : 'password', | |
database : 'irc_logger' | |
}; | |
var connection = mysql.createConnection(database_options); | |
function logChat(channel, user, message){ | |
connection.beginTransaction(function(err) { | |
if (err) { throw err; } | |
connection.query( | |
'INSERT INTO messages (channel, user, text, emotes, fullInfo) VALUES (?,?,?,?,?)', | |
[channel, user.username, message, JSON.stringify(user.emotes), JSON.stringify(user)], | |
function(err, result) { | |
if(!err){ | |
}else{ | |
return connection.rollback(function() {throw err; }); | |
} | |
} | |
); | |
connection.commit(function(err) { | |
if (err) { | |
return connection.rollback(function() { | |
throw err; | |
}); | |
} | |
console.log("Logged: ["+channel+"]["+user.username+"] " + message ); | |
}); | |
}); | |
} | |
var IRCLogger = function(games){ | |
this.client = new irc.client({ | |
options: { | |
debug: false | |
}, | |
connection: { | |
random: "chat", | |
reconnect: true | |
}, | |
identity: { | |
username: "username", | |
password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
} | |
}); | |
this.client.games = games; | |
this.client.on("connected", function (address, port) { | |
var now = new Date(); | |
console.log("[" + now.toUTCString() + "] Connected"); | |
for(var i = 0; i < this.games.length; i++){ | |
//Gets the 10 top streams for the game and add them to the log list | |
var game = this.games[i].replace(' ', '+'); | |
var _this = this; | |
this.api({ | |
url: "https://api.twitch.tv/kraken/streams?game="+game+"&limit=10", | |
method: "GET", | |
headers: {"Accept": "application/vnd.twitchtv.v3+json"} | |
}, | |
function(err, res, body) { | |
if(!err){ | |
var response = JSON.parse(body); | |
for(var key in response.streams){ | |
if (response.streams.hasOwnProperty(key)) { | |
console.log(response.streams[key].channel.name); | |
_this.join('#'+response.streams[key].channel.name); | |
} | |
} | |
} | |
}); | |
} | |
}); | |
this.client.on("connecting", function (address, port) { | |
var now = new Date(); | |
console.log("[" + now.toUTCString() + "] Connecting to " + address + ":" + port); | |
}); | |
this.client.on("disconnected", function (reason) { | |
var now = new Date(); | |
console.log("[" + now.toUTCString() + "] Disconnected"); | |
}); | |
this.client.on("chat", function (channel, user, message, self) { | |
logChat(channel, user, message); | |
}); | |
this.client.on("join", function (channel, username) { | |
var now = new Date(); | |
console.log("[" + now.toUTCString() + "] Joining " + channel); | |
}); | |
}; | |
IRCLogger.prototype.start = function() { | |
this.client.connect(); | |
}; | |
IRCLogger.prototype.restart = function() { | |
this.client.disconnect(); | |
this.client.connect(); | |
}; | |
var logger = new IRCLogger(["Dota 2"]); | |
logger.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment