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'] + }); + } };