-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.51 KB
/
app.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
let axios = require('axios');
let OAuth2 = require('oauth20');
var irc = require('twitch-irc-lite');
let credentialsTwitch = Symbol('credentialsTwitch');
let chatTwitch = Symbol('chatTwitch');
let urlsTwitch = Symbol('urlsTwitch');
let getTwitch = Symbol('getTwitch');
class Twitch extends OAuth2 {
constructor(clientId, clientSecret, redirectUrl, scopes, userLogin, accessToken='') {
super(clientId, clientSecret, redirectUrl, scopes, accessToken, 'https://api.twitch.tv/kraken/oauth2/');
this[credentialsTwitch] = {
userLogin: userLogin,
clientId: clientId,
userId: ''
};
this[urlsTwitch] = {
channels: 'users',
streams: 'streams'
};
this.axiosTwitch = axios.create({
baseURL: 'https://api.twitch.tv/helix/'
});
}
connectChat() {
this[chatTwitch] = new irc(`oauth:${this.getCredentials().accessToken}`, this[credentialsTwitch].userLogin);
this[chatTwitch].join(this[credentialsTwitch].userLogin);
}
getStream() {
let url = this[urlsTwitch].streams;
let params = {
'user_login': this[credentialsTwitch].userLogin
};
return this[getTwitch](url, params);
}
chat(callback) {
this[chatTwitch].chatEvents.addListener('message', (channel, from, msg) => {
callback(from, msg);
});
}
[getTwitch](url, params) {
return this.axiosTwitch({
method: 'GET',
url: url,
params: params,
headers: {
Authorization: `OAuth ${this.getCredentials().accessToken}`,
'Client-ID': this[credentialsTwitch].clientId
}
});
}
}
module.exports = Twitch;