forked from diceguyd30/queso_to_go_template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
chatbot.js
83 lines (76 loc) · 2.23 KB
/
chatbot.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
const tmi = require('tmi.js');
const build_chatter = function(username, displayName, isSubscriber, isMod, isBroadcaster) {
return {
username: username,
displayName: displayName,
isSubscriber: isSubscriber,
isMod: isMod,
isBroadcaster: isBroadcaster
}
}
const chatbot_helper = function(username, password, channel) {
var tmi_settings = {
identity: {
username: username,
password: password
},
connection: {
reconnect: true,
maxReconnectAttempts: 50,
secure: true,
timeout: 20000
},
channels: [
channel
]
};
return {
client: new tmi.client(tmi_settings),
connect: function() {
// Connect to Twitch:
this.client.connect();
},
setup: function(handle_func) {
var client = this.client;
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}
// Called every time a message comes in
function onMessageHandler(channel, tags, message, self) {
if (self) { return; } // Ignore messages from the bot
// Remove whitespace from chat message
const command = message.trim();
const respond = (response_text) => {
client.say(channel, response_text);
};
var chatter;
if (tags.badges == null) {
chatter = build_chatter(tags.username,
tags['display-name'],
false,
false,
false);
} else {
chatter = build_chatter(tags.username,
tags['display-name'],
tags.badges.subscriber != undefined || tags.badges.founder != undefined,
tags.badges.moderator != undefined,
tags.badges.broadcaster != undefined);
}
handle_func(command, chatter, respond);
}
// Register our event handlers (defined below)
this.client.on('connected', onConnectedHandler);
this.client.on('message', onMessageHandler);
},
say: function(message) {
this.client.say('#' + channel, message);
}
};
};
module.exports = {
helper: function(username, password, channel) {
return chatbot_helper(username, password, channel);
},
};