This repository was archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold.js
More file actions
313 lines (274 loc) · 8.37 KB
/
old.js
File metadata and controls
313 lines (274 loc) · 8.37 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
require("dotenv/config");
const { LiveChat } = require("youtube-chat");
const botID = "UC8qtw3cJ0A0ItDcLCnJzstg";
const chat = new LiveChat({
liveId: require("./config.json").livestreamId,
});
const Enmap = require("enmap");
/** @type {Enmap.default} */
const xpDb = new Enmap({
name: "xpDatabase",
dataDir: "./db/xp",
});
/** @type {Enmap.default} */
const pointsDb = new Enmap({
name: "pointsDatabase",
dataDir: "./db/points",
});
/** @type {Enmap.default} */
const votesDb = new Enmap({
name: "votesDatabase",
dataDir: "./db/votes",
});
Object.keys(require("./oldVotes.json")).forEach((key) => {
votesDb.ensure(key, require("./oldVotes.json")[key]);
});
/** @type {Enmap.default} */
const countDb = new Enmap({
name: "countDatabase",
dataDir: "./db/count",
});
countDb.ensure("count", 0);
countDb.ensure("lastCounted", []);
const activeUsers = new Map();
chat.on("start", () => {
setTimeout(() => {
chat.on("chat", chatHandler);
}, 4000);
});
chat.start();
require("./server.js")({
xp: xpDb,
votes: votesDb,
points: pointsDb,
count: countDb,
activeUsers,
});
const ignoredCommands = [
"!sr",
"!songrequest",
"!skip",
"!currentsong",
"!nextsong",
"!pizza",
"!uptime",
"!points",
"!hours",
"!top",
"!tophours",
"!addpoints",
"!removepoints",
"!give",
"!gamble",
"!addcommand",
"!removecommand",
"!editcommand",
"!enable",
"!playlist",
"!rules",
"!discord",
];
const random = (min, max) =>
Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min) + 1)) + min;
setInterval(() => {
const keys = [...activeUsers.keys()];
for (const key of keys) {
const date = activeUsers.get(key).date;
if (new Date() - new Date(date) > 1 * 60 * 1000) {
activeUsers.delete(key);
continue;
}
pointsDb.ensure(key, {
points: 0,
});
pointsDb.math(key, "+", random(1, 5), "points");
}
if (activeUsers.size !== 0)
sendMessage(
`${activeUsers.size} ${
activeUsers.size === 1 ? "person has" : "people have"
} been given points!`
);
}, 1 * 60 * 1000);
/**
* @param {import('youtube-chat/dist/types/data').ChatItem} chat
*/
function chatHandler(chat) {
console.log(chat);
if (chat.author.channelId === botID) return;
const message = chat.message
.map((item) => (item.emojiText ? `:${item.emojiText}:` : item.text))
.join(" ");
if (message.startsWith("!rank") || message.startsWith("!xp")) {
xpDb.ensure(chat.author.channelId, {
xp: 0,
level: 0,
});
sendMessage(
`${chat.author.name}, you have ${xpDb
.get(chat.author.channelId, "xp")
.toLocaleString()} xp and you're level ${xpDb
.get(chat.author.channelId, "level")
.toLocaleString()}. To reach the next level, you need to reach ${calculateLevelXp(
xpDb.get(chat.author.channelId, "level") + 1
)} xp.`
);
return;
} else if (message.startsWith("!points")) {
pointsDb.ensure(chat.author.channelId, {
points: 0,
});
sendMessage(
`${chat.author.name}, you have ${pointsDb.get(
chat.author.channelId,
"points"
)} points.`
);
return;
} else if (
message.startsWith("!") &&
!ignoredCommands.some((command) =>
message.toLowerCase().startsWith(command)
)
) {
const votee = message.split("!")[1].split(" ")[0].toLowerCase();
votesDb.ensure(votee, 0);
votesDb.math(votee, "+", 1);
sendMessage(
`${chat.author.name} has voted for ${votee}, and so has ${votesDb
.get(votee)
.toLocaleString()} other people!`
);
return;
} else if (message.toLowerCase() === "h") {
votesDb.ensure("h", 0);
votesDb.math("h", "+", 1);
sendMessage(`h. ${votesDb.get("h")} other people have h'ed.`);
return;
}
const messageObj = {
content: message,
author: { name: chat.author.name, id: chat.author.channelId },
};
level(messageObj);
points(messageObj);
counting(messageObj);
const hellos = ["hello", "hi", "hey"];
if (hellos.some((hello) => message.startsWith(hello))) {
sendMessage(
`Hello ${chat.author.name}! Welcome to the Graphify stream! We hope you enjoy your stay.`
);
return;
}
const goodbyes = ["bye", "cya", "gtg", "goodbye"];
if (goodbyes.some((goodbye) => message.startsWith(goodbye))) {
sendMessage(
`Goodbye ${chat.author.name}! We hope to see you here again soon!`
);
return;
}
}
const xpCooldowns = new Set();
const calculateLevelXp = (level) => 100 * level || 1;
/**
* @param {{ content: string; author: { name: string, id: string; } }} message
*/
function level(message) {
if (xpCooldowns.has(message.author.id)) return;
const xpToGive = random(5, 15);
xpDb.ensure(message.author.id, {
xp: 0,
level: 0,
});
xpDb.math(message.author.id, "+", xpToGive, "xp");
xpCooldowns.add(message.author.id);
setTimeout(() => xpCooldowns.delete(message.author.id), 30_000);
const user = xpDb.get(message.author.id);
if (user.xp > calculateLevelXp(user.level)) {
xpDb.math(message.author.id, "+", 1, "level");
sendMessage(
`LEVEL UP! ⬆️ ${message.author.name} has leveled up to level ${xpDb
.get(message.author.id, "level")
.toLocaleString()}.`
);
}
}
/**
* @param {{ content: string; author: { name: string, id: string; } }} message
*/
function points(message) {
activeUsers.set(message.author.id, {
name: message.author.name,
date: Date.now(),
});
pointsDb.ensure(message.author.id, {
messages: 0,
});
pointsDb.math(message.author.id, "+", 1, "messages");
}
/**
* @param {{ content: string; author: { name: string, id: string; } }} message
*/
function counting(message) {
const firstArg = message.content.split(" ")[0].split(",").join("");
if (!isNaN(firstArg)) {
const num = parseInt(firstArg);
if (num !== countDb.get("count") + 1) return;
countDb.set("count", num);
let lastCounted = countDb.get("lastCounted");
if (lastCounted.length > 9) lastCounted = lastCounted.slice(0, 9);
lastCounted.unshift({
name: message.author.name,
id: message.author.id,
count: num,
});
countDb.set("lastCounted", lastCounted);
}
}
const axios = require("axios").default;
async function sendMessage(text) {
const body = {
...require("./body.json"),
richMessage: { textSegments: [{ text }] },
};
const { data } = await axios.post(
"https://www.youtube.com/youtubei/v1/live_chat/send_message?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false",
body,
{
headers: {
accept: "*/*",
"accept-language": "en-US,en;q=0.9",
authorization: process.env.AUTHORIZATION,
"content-type": "application/json",
"sec-ch-ua":
'"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"115.0.5790.110"',
"sec-ch-ua-full-version-list":
'"Not/A)Brand";v="99.0.0.0", "Google Chrome";v="115.0.5790.110", "Chromium";v="115.0.5790.110"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": '""',
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua-platform-version": '"15.0.0"',
"sec-ch-ua-wow64": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "same-origin",
"sec-fetch-site": "same-origin",
"x-client-data": process.env.CLIENT_DATA,
"x-goog-authuser": "0",
"x-goog-pageid": process.env.GOOG_PAGE_ID,
"x-goog-visitor-id": process.env.GOOG_VISITOR_ID,
"x-origin": "https://www.youtube.com",
"x-youtube-bootstrap-logged-in": "true",
"x-youtube-client-name": "1",
"x-youtube-client-version": "2.20230728.00.00",
cookie: process.env.COOKIE,
Referer:
"https://www.youtube.com/live_chat?continuation=0ofMyAN-Gl5DaWtxSndvWVZVTm5SelZoVW1OWlIzcFFVRUkwVlVjemJWTXRXazVuRWd0RWEzcGtjMEl0TlU5NVl4b1Q2cWpkdVFFTkNndEVhM3BrYzBJdE5VOTVZeUFCTUFBJTNEMAGCAQYIBBgCIACIAQGgAbzmxqyXs4ADqAEAsgEA",
"Referrer-Policy": "strict-origin-when-cross-origin",
},
}
);
return data;
}