From 3002473905092fbfbc03908cb799b7581810d0f4 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 19 Jul 2020 14:20:45 +0300 Subject: [PATCH] Add !edit and !delete with options (disabled by default) --- src/cfg.js | 2 ++ src/commands.js | 15 ++++++++++++++- src/modules/reply.js | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/cfg.js b/src/cfg.js index 5d649f8..dc3b47b 100644 --- a/src/cfg.js +++ b/src/cfg.js @@ -89,6 +89,8 @@ const defaultConfig = { "typingProxyReverse": false, "mentionUserInThreadHeader": false, "rolesInThreadHeader": false, + "allowStaffEdit": false, + "allowStaffDelete": false, "enableGreeting": false, "greetingMessage": null, diff --git a/src/commands.js b/src/commands.js index 1973638..3135ee9 100644 --- a/src/commands.js +++ b/src/commands.js @@ -1,7 +1,9 @@ -const { CommandManager, defaultParameterTypes, TypeConversionError } = require('knub-command-manager'); +const { CommandManager, defaultParameterTypes, TypeConversionError, IParameter, ICommandConfig } = require('knub-command-manager'); +const Eris = require('eris'); const config = require('./cfg'); const utils = require('./utils'); const threads = require('./data/threads'); +const Thread = require('./data/Thread'); module.exports = { createCommandManager(bot) { @@ -84,8 +86,19 @@ module.exports = { }; }; + /** + * @callback InboxThreadCommandHandler + * @param {Eris.Message} msg + * @param {object} args + * @param {Thread} thread + */ + /** * Add a command that can only be invoked in a thread on the inbox server + * @param {string|RegExp} trigger + * @param {string|IParameter[]} parameters + * @param {InboxThreadCommandHandler} handler + * @param {ICommandConfig} commandConfig */ const addInboxThreadCommand = (trigger, parameters, handler, commandConfig = {}) => { const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : []; diff --git a/src/modules/reply.js b/src/modules/reply.js index bc2afe3..cd0bef2 100644 --- a/src/modules/reply.js +++ b/src/modules/reply.js @@ -1,5 +1,7 @@ const attachments = require("../data/attachments"); const utils = require('../utils'); +const config = require('../cfg'); +const Thread = require('../data/Thread'); module.exports = ({ bot, knex, config, commands }) => { // Mods can reply to modmail threads using !r or !reply @@ -16,7 +18,6 @@ module.exports = ({ bot, knex, config, commands }) => { aliases: ['r'] }); - // Anonymous replies only show the role, not the username commands.addInboxThreadCommand('anonreply', '[text$]', async (msg, args, thread) => { if (! args.text && msg.attachments.length === 0) { @@ -29,4 +30,42 @@ module.exports = ({ bot, knex, config, commands }) => { }, { aliases: ['ar'] }); + + if (config.allowStaffEdit) { + commands.addInboxThreadCommand('edit', ' ', async (msg, args, thread) => { + const threadMessage = await thread.findThreadMessageByMessageNumber(args.messageNumber); + if (! threadMessage) { + utils.postError(msg.channel, 'Unknown message number'); + return; + } + + if (threadMessage.user_id !== msg.author.id) { + utils.postError(msg.channel, 'You can only edit your own replies'); + return; + } + + await thread.editStaffReply(msg.member, threadMessage, args.text) + }, { + aliases: ['e'] + }); + } + + if (config.allowStaffDelete) { + commands.addInboxThreadCommand('delete', '', async (msg, args, thread) => { + const threadMessage = await thread.findThreadMessageByMessageNumber(args.messageNumber); + if (! threadMessage) { + utils.postError(msg.channel, 'Unknown message number'); + return; + } + + if (threadMessage.user_id !== msg.author.id) { + utils.postError(msg.channel, 'You can only delete your own replies'); + return; + } + + await thread.deleteStaffReply(msg.member, threadMessage); + }, { + aliases: ['d'] + }); + } };