-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitchChatService.js
103 lines (79 loc) · 2.63 KB
/
twitchChatService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Created by p0rp on 10/19/2016.
*/
var tmi = require("tmi.js");
//////////// CONFIG ////////////////////////
var config = require('./my_config.json');
var CHANNELS = config.api.twitch.channels;
var USER = config.api.twitch.username;
var PASSWORD = config.api.twitch.password;
// var PASSWORD = 'oauth:twitchAuthTokenString'; // can use auth token instead
//////////////////////////////////////////
var PLATFORM = 'TWITCH' + CHANNELS[0]; // very sloppy
var LAST_MESSAGE = '';
var redis = require('redis'); // get Redis
var sub = redis.createClient(), pub = redis.createClient(); // get pub and sub objects
var options = {
options: {
debug: true
},
connection: {
reconnect: true
},
identity: {
username: USER,
password: PASSWORD
},
channels: CHANNELS
};
var client = new tmi.client(options);
// Connect the client to the server..
client.connect();
client.on("message", function (channel, userstate, message, self) {
// Don't listen to my own messages..
if (self) return;
// Handle different message types..
switch(userstate["message-type"]) {
case "action":
// This is an action message..
break;
case "chat":
// This is a chat message..
console.log("handling chat message!");
console.log("userstate: ", userstate);
console.log("username: ", userstate.username);
console.log("message: ", message);
var data = {
platform: PLATFORM,
"channel": channel,
"user": userstate.username,
"userID": userstate['user-id'],
"message": message
}
data = JSON.stringify(data);
console.log("Pre-Pub Data: ", data);
pub.publish('chat', data);
break;
case "whisper":
// This is a whisper..
break;
default:
// Something else ?
break;
}
});
sub.on("message", function (channel, message) {
// handle incoming redis message
console.log("Message: ", channel, " - ", message);
var data = JSON.parse(message);
console.log("data: ", data);
console.log("data: ", data.message);
if (data['platform'] != PLATFORM) {
// ok now format the message for sending
var msg = '[' + data['user'] + '] - ' + data['message'];
console.log(msg);
client.say(CHANNEL,msg);
LAST_MESSAGE = msg;
}
});
sub.subscribe("chat");