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

Commit

Permalink
Completely refactor everything
Browse files Browse the repository at this point in the history
  • Loading branch information
kyogoi committed Oct 5, 2019
1 parent 07a89ca commit 1121f7d
Showing 1 changed file with 45 additions and 48 deletions.
93 changes: 45 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,52 @@
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]) {
let channel = bot.channels.get(oldMember.voiceChannelID);
if (!channel.members.length) {
channels[oldMember.voiceChannelID].setToPurge = true;
updateChannels();
(async () => {
const Discord = require('discord.js');
const bot = new Discord.Client();
const config = require(__dirname + '/../config.json');
const sqlite = require('sqlite');
const db = await sqlite.open(__dirname + '/../db/channels.db');

async function purgeAll() {
const channels = await db.all('SELECT text_id FROM channels WHERE set_to_purge = 1');

for (const channel of channels) {
await purge(bot.channels.get(channel.text_id));
}
}

// Dealing with a purge channel
if (channels[newMember.voiceChannelID]) {
channels[newMember.voiceChannelID].setToPurge = false;
updateChannels();
async function purge(channel) {
while (true) {
const messages = await channel.fetchMessages({ limit: 100 });
if (messages.array().length > 0) {
await channel.bulkDelete(messages);
} else {
db.run('UPDATE channels SET set_to_purge = 0 WHERE text_id = ?', [channel.id]);
break;
}
}
}

});
bot.on('voiceStateUpdate', async (oldMember, newMember) => {
const channels = await db.all('SELECT voice_id, text_id FROM channels');

// Dealing with a purge channel
if (channels.some((v) => v.voice_id == oldMember.voiceChannelID)) {
const channel = bot.channels.get(oldMember.voiceChannelID);
if (!channel.members.length) {
db.run('UPDATE channels SET set_to_purge = 1 WHERE voice_id = ?', [oldMember.voiceChannelID]);
}
}

// Dealing with a purge channel
if (channels.some((v) => v.voice_id == newMember.voiceChannelID)) {
db.run('UPDATE channels SET set_to_purge = 0 WHERE voice_id = ?', [newMember.voiceChannelID]);
}

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

bot.login(config.token);
bot.once('ready', async () => {
db.run('UPDATE channels SET set_to_purge = 0');
setInterval(purgeAll, 5000);
});

bot.login(config.token);
})();

0 comments on commit 1121f7d

Please sign in to comment.