From cd3da4c4ecf650a69190e94073216ccd817a06dc Mon Sep 17 00:00:00 2001 From: Dragory Date: Wed, 6 Mar 2019 20:50:24 +0200 Subject: [PATCH] Add syncPermissionsOnMove config option --- src/config.js | 1 + src/modules/move.js | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/config.js b/src/config.js index 99f56e9..d111c4b 100644 --- a/src/config.js +++ b/src/config.js @@ -66,6 +66,7 @@ const defaultConfig = { "ignoreAccidentalThreads": false, "threadTimestamps": false, "allowMove": false, + "syncPermissionsOnMove": false, "typingProxy": false, "typingProxyReverse": false, "mentionUserInThreadHeader": false, diff --git a/src/modules/move.js b/src/modules/move.js index 5be351f..8d5a9bc 100644 --- a/src/modules/move.js +++ b/src/modules/move.js @@ -2,6 +2,7 @@ const config = require('../config'); const Eris = require('eris'); const threadUtils = require('../threadUtils'); const transliterate = require("transliteration"); +const erisEndpoints = require('eris/lib/rest/Endpoints'); module.exports = bot => { const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args); @@ -53,9 +54,35 @@ module.exports = bot => { const targetCategory = containsRankings[0][0]; - await bot.editChannel(thread.channel_id, { - parentID: targetCategory.id - }); + try { + await bot.editChannel(thread.channel_id, { + parentID: targetCategory.id + }); + } catch (e) { + thread.postSystemMessage(`Failed to move thread: ${e.message}`); + return; + } + + // If enabled, sync thread channel permissions with the category it's moved to + if (config.syncPermissionsOnMove) { + const newPerms = Array.from(targetCategory.permissionOverwrites.map(ow => { + return { + id: ow.id, + type: ow.type, + allow: ow.allow, + deny: ow.deny + }; + })); + + try { + await bot.requestHandler.request("PATCH", erisEndpoints.CHANNEL(thread.channel_id), true, { + permission_overwrites: newPerms + }); + } catch (e) { + thread.postSystemMessage(`Thread moved to ${targetCategory.name.toUpperCase()}, but failed to sync permissions: ${e.message}`); + return; + } + } thread.postSystemMessage(`Thread moved to ${targetCategory.name.toUpperCase()}`); });