-
Notifications
You must be signed in to change notification settings - Fork 0
/
discordnuke.js
142 lines (112 loc) · 3.31 KB
/
discordnuke.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const authToken = 'TOKEN_HERE';
const author_id = "ID_HERE";
async function apiCall(url, method) {
const headers = { 'Authorization': authToken, 'Content-Type': 'application/json' };
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
}
const response = await fetch(url, {headers, method: method});
if(method == "GET" || method == "POST") {
const json = await response.json();
return json;
}
}
async function apiGET(url) {
return apiCall(url, 'GET');
}
async function apiDELETE(url) {
return apiCall(url, 'DELETE');
}
async function getServers() {
return await apiGET("https://discordapp.com/api/v6/users/@me/guilds");
}
async function getDmChannels() {
return await apiGET("https://discordapp.com/api/v6/users/@me/channels");
}
function timestamp() {
const now = new Date() ;
return Math.round(now.getTime() / 1000);
}
async function deleteItems(items) {
let clock = 0;
let interval = 500;
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), duration);
});
}
offset = 0;
ts = timestamp();
total = items.length;
await items.forEach(async function(item) {
if(item && item.hit == true) {
await delay(clock += interval);
await apiDELETE(`https://discordapp.com/api/v6/channels/${item.channel_id}/messages/${item.id}`);
}
if(timestamp() - ts > 10) {
ts = timestamp();
console.log("Deleted " + Math.round(offset * 100 / total) + "%");
}
offset++;
});
}
async function getDeleteItems(channel_id = null, server_id = null) {
items = []
offset = 0
total = 0
ts = timestamp();
do {
if(channel_id != null) {
json = await apiGET(`https://discordapp.com/api/v6/channels/${channel_id}/messages/search?author_id=${author_id}&offset=${offset}`);
} else {
json = await apiGET(`https://discordapp.com/api/v6/guilds/${server_id}/messages/search?author_id=${author_id}&offset=${offset}`);
}
if(total == 0) {
total = json.total_results;
console.log("There are " + json.total_results + " messages to delete.");
} else if(total > 0 && timestamp() - ts > 10) {
ts = timestamp();
console.log("Status: " + Math.round(offset * 100 / total) + "%");
}
await Array.from(json.messages).map(message => {
message.forEach(async function(item) {
if(item && item.hit == true) {
items.push(item);
}
});
});
offset += 25
}
while(offset < total);
return items;
}
async function deleteServerMessages(server_id) {
items = await getDeleteItems(null, server_id);
await deleteItems(items);
console.log("Finished deleting messages");
return false;
}
async function deleteChannelMessages(channel_id) {
items = await getDeleteItems(channel_id, null);
await deleteItems(items);
console.log("Finished deleting messages");
return false;
}
servers = await getServers();
for(i in servers) {
server = servers[i];
if(server) {
console.log("scrubbing server " + servers[i].name);
await deleteServerMessages(server["id"]);
}
}
dms = await getDmChannels();
for(i in dms) {
dm = dms[i];
if(dm) {
console.log("scrubbing dm " + dm["recipients"][0]["username"]);
await deleteChannelMessages(dm["id"]);
}
}