Skip to content

Instantly share code, notes, and snippets.

@codyphobe
Created August 1, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyphobe/07be96e9a8142669d99cfb035d8d16ed to your computer and use it in GitHub Desktop.
Save codyphobe/07be96e9a8142669d99cfb035d8d16ed to your computer and use it in GitHub Desktop.
Deleting every message from a channel using DiscordJS
const client = new Discord.Client();
client.on('message', msg => {
let deleteStuff = () => {
let count = 0;
msg.channel.fetchMessages({limit: 100})
.then(messages => {
let messagesArr = messages.array();
let messageCount = messagesArr.length;
for(let i = 0; i < messageCount; i++) {
messagesArr[i].delete()
.then(function() {
count = count + 1;
if(count >= 100) {
deleteStuff();
}
})
.catch(function() {
count = count + 1;
if(count >= 100) {
deleteStuff();
}
})
}
})
.catch(function(err) {
console.log('error thrown');
console.log(err);
});
};
if(messageContent === '!:cleanse') {
deleteStuff();
}
};
client.login(process.env.TOKEN);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment