-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
143 lines (124 loc) · 5.32 KB
/
index.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
import nodeNotifier from "node-notifier";
import json5 from "json5";
import chalk from "chalk";
import fetch from "node-fetch";
import { readFileSync } from "fs";
const { notify } = nodeNotifier;
const { parse } = json5;
class Logger {
public static log(type: string, info: string): void {
type.toUpperCase();
console.log(chalk.greenBright(`${chalk.bold(`[${type}]`)} \t${info}`));
}
public static error(type: string, info: string): void {
type.toUpperCase();
console.log(chalk.redBright(`${chalk.bold(`[${type}]`)} \t${info}`));
}
public static warn(type: string, info: string): void {
type.toUpperCase();
console.log(chalk.yellowBright(`${chalk.bold(`[${type}]`)} \t${info}`));
}
}
(async (): Promise<void> => {
Logger.log("STARTUP", "Starting bloxflip-rain...");
interface configInt {
minimum: number;
delay: number;
os_notifs: boolean;
webhook: {
enabled: boolean;
link: string;
};
}
let config: configInt;
try {
config = parse(readFileSync("./config.json", { encoding: "utf8" }));
if (config.delay < 1000) {
Logger.error("CONFIG", "Delay cant be lower than 1 second.");
return;
}
if (!config.os_notifs && !config.webhook.enabled) {
Logger.error("CONFIG", "Either OS notifications or webhooks should be enabled.");
return;
}
Logger.log("CONFIG", "Successfully parsed config");
} catch (err) {
Logger.error("CONFIG", `Unable to parse config\n ${err}`);
return;
}
Logger.log("RAIN", "\tStarting rain notifier...");
const webhookLink: string = config.webhook.link;
let notified = false;
for (let i = 0; i < Infinity; i++) {
try {
const bfRes: any = await (await fetch("https://rest-bf.blox.land/chat/history")).json();
if (bfRes.rain.active) {
if (!notified) {
if (bfRes.rain.prize >= config.minimum) {
Logger.log("RAIN", `Rain Detected \nRobux: ${bfRes.rain.prize} R$ \nHost: ${bfRes.rain.host} \nTime Remaining: ${bfRes.rain.duration / 60000} minutes`);
if (config.os_notifs) {
notify({
title: "Bloxflip Rain Notifier",
message: `Robux: ${bfRes.rain.prize} R$ \nHost: ${bfRes.rain.host} \nTime Remaining: ${bfRes.rain.duration / 60000} minutes`,
subtitle: "bloxflip-rain",
sound: true
});
}
if (config.webhook.enabled) {
const embed = {
"embeds": [
{
"title": "Bloxflip Rain Notifier",
"url": "https://bloxflip.com",
"color": 3092790,
"fields": [
{
"name": "Prize",
"value": `${bfRes.rain.prize} R$`,
"inline": true
},
{
"name": "Host",
"value": bfRes.rain.host,
"inline": true
},
{
"name": "Time Remaining",
"value": `${bfRes.rain.duration / 60000} minutes`,
"inline": true
}
],
"footer": {
"text": "bloxflip-rain"
}
}
]
};
await fetch(webhookLink, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(embed)
});
}
} else {
Logger.warn("RAIN", "\tPrize is not greater or equal than the minimum value set in the config, ignoring...");
}
notified = true;
}
} else {
notified = false;
}
} catch (err) {
Logger.warn("FETCH", `Unable to fetch chat history, ignoring error...\n${err}`);
}
await sleep(config.delay);
}
})();
function sleep(ms: number): Promise<unknown> {
return new Promise((resolve): void => {
setTimeout(resolve, ms);
});
}