-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 1.89 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require("fs");
const fetch = require("node-fetch");
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
class Authorization {
constructor(token, filename, channelId, timeSleepMin, timeSleepMax) {
this.token = token;
this.filename = filename;
this.channelId = channelId;
this.timeSleepMin = timeSleepMin;
this.timeSleepMax = timeSleepMax;
}
}
function readConfigFile(filename) {
try {
const data = fs.readFileSync(filename, "utf8");
return JSON.parse(data);
} catch (err) {
console.error("error reading or parsing config file:", err);
return [];
}
}
function getRandomLine(filename) {
try {
const data = fs.readFileSync(filename, "utf8");
const lines = data.split("\n");
return lines[Math.floor(Math.random() * lines.length)];
} catch (err) {
console.error("error reading file:", err);
return "";
}
}
function sendMessage(auth) {
const postURL = `https://discord.com/api/v9/channels/${auth.channelId}/messages`;
const msg = getRandomLine(auth.filename);
console.log(msg);
fetch(postURL, {
method: "POST",
headers: {
Authorization: auth.token,
"Content-Type": "application/json",
},
body: JSON.stringify({ content: msg }),
})
.then((response) => response.json())
.then((data) => {
console.log("msg sent", data);
})
.catch((error) => {
console.error("error sending message", error);
});
}
function main() {
const configData = readConfigFile("config.json");
const auths = configData.map(
(data) =>
new Authorization(
data.token,
data.filename,
data.channelId,
data.timeSleepMin,
data.timeSleepMax
)
);
auths.forEach((auth) => {
setInterval(() => {
sendMessage(auth);
}, randomBetween(auth.timeSleepMin * 1000, auth.timeSleepMax * 1000));
});
}
main();