From 1ec0a811d9eddeabd4609847c06c274431e2464d Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 20 Sep 2018 22:27:59 +0300 Subject: [PATCH] Support role and user IDs in inboxServerPermission. Allow specifying multiple values for inboxServerPermission. --- src/config.js | 9 +++++++++ src/utils.js | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index 89cc73d..45ec2a7 100644 --- a/src/config.js +++ b/src/config.js @@ -135,4 +135,13 @@ if (! Array.isArray(finalConfig['mainGuildId'])) { finalConfig['mainGuildId'] = [finalConfig['mainGuildId']]; } +// Make sure inboxServerPermission is always an array +if (! Array.isArray(finalConfig['inboxServerPermission'])) { + if (finalConfig['inboxServerPermission'] == null) { + finalConfig['inboxServerPermission'] = []; + } else { + finalConfig['inboxServerPermission'] = [finalConfig['inboxServerPermission']]; + } +} + module.exports = finalConfig; diff --git a/src/utils.js b/src/utils.js index 85518f8..fdf4870 100644 --- a/src/utils.js +++ b/src/utils.js @@ -78,8 +78,20 @@ function postError(str) { * @returns {boolean} */ function isStaff(member) { - if (! config.inboxServerPermission) return true; - return member.permission.has(config.inboxServerPermission); + if (config.inboxServerPermission.length === 0) return true; + + return config.inboxServerPermission.some(perm => { + if (isSnowflake(perm)) { + // If perm is a snowflake, check it against the member's user id and roles + if (member.id === perm) return true; + if (member.roles.includes(perm)) return true; + } else { + // Otherwise assume perm is the name of a permission + return member.permission.has(perm); + } + + return false; + }); } /** @@ -274,6 +286,11 @@ function setDataModelProps(target, props) { } } +const snowflakeRegex = /^[0-9]{17,}$/; +function isSnowflake(str) { + return snowflakeRegex.test(str); +} + module.exports = { BotError, @@ -302,4 +319,6 @@ module.exports = { trimAll, setDataModelProps, + + isSnowflake, };