-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
167 lines (151 loc) · 6.17 KB
/
main.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Innertube, UniversalCache, YTNodes } from 'youtubei.js';
import { LiveChatContinuation } from 'youtubei.js/dist/src/parser';
import LiveChat, { ChatAction, LiveMetadata } from 'youtubei.js/dist/src/parser/youtube/LiveChat';
import Video from 'youtubei.js/dist/src/parser/classes/Video';
import AddChatItemAction from 'youtubei.js/dist/src/parser/classes/livechat/AddChatItemAction';
import MarkChatItemAsDeletedAction from 'youtubei.js/dist/src/parser/classes/livechat/MarkChatItemAsDeletedAction';
import LiveChatTextMessage from 'youtubei.js/dist/src/parser/classes/livechat/items/LiveChatTextMessage';
import LiveChatPaidMessage from 'youtubei.js/dist/src/parser/classes/livechat/items/LiveChatPaidMessage';
import { exit } from 'process';
import Actions from 'youtubei.js/dist/src/core/Actions';
import { emitKeypressEvents } from 'readline';
import config from './config/config.json'
const robot = require("robotjs");
const prompt = require("prompt-sync")({ sigint: true });
const buttons = config.buttons as Record<string, string>
function randomIntFromInterval(min: any, max: any) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
function processQueueDemocratic(){
let tempQueue: Array<string> = [ ];
tempQueue = queueDemocratic.sort();
//empty current queue
queueDemocratic = []; //should work
//gotta figure out which word in the current queue has been spammed the most
const maxValue = tempQueue.reduce((previous, current, i, arr) => {
if (
arr.filter(item => item === previous).length >
arr.filter(item => item === current).length
) {
return previous;
} else {
return current;
}
});
robot.keyToggle(buttons[maxValue], 'down');
robot.keyToggle(buttons[maxValue], 'up');
tempQueue = [ ];
}
function processQueueNormal(){
//queue is adding new things at the bottom, so here we need to use index 0 and then remove with shift
if (!queue[0])
return;
let text = queue[0].message.toString().toLowerCase();
if (text.toLowerCase() in config.buttons && streamerControl == 0)
{
if (text == config.startButton && config.delayStartButton == "true")
{
if (randomIntFromInterval(1, 25) == 10) //inspired from twitch plays program doug made for alpharad to prevent stupid trolling. comment this line to disable
robot.keyTap(buttons[text]);
}
else
robot.keyToggle(buttons[text], 'down');
robot.keyToggle(buttons[text], 'up');
}
if (text == "control" && queue[0].author?.name.toString() == config.streamerName)
{
if (streamerControl == 1)
streamerControl = 0; //lets chat take control of game
else
streamerControl = 1; //pauses message processing
}
else if (text == "democratic" && queue[0].author?.name.toString() == config.streamerName)
{
//enable democratic mode
clearInterval(task);
setTimeout(processQueueDemocratic, config.democraticDelay);
toggleDemocraticMode = 1;
}
console.log(queue[0].author?.name.toString() +": " + text);
queue.shift();
}
console.log("\nWelcome to YoutubePlays, by XLuma!\n");
let streamerControl = 0; //kind of a switch to let the streamer take control of chat
let queue: Array<LiveChatTextMessage> = [ ];
let queueDemocratic: Array<string> = [ ];
let queueLength: number;
let task: NodeJS.Timeout;
let toggleDemocraticMode = 0;
(async () => {
if (!config.liveId || config.liveId.trim().length === 0) {
console.log("No liveId found ! Add your liveId to the config.json file inside the config folder");
exit(1);
}
robot.setKeyboardDelay(config.keyboardDelay);
const yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true });
console.log(config.liveId)
const info = await yt.getInfo(config.liveId);
console.log(info)
const livechat = info.getLiveChat();
livechat.on('start', (initial_data: LiveChatContinuation) => {
/**
* Initial info is what you see when you first open a Live Chat — this is; inital actions (pinned messages, top donations..), account's info and so on.
*/
console.info(`Hey ${initial_data.viewer_name || 'N/A'}, welcome to Live Chat!`);
});
livechat.on('chat-update', (action: ChatAction) => {
/**
* An action represents what is being added to
* the live chat. All actions have a `type` property,
* including their item (if the action has an item).
*
* Below are a few examples of how this can be used.
*/
if (action.is(YTNodes.AddChatItemAction)) {
const item = action.as(YTNodes.AddChatItemAction).item;
if (!item)
return console.info('Action did not have an item.', action);
const hours = new Date(item.hasKey('timestamp') ? item.timestamp : Date.now()).toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit'
});
switch (item.type) {
case 'LiveChatTextMessage':
if (!task)
{
if (toggleDemocraticMode == 1)
{
task = setTimeout(processQueueDemocratic, config.democraticDelay); //this should let the queue fill up for 5 seconds and then sort it, and make a move
}
else
task = setInterval(processQueueNormal, config.messageInterval);
}
if (toggleDemocraticMode == 1)
{
if (item.as(YTNodes.LiveChatTextMessage).message.toString().toLowerCase() == "anarchy" && (item.as(YTNodes.LiveChatTextMessage).author?.name.toString() == config.streamerName))
{
clearInterval(task);
task = setInterval(processQueueNormal, config.messageInterval);
}
queueLength = queueDemocratic.push(item.as(YTNodes.LiveChatTextMessage).message.toString().toLowerCase());
}
queueLength = queue.push(item.as(YTNodes.LiveChatTextMessage));
console.log(queueLength);
break;
case 'LiveChatPaidMessage':
console.info(
`${hours} - ${item.as(YTNodes.LiveChatPaidMessage).author.name.toString()}:\n` +
`${item.as(YTNodes.LiveChatPaidMessage).purchase_amount}\n`
);
break;
default:
console.debug(action);
break;
}
}
if (action.is(YTNodes.RemoveChatItemAction)) {
console.warn(`Message ${action.target_item_id} just got deleted!`, '\n');
}
});
livechat.start();
})();