twitch-chat/index.js

56 lines
1.3 KiB
JavaScript

const port = 3000;
const fs = require('fs');
const tmi = require('tmi.js');
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
// Define configuration options
const opts = JSON.parse(fs.readFileSync('settings.json'));
// 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, message, self) {
io.emit("message", {
target,
context,
message,
self
});
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler() {
console.log("Connected to twitch");
}
app.get('/', function(req, res){
res.sendFile(__dirname + '/static/index.html');
});
app.use('/static', express.static('static'));
io.on('connection', function(socket){
socket.on('message', function(message) {
io.emit('message', message);
});
socket.on('disconnect', function() {
});
});
http.listen(port, function(){
console.log('Listening on port ' + port);
});