-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
59 lines (47 loc) · 1.7 KB
/
bot.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
const youtube = require('./youtube/youtube-comments');
const twitch = require('./twitch/twitch-chat');
const config = require('./config/bot-config');
/**
* Async wait
*/
const wait = async (ms) => new Promise((res, rej) => {
setTimeout(() => res(), ms);
});
/**
* Random value from the specified array
*/
const randomFrom = arr => arr[Math.floor(Math.random() * arr.length)];
/**
* Application entry point
*/
const main = async () => {
// -- Load YouTube Comments for Configured Video Ids
const requiredPhrases = config.youtube.comments.requiredPhrases
.map(phrase => phrase.toLowerCase());
const requiredLength = config.youtube.comments.requiredLength;
const videoIds = config.youtube.videoIds;
youtube.init(config.youtube.apiKey);
const comments = await youtube.comments({
videoIds,
requiredPhrases,
requiredLength
});
console.log(`Successfully loaded ${comments.length} comments from ${videoIds.length} videos.`);
// -- Connect to Twitch
await twitch.connect({
user: config.twitch.username,
oauthToken: config.twitch.token,
channel: config.twitch.channel
});
// -- Run Loop Selecting random comment index and chatting
const minChatInterval = config.twitch.chat.min * 1000;
const maxChatInterval = (config.twitch.chat.max * 1000) - minChatInterval;
while(true) {
const comment = randomFrom(comments);
await twitch.chat({ text: comment });
const nextWait = minChatInterval + Math.floor(Math.random() * maxChatInterval);
console.log(`Waiting ${nextWait / 1000} seconds...`);
await wait(nextWait);
}
};
main().then(console.log).catch(console.error);