-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (103 loc) · 3.44 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
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
const {request} = require("undici");
const ms = require("ms");
const {webcrypto} = require("crypto");
const path = require("node:path");
const fs = require("node:fs/promises");
const {existsSync} = require("node:fs");
let checkpoint = null;
const config = require("./config");
const baseURL = "https://discord.com/api/v" + config.apiVersion;
const limit = 100;
const delayStop = 5; // how much will be deleted in a single session before cooldown
const checkpointPath = path.join(__dirname, "messageID");
const headers = {
Authorization: config.token,
"User-Agent": "Mozilla/5.0 (Linux; Android 8.0.0; ASUS_X00QD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36"
};
function randomTimer(min, max) {
const array = new Uint32Array(1);
webcrypto.getRandomValues(array);
return min + (array[0] % (max - min + 1));
};
// first, collect all the messages, slowly + filtering
async function collectMessages(restartMsgID) {
try {
if (!existsSync(checkpointPath)) {
await fs.writeFile(checkpointPath, "", "utf-8");
};
let startPoint = restartMsgID || (await fs.readFile(checkpointPath)).toString("utf-8") || config.startMessageID;
const fetchMessages = await request(baseURL + `/channels/${config.channelID}/messages?around=${startPoint}&limit=${limit}`, {
method: "GET", headers
});
if (!fetchMessages || fetchMessages.statusCode >= 400) {
console.log("Unable to fetch messages.", `[${fetchMessages.statusCode}] ${await fetchMessages.body.text()}`)
return stop();
};
console.log(`Check ${startPoint}`)
let messageIDLoop = [];
let array = await fetchMessages.body.json();
if (array instanceof Array) {
messageIDLoop = array
.filter(x => [0, 19].includes(x.type) && x.author.id == config.authorID)
.sort((a, b) => a.id - b.id)
.map(x => x.id);
};
if (!array?.length) {
console.log("No content presented in array.")
return stop();
};
if (messageIDLoop.length) {
for (let i = 0; i < messageIDLoop.length; i++) {
const messageID = messageIDLoop[i];
await deleteMessage(messageID);
console.log(`Deleted ${messageID} [${i + 1} / ${messageIDLoop.length}]`)
if (i !== 0 && i % delayStop == 0) {
const delayTime = randomTimer(ms("15s"), ms("30s"));
console.log(`${messageID} delay time: ${delayTime} ms`);
await delay(delayTime);
};
if (messageIDLoop.length === i + 1) {
console.log("End of line, trying to get more messages.");
await Promise.all([
collectMessages(messageID),
fs.writeFile(checkpointPath, messageID, "utf-8")
]);
messageIDLoop = [];
break;
};
continue;
};
} else {
console.log("No messages presented.")
return stop();
};
return;
} catch (error) {
console.error(error);
return stop();
};
};
async function deleteMessage(messageID) {
try {
await request(baseURL + `/channels/${config.channelID}/messages/${messageID}`, {
method: "DELETE", headers
});
} catch (error) {
console.error(error);
};
};
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
async function main() {
try {
await collectMessages(checkpoint || undefined);
} catch (error) {
console.error(error);
return stop();
};
};
function stop() {
return process.exit(128);
};
main();