-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
105 lines (95 loc) · 2.35 KB
/
index.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
104
105
"use strict"
const IRC = require('irc')
const _ = require('lodash')
const parser = require('./src/parser')
function Bot({
username=null,
oauth=null,
channel=null
}) {
if(!username || !oauth || !channel) {
throw new Error('Bot() requires options argument')
}
this.username = username
this.oauth = oauth
this.channel = channel.toLowerCase()
this.client = null
}
Bot.prototype = {
connect() {
return new Promise((resolve, reject) => {
this.client = new IRC.Client('irc.chat.twitch.tv', this.username, {
port: 443,
password: this.oauth,
channels: ['#' + this.channel],
debug: false,
secure: true,
autoConnect: false
})
this.client.connect(connected => {
if(!connected) reject()
if(connected.rawCommand === '001') {
this.client.send('CAP REQ', 'twitch.tv/membership')
this.client.send('CAP REQ', 'twitch.tv/tags')
this.client.send('CAP REQ', 'twitch.tv/commands')
resolve()
}
})
this.client.addListener('error', err => {
console.log('CONNECTION ERROR')
console.log(err)
reject(err)
})
})
},
listen(callback) {
return new Promise((resolve, reject) => {
this.raw((err, event) => {
if(err) {
resolve(callback(err))
} else {
if(event.commandType === 'normal') {
const split = event.command.split(';')
if(_.includes(split[2], 'display-name=') && !_.includes(event.args[0], 'USERSTATE')) {
parser.createChatter(event)
.then(chatter => resolve(callback(null, chatter)))
.catch(err => resolve(callback(err)))
}
}
}
})
})
},
listenFor(word, callback) {
return new Promise((resolve, reject) => {
this.raw((err, event) => {
if(err) {
resolve(callback(err))
} else {
if(event.commandType === 'normal') {
const split = event.command.split(';')
if(_.includes(split[2], 'display-name=')) {
parser.exactMatch(event, word)
.then(chatter => resolve(callback(null, chatter)))
.catch(err => resolve(callback(err)))
}
}
}
})
})
},
raw(cb_event) {
return new Promise((resolve, reject) => {
this.client.addListener('raw', event => {
resolve(cb_event(null, event))
})
this.client.addListener('error', err => {
resolve(cb_event(err))
})
})
},
msg(text) {
this.client.send('PRIVMSG #' + this.channel, text)
}
}
module.exports = Bot