Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
Upload to origin
Browse files Browse the repository at this point in the history
  • Loading branch information
kyogoi committed Oct 4, 2019
1 parent d2deaa7 commit 22fd573
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const Discord = require('discord.js');
const bot = new Discord.Client();
const config = require(__dirname + '/../config.json');
const fs = require('fs');
let channels = require(__dirname + '/../channels.json');

function updateChannels() {
fs.writeFileSync(__dirname + '/../channels.json', JSON.stringify(channels));
}

function purge() {
let isPurged = 0;

Object.keys(channels).forEach(async (id) => {
if (channels[id].setToPurge) {
const textChan = bot.channels.get(channels[id].textChannel);
while (!isPurged) {
await textChan.fetchMessages({ limit: 100 }).then(async (messages) => {
if (messages.array().length > 0) {
await textChan.bulkDelete(messages);
} else {
isPurged = 1;
}
});
}
channels[id].setToPurge = false;
updateChannels();
}
});
}

bot.on('voiceStateUpdate', (oldMember, newMember) => {

// Dealing with a purge channel
if (channels[oldMember.voiceChannelID]) {
console.log(oldMember.voiceChannelID);
let channel = bot.channels.get(oldMember.voiceChannelID);
if (!channel.members.length) {
channels[oldMember.voiceChannelID].setToPurge = true;
updateChannels();
}
}

// Dealing with a purge channel
if (channels[newMember.voiceChannelID]) {
channels[newMember.voiceChannelID].setToPurge = false;
updateChannels();
}

});

bot.on('ready', () => {
setInterval(purge, 1800000);
});

bot.login(config.token);

0 comments on commit 22fd573

Please sign in to comment.