Last active
November 10, 2021 13:16
-
-
Save evandhoffman/eebd591db63da51037d338951290f6be to your computer and use it in GitHub Desktop.
Delete your discord messages in a channel older than a specific date
This file contains 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
// Lifted partially from https://gist.github.com/orion-v/95cb48fa73808cdc5c589fe415cc65f1 | |
// and https://github.com/vegeta897/snow-stamp/blob/main/src/convert.js#L2 | |
const DISCORD_EPOCH = 1420070400000 | |
function dateToPostId(date) { | |
return (date - DISCORD_EPOCH) * 4194304 | |
//return new Date(snowflake / 4194304 + epoch) | |
} | |
async function clearMessages(date) { | |
const server = "fill this in "; // server id number | |
const author = "fill this in "; // user id number | |
const authToken = "fill this in "; | |
const headers = { 'Authorization': authToken, 'Content-Type': 'application/json' }; | |
const baseURL = `https://discord.com/api/v9/channels`; | |
let max_id = dateToPostId(date) | |
let searchURL = `https://discord.com/api/v9/guilds/${server}/messages/search?author_id=${author}&max_id=${max_id}`; | |
console.log("date " + date + " becomes max_id " + max_id + ".") | |
let clock = 0; | |
let interval = 10000; | |
function delay(duration) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(), duration); | |
}); | |
} | |
const response = await fetch(searchURL, {headers}); | |
const json = await response.json(); | |
console.log("There are " + json.total_results + " messages left to delete."); | |
await Array.from(json.messages).map(message => { | |
message.forEach(async function(item) { | |
if(item.hit == true) { | |
await delay(clock += interval); | |
await fetch(`${baseURL}/${item.channel_id}/messages/${item.id}`, { headers, method: 'DELETE' }); | |
} | |
}); | |
}); | |
if (json.total_results > 0) { | |
delay(clock += interval).then(() => { clearMessages(date); }); | |
} else { | |
console.log("Finished deleting messages") | |
}; | |
} | |
clearMessages(Date.parse('01 Nov 2021 00:00:00 GMT')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment