From ec43c9cdaebf9816a206d19b10a6df7df9b90968 Mon Sep 17 00:00:00 2001 From: willowsnow Date: Fri, 4 Oct 2019 14:32:59 -0500 Subject: [PATCH] Upload to origin --- .gitignore | 4 ++++ channels.json.example | 6 +++++ config.json.example | 4 ++++ package-lock.json | 53 ++++++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++++++++++ src/index.js | 56 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 channels.json.example create mode 100644 config.json.example create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.js diff --git a/.gitignore b/.gitignore index ad46b30..49907c9 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ typings/ # next.js build output .next + +# bot configs +config.json +channels.json \ No newline at end of file diff --git a/channels.json.example b/channels.json.example new file mode 100644 index 0000000..731ac4b --- /dev/null +++ b/channels.json.example @@ -0,0 +1,6 @@ +{ + "voiceChannelID": { + "textChannel": "textChannelID", + "setToPurge": false + } +} \ No newline at end of file diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..82f1ead --- /dev/null +++ b/config.json.example @@ -0,0 +1,4 @@ +{ + "prefix": "!", + "token": "" +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..88aed73 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,53 @@ +{ + "name": "mc-vc-bot", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "async-limiter": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" + }, + "discord.js": { + "version": "11.5.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.5.1.tgz", + "integrity": "sha512-tGhV5xaZXE3Z+4uXJb3hYM6gQ1NmnSxp9PClcsSAYFVRzH6AJH74040mO3afPDMWEAlj8XsoPXXTJHTxesqcGw==", + "requires": { + "long": "^4.0.0", + "prism-media": "^0.0.3", + "snekfetch": "^3.6.4", + "tweetnacl": "^1.0.0", + "ws": "^6.0.0" + } + }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "prism-media": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.3.tgz", + "integrity": "sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==" + }, + "snekfetch": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz", + "integrity": "sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==" + }, + "tweetnacl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz", + "integrity": "sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==" + }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..77893e2 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "mc-vc-bot", + "version": "1.0.0", + "description": "A Discord bot that handles the permissions for linked voice and text channels", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/wisnoi/mc-vc-bot.git" + }, + "author": "wisnoi", + "license": "MIT", + "bugs": { + "url": "https://github.com/wisnoi/mc-vc-bot/issues" + }, + "homepage": "https://github.com/wisnoi/mc-vc-bot#readme", + "dependencies": { + "discord.js": "^11.5.1" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..c7c209f --- /dev/null +++ b/src/index.js @@ -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); \ No newline at end of file