Skip to content

Instantly share code, notes, and snippets.

@juhokuu
Last active November 11, 2018 03:31
Show Gist options
  • Save juhokuu/a26136084c945fc8a84e50ab6478a216 to your computer and use it in GitHub Desktop.
Save juhokuu/a26136084c945fc8a84e50ab6478a216 to your computer and use it in GitHub Desktop.
Delete private Discord msgs you've sent
// vim: set ts=4 sw=4:
var ownid = '';
var atoken = '';
var stopthis = 'no', waitms = 500;
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(function() {
return resolve();
}, duration);
});
};
function nukeAll(msgid) {
console.info('nukeAll start');
if (stopthis == 'yes') return false;
const channel = window.location.href.split('/').pop();
const baseURL = `https://discordapp.com/api/channels/${channel}/messages`;
const headers = {"Authorization": atoken };
const limit = 100;
let clock = 0, ratel = waitms;
let url = '';
if (typeof(msgid) == 'undefined') url = baseURL + '?limit=' + limit;
else url = baseURL + '?before=' + msgid + '&limit=' + limit;
console.info('Fetching', url);
fetch(url, { headers }).then(response => {
if (response.status == 200) return response.json();
else throw 'No more messages';
}).then(messages => {
let lastmsg = messages[messages.length - 1].id;
console.info('Got', messages.length, 'messages from the server. Entering a loop to delete them.');
messages.forEach(item => {
if (item.author.id != ownid) { lastmsg = item.id; ratel = 20 }
else { ratel = waitms; }
delay(clock += ratel).then(() => {
if (item.author.id == ownid && stopthis != 'yes') {
fetch(`${baseURL}/${item.id}`, {headers, method: 'DELETE'}).then((response) => {
if (response.status == 429) { stopthis = 'yes'; }
console.log(response.status.toString(), 'Deleted', item.timestamp.substring(0, 10), item.id, item.content.replace(/(\r\n|\n|\r)/gm,"").substring(0, 80));
});
} else {
console.log('000', 'Skipped', item.timestamp.substring(0, 10), item.id, item.content.replace(/(\r\n|\n|\r)/gm,"").substring(0, 80));
}
if (item.id == lastmsg) {
console.info('Reached the last message of the current batch. Calling self.');
delay(5000).then(() => { nukeAll(lastmsg); });
}
});
});
}).then(() => { console.info('nukeAll end'); }).catch(err => console.error('ERROR', err));
};
// Minified
// var ownid="";var atoken="";var stopthis="no",waitms=500;function delay(duration){return new Promise((resolve,reject)=>{setTimeout(function(){return resolve()},duration)})};function nukeAll(msgid){console.info("nukeAll start");if(stopthis=="yes")return false;const channel=window.location.href.split("/").pop();const baseURL=`https://discordapp.com/api/channels/${channel}/messages`;const headers={Authorization:atoken};const limit=100;let clock=0,ratel=waitms;let url="";if(typeof msgid=="undefined")url=baseURL+"?limit="+limit;else url=baseURL+"?before="+msgid+"&limit="+limit;console.info("Fetching",url);fetch(url,{headers}).then(response=>{if(response.status==200)return response.json();else throw"No more messages"}).then(messages=>{let lastmsg=messages[messages.length-1].id;console.info("Got",messages.length,"messages from the server. Entering a loop to delete them.");messages.forEach(item=>{if(item.author.id!=ownid){lastmsg=item.id;ratel=20}else{ratel=waitms}delay(clock+=ratel).then(()=>{if(item.author.id==ownid&&stopthis!="yes"){fetch(`${baseURL}/${item.id}`,{headers,method:"DELETE"}).then(response=>{if(response.status==429){stopthis="yes"}console.log(response.status.toString(),"Deleted",item.timestamp.substring(0,10),item.id,item.content.replace(/(\r\n|\n|\r)/gm,"").substring(0,80))})}else{console.log("000","Skipped",item.timestamp.substring(0,10),item.id,item.content.replace(/(\r\n|\n|\r)/gm,"").substring(0,80))}if(item.id==lastmsg){console.info("Reached the last message of the current batch. Calling self.");delay(5e3).then(()=>{nukeAll(lastmsg)})}})})}).then(()=>{console.info("nukeAll end")}).catch(err=>console.error("ERROR",err))};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment