This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
48 deletions.
There are no files selected for viewing
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
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); | ||
})(); |