-
Notifications
You must be signed in to change notification settings - Fork 0
/
nukeMessages.js
99 lines (89 loc) · 2.69 KB
/
nukeMessages.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
//Your Token
let authToken = 'YourToken'; //input your token here
//ID of user that you want remove messages in DM
var idUser = ['ID']; //input id user here
//Optional option => last message send
if (typeof (beforeId) === 'undefined') {
var beforeId = null;
}
//Start Script
clearMessages = function () {
//Connection with Discord
const channel = window.location.href.split('/').pop();
const baseURL = `https://discordapp.com/api/channels/${channel}/messages`;
const headers = { Authorization: authToken };
let clock = 0;
let interval = 1300;
let messagesStore = [];
//Delay
function delay(duration) {
return new Promise((resolve) => {
setTimeout(resolve, duration);
});
}
//Load Messages
function loadMessages() {
let url = `${baseURL}?limit=100`;
if (beforeId) {
url += `&before=${beforeId}`;
}
return fetch(url, { headers });
}
//Delete Message
function tryDeleteMessage(message) {
if (idUser.indexOf(message.author.id) === -1 && message.content != null) {
console.log(`Message by ${message.author.username} (${message.content.substring(0, 30)}...) deleted.`);
return fetch(`${baseURL}/${message.id}`, { headers, method: 'DELETE' });
}
}
//Filter messages by ID
function filterMessages(message) {
return idUser.indexOf(message.author.id) === -1;
}
function onlyNotDeleted(message) {
return message.deleted === false;
}
loadMessages()
.then(resp => resp.json())
.then(messages => {
if (messages === null || messages.length === 0) {
console.log(`all messages have been deleted!`);
console.log(`Restart de page!`);
return;
}
beforeId = messages[messages.length - 1].id;
messages.forEach(message => { message.deleted = false; });
messagesStore = messagesStore.concat(messages.filter(filterMessages));
return Promise.all(messagesStore.filter(onlyNotDeleted).map(async (message) => {
await delay(clock += interval);
const resp = await tryDeleteMessage(message);
if (resp && resp.status === 204) {
message.deleted = true;
return resp.text();
}
const result = undefined;
if (result) {
result = JSON.parse(result);
if (result.code === 50003) {
console.log(`Sorry, my system can't remove message by: ${message.author.username}, skipping`);
idUser.push(message.author.id);
messagesStore = messagesStore.filter(filterMessages);
}
}
}));
})
.then(function () {
if (messagesStore.length !== 0 && messagesStore.length < 100) {
clearMessages();
} else {
ClearLoop();
}
});
};
//loop script
function ClearLoop() {
clearMessages();
}
//ClearLoop
ClearLoop();
console.clear()