Last active
August 17, 2018 21:45
-
-
Save highwaycoder/eefa52689dabb668c380331d3a9dd0d7 to your computer and use it in GitHub Desktop.
Batch delete for your own messages on a discord server. Use with caution. Work in progress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var before = 'LAST_MESSAGE_ID'; | |
var authorId = 'AUTHOR_ID'; | |
const authToken = 'AUTH_TOKEN'; | |
let clearMessages = () => { | |
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 = 500; | |
let delay = (duration) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(()=>resolve(), duration); | |
}); | |
} | |
let lastBefore; | |
fetch(`${baseUrl}?before=${before}&limit=100`, {headers}) | |
.then(resp => resp.json()) | |
.then(messages => { | |
let filtered = messages.filter((message) => { | |
return message.author.id === authorId; | |
}); | |
lastBefore = before; | |
before = messages.sort((a,b)=> (new Date(b.timestamp)).getTime() - (new Date(a.timestamp)).getTime()).unshift().id; | |
return Promise.all(filtered.map((message) => { | |
return delay(clock += interval).then(() => fetch(`${baseUrl}/${message.id}`, { | |
method: 'DELETE', | |
headers, | |
body: JSON.stringify(messages) | |
})); | |
})); | |
}).then(() => { | |
if(lastBefore !== before) { | |
setTimeout(clearMessages, 500); | |
} | |
}); | |
} | |
clearMessages(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment