-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (61 loc) · 2.17 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
63
64
65
66
67
68
69
import fs from 'fs'
import tmi from 'tmi.js'
import { google } from 'googleapis'
import { formatDate } from './utils'
import { init, addToPlaylist } from './quickstart'
import { BOT_USERNAME, OAUTH_TOKEN, CHANNEL_NAME, YOUTUBE_REGEX, YOUTUBE_VID_ID_REGEX } from './constants'
const options = {
options: {
debug: false
},
connection: {
reconnect: true,
secure: true
},
identity: {
username: BOT_USERNAME,
password: OAUTH_TOKEN
},
channels: [ CHANNEL_NAME ]
}
// initialize a client w/ options to connect to Twitch server
const client = new tmi.Client(options);
let oauth2Client = null;
// connect to Twitch server
client.connect();
// invoked every time bot connects to Twitch chat
client.on("connected", (addr, port) => {
try {
client.say(options.channels[0], 'swoocn override - [youtube-request-twitch-bot] initiated for testing..');
// client.ws.on('message', data => console.log(data));
console.log(`[youtube-request-twitch-bot initiated] - connected to ${addr}:${port}`);
// authorize a client with the loaded credentials, then call the YouTube API.
oauth2Client = init();
console.log('connected to YouTube Data API..');
} catch (err) {
console.log(`unable to process connect; -> ${err}`);
}
});
// invoked every time a message is received by the bot
client.on("message", (target, context, msg, self) => {
if (self) { return; }
if (new RegExp(YOUTUBE_REGEX).test(msg)) {
try {
// extract ID from YouTube link where id[1] is the vid ID
var id = msg.match(YOUTUBE_VID_ID_REGEX);
fs.appendFile('output/youtube.list', `[${formatDate(new Date())}]: @${context.username} - ${id[0]}\n`, (err) => {
if (err) throw err;
console.log(`updated -> @${context.username} - ${id[1]}`);
// invoke YouTube Data API to add item to playlist
addToPlaylist(oauth2Client, id[1]);
});
} catch (err) {
console.log(`unable to process message: ${msg}; -> ${err}`);
}
}
});
// invoked every time the bot disconnects from Twitch chat
client.on("disconnected", (reason) => {
console.log(`[youtube-request-twitch-bot disconnected] - ${reason}`);
process.exit(1);
});