From ed37673b7e5bd9ff97c182da7381ba1d310a4abc Mon Sep 17 00:00:00 2001 From: Miikka Virtanen Date: Tue, 6 Dec 2016 00:45:33 +0200 Subject: [PATCH] Add blocking support. Add status message. --- .gitignore | 3 ++- index.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a0b827c..57172cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.vscode /node_modules /config.json -/.vscode +/blocked.json diff --git a/index.js b/index.js index 6ef4855..7edbbf9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const Eris = require('eris'); const moment = require('moment'); const Queue = require('./queue'); @@ -14,6 +15,20 @@ let modMailGuild; const modMailChannels = {}; const messageQueue = new Queue(); +const blockFile = `${__dirname}/blocked.json`; +let blocked = []; + +try { + const blockedJSON = fs.readFileSync(blockFile, {encoding: 'utf8'}); + blocked = JSON.parse(blockedJSON); +} catch(e) { + fs.writeFileSync(blockFile, '[]'); +} + +function saveBlocked() { + fs.writeFileSync(blockFile, JSON.stringify(blocked, null, 4)); +} + bot.on('ready', () => { modMailGuild = bot.guilds.find(g => g.id === config.mailGuildId); @@ -21,6 +36,8 @@ bot.on('ready', () => { console.error('You need to set and invite me to the mod mail guild first!'); process.exit(0); } + + bot.editStatus(null, {name: 'DM me for help'}); }); function getModmailChannelInfo(channel) { @@ -78,6 +95,8 @@ bot.on('messageCreate', (msg) => { if (! (msg.channel instanceof Eris.PrivateChannel)) return; if (msg.author.id === bot.user.id) return; + if (blocked.indexOf(msg.author.id) !== -1) return; + // This needs to be queued as otherwise, if a user sent a bunch of messages initially and the createChannel endpoint is delayed, we might get duplicate channels messageQueue.add(() => { return getModmailChannel(msg.author).then(channel => { @@ -132,4 +151,44 @@ bot.registerCommand('close', (msg, args) => { }); }); +bot.registerCommand('block', (msg, args) => { + if (msg.channel.guild.id !== modMailGuild.id) return; + if (! msg.member.permission.has('manageRoles')) return; + if (args.length !== 1) return; + + let userId; + if (args[0].match(/^[0-9]+$/)) { + userId = args[0]; + } else { + let mentionMatch = args[0].match(/^<@([0-9]+?)>$/); + if (mentionMatch) userId = mentionMatch[1]; + } + + if (! userId) return; + + blocked.push(args[0]); + saveBlocked(); + msg.channel.createMessage(`Blocked <@${userId}> (id ${userId}) from modmail`); +}); + +bot.registerCommand('unblock', (msg, args) => { + if (msg.channel.guild.id !== modMailGuild.id) return; + if (! msg.member.permission.has('manageRoles')) return; + if (args.length !== 1) return; + + let userId; + if (args[0].match(/^[0-9]+$/)) { + userId = args[0]; + } else { + let mentionMatch = args[0].match(/^<@([0-9]+?)>$/); + if (mentionMatch) userId = mentionMatch[1]; + } + + if (! userId) return; + + blocked.splice(blocked.indexOf(args[0]), 1); + saveBlocked(); + msg.channel.createMessage(`Unblocked <@${userId}> (id ${userId}) from modmail`); +}); + bot.connect();