From d0abeb56b4c566bce3060db4249d1136c717345a Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" Date: Sat, 24 Jun 2023 01:20:56 +0500 Subject: [PATCH] fix permissions and support for users categories --- ModuleExportsFunctions/formTypes.js | 12 ++-- Routes/dashboard.js | 57 ++++++++----------- .../utils/functions/settingsPage.js | 14 ++--- theme/dbd-soft-ui/views/debug.ejs | 2 +- theme/dbd-soft-ui/views/guilds.ejs | 7 +-- 5 files changed, 38 insertions(+), 54 deletions(-) diff --git a/ModuleExportsFunctions/formTypes.js b/ModuleExportsFunctions/formTypes.js index 519a56d..f46a365 100644 --- a/ModuleExportsFunctions/formTypes.js +++ b/ModuleExportsFunctions/formTypes.js @@ -1,4 +1,4 @@ -const { Permissions } = require("discord.js"); +const { PermissionsBitField, ChannelType } = require("discord.js"); module.exports = { select: (list, disabled, themeOptions = {}) => { @@ -80,7 +80,7 @@ module.exports = { themeOptions, }; }, - channelsSelect: (disabled, channelTypes = ["GUILD_TEXT"], hideNSFW, onlyNSFW, hideNoAccess, themeOptions = {}) => { + channelsSelect: (disabled, channelTypes = [ChannelType.GuildText], hideNSFW, onlyNSFW, hideNoAccess, themeOptions = {}) => { return { type: "channelsSelect", function: (client, guildid, userid) => { @@ -98,8 +98,8 @@ module.exports = { if (hideNSFW && channel.nsfw) return; if (onlyNSFW && !channel.nsfw) return; if (hideNoAccess) { - if (!user.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES) || !user.permissionsIn(channel).has(Permissions.FLAGS.VIEW_CHANNEL)) return; - if (!bot.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES) || !bot.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES)) return; + if (!user.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages) || !user.permissionsIn(channel).has(PermissionsBitField.Flags.ViewChannel)) return; + if (!bot.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages) || !bot.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages)) return; } listCount[channel.name] ? (listCount[channel.name] = listCount[channel.name] + 1) : (listCount[channel.name] = 1); @@ -145,8 +145,8 @@ module.exports = { if (hideNSFW && channel.nsfw) return; if (onlyNSFW && !channel.nsfw) return; if (hideNoAccess) { - if (!user.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES) || !user.permissionsIn(channel).has(Permissions.FLAGS.VIEW_CHANNEL)) return; - if (!bot.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES) || !bot.permissionsIn(channel).has(Permissions.FLAGS.SEND_MESSAGES)) return; + if (!user.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages) || !user.permissionsIn(channel).has(PermissionsBitField.Flags.ViewChannel)) return; + if (!bot.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages) || !bot.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages)) return; } listCount[channel.name] ? (listCount[channel.name] = listCount[channel.name] + 1) : (listCount[channel.name] = 1); diff --git a/Routes/dashboard.js b/Routes/dashboard.js index 525725e..672f75c 100644 --- a/Routes/dashboard.js +++ b/Routes/dashboard.js @@ -62,13 +62,12 @@ module.exports = (app, config, themeConfig) => { let customThemeOptions; if (themeConfig?.customThemeOptions?.getGuild) { - customThemeOptions = - await themeConfig.customThemeOptions.getGuild({ - req: req, - res: res, - config: config, - guildId: req.params.id, - }); + customThemeOptions = await themeConfig.customThemeOptions.getGuild({ + req: req, + res: res, + config: config, + guildId: req.params.id, + }); } const bot = config.bot; @@ -85,11 +84,7 @@ module.exports = (app, config, themeConfig) => { } catch (e) { /* ... */ } } - // for (const PermissionRequired of req.requiredPermissions) { - // const permissionsReq = PermissionRequired[0]; - - // if (!bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id).permissions.has(permissionsReq)) return res.redirect("/manage?error=noPermsToManageGuild"); - // } + if (!bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id).permissions.has(config.requiredPermissions)) return res.redirect("/manage?error=noPermsToManageGuild"); if (bot.guilds.cache.get(req.params.id).channels.cache.size < 1) { try { @@ -109,6 +104,7 @@ module.exports = (app, config, themeConfig) => { if (!config.useCategorySet) for (const s of config.settings) { if (!canUseList[s.categoryId]) canUseList[s.categoryId] = {}; + for (const c of s.categoryOptionsList) { if (c.allowedCheck) { const canUse = await c.allowedCheck({ @@ -126,21 +122,20 @@ module.exports = (app, config, themeConfig) => { }; } - if (c.optionType == "spacer") { /* ... */ } else { + if (c.optionType.type == "spacer") { /* ... */ } else { if (!actual[s.categoryId]) actual[s.categoryId] = {}; if (!actual[s.categoryId][c.optionId]) { - actual[s.categoryId][c.optionId] = - await c.getActualSet({ - guild: { - id: req.params.id, - object: bot.guilds.cache.get(req.params.id), - }, - user: { - id: req.session.user.id, - object: bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id), - }, - }); + actual[s.categoryId][c.optionId] = await c.getActualSet({ + guild: { + id: req.params.id, + object: bot.guilds.cache.get(req.params.id), + }, + user: { + id: req.session.user.id, + object: bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id), + }, + }); } } } @@ -245,22 +240,18 @@ module.exports = (app, config, themeConfig) => { // } const bot = config.bot; + const member = bot.guilds.cache.get(req.params.guildId).members.cache.get(req.session.user.id); if (!bot.guilds.cache.get(req.params.guildId)) return res.redirect("/manage?error=noPermsToManageGuild"); await bot.guilds.cache.get(req.params.guildId).members.fetch(req.session.user.id); - // for (const PermissionRequired of req.requiredPermissions) { - // const permissionsReq = PermissionRequired[0]; - - // if (!bot.guilds.cache.get(req.params.guildId).members.cache.get(req.session.user.id).permissions.has(permissionsReq)) - // return res.redirect("/manage?error=noPermsToManageGuild"); - // } + if (!member.permissions.has(config.requiredPermissions)) return res.redirect("/manage?error=noPermsToManageGuild"); const cid = req.params.categoryId; const settings = config.settings; - const category = settings.find((c) => c.categoryId == cid); + const category = settings.find(c => c.categoryId == cid); if (!category) return res.send({ @@ -296,9 +287,7 @@ module.exports = (app, config, themeConfig) => { error: canUse.errorMessage, }; - errors.push( - option.optionName + "%is%" + setNewRes.error + "%is%" + option.optionId, - ); + errors.push(option.optionName + "%is%" + setNewRes.error + "%is%" + option.optionId); } else if (option.optionType != "spacer") { if (config.useCategorySet) { if (option.optionType.type == "rolesMultiSelect" || option.optionType.type == "channelsMultiSelect" || option.optionType.type == "multiSelect") { diff --git a/theme/dbd-soft-ui/utils/functions/settingsPage.js b/theme/dbd-soft-ui/utils/functions/settingsPage.js index 89f40ff..ca83235 100644 --- a/theme/dbd-soft-ui/utils/functions/settingsPage.js +++ b/theme/dbd-soft-ui/utils/functions/settingsPage.js @@ -1,5 +1,3 @@ -const { Permissions } = require("discord.js"); - module.exports = function (config, themeConfig) { // eslint-disable-next-line no-unused-vars config.guildSettings = async function (req, res, home, category) { @@ -19,12 +17,7 @@ module.exports = function (config, themeConfig) { } catch (e) { /* ... */ } } - // for (const PermissionRequired of req.requiredPermissions) { - // const permissionsReq = Permissions.FLAGS[PermissionRequired[0]]; - - // if (!bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id).permissions.has(permissionsReq)) - // return res.redirect("/manage?error=noPermsToManageGuild"); - // } + if (!bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id).permissions.has(config.requiredPermissions)) return res.redirect("/manage?error=noPermsToManageGuild"); if (bot.guilds.cache.get(req.params.id).channels.cache.size < 1) { try { @@ -44,7 +37,9 @@ module.exports = function (config, themeConfig) { const canUseList = {}; - if (config.settings?.length) + if (config.settings?.length) { + config.settings = config.settings.filter(c => c.categoryPermissions ? bot.guilds.cache.get(req.params.id).members.cache.get(req.session.user.id).permissions.has(c.categoryPermissions) : true); + for (const category of config.settings) { if (!canUseList[category.categoryId]) canUseList[category.categoryId] = {}; if (!actual[category.categoryId]) actual[category.categoryId] = {}; @@ -216,6 +211,7 @@ module.exports = function (config, themeConfig) { } } } + } let errors; let success; diff --git a/theme/dbd-soft-ui/views/debug.ejs b/theme/dbd-soft-ui/views/debug.ejs index 00df43e..78fc8dc 100644 --- a/theme/dbd-soft-ui/views/debug.ejs +++ b/theme/dbd-soft-ui/views/debug.ejs @@ -33,7 +33,7 @@ {name: "Theme", value: "Soft UI Theme"}, {name: "Modified", value: `${modded}`}, {name: "Uptime", value: `${filterUptime}`}, - // {name: "Permissions", value: req.requiredPermissions}, + {name: "Permissions", value: config.requiredPermissions}, {name: "Redirect URI", value: `${config.redirectUri?.includes("/discord/callback")}`} ] diff --git a/theme/dbd-soft-ui/views/guilds.ejs b/theme/dbd-soft-ui/views/guilds.ejs index 285bfd5..ed30d93 100644 --- a/theme/dbd-soft-ui/views/guilds.ejs +++ b/theme/dbd-soft-ui/views/guilds.ejs @@ -27,9 +27,8 @@
- <% - for(var i in req.session.guilds){ %> - <% if((req.session.guilds[i].permissions & 0x00000020) == 0x00000020){ %> + <% for(var i in req.session.guilds) { %> + <% if((req.session.guilds[i].permissions & 0x0400) == 0x0400){ %> <% if(bot.guilds.cache.get(req.session.guilds[i].id)){ const guild = bot.guilds.cache.get(req.session.guilds[i].id); let icon; @@ -47,7 +46,7 @@
<% for (var i in req.session.guilds){ %> - <% if((req.session.guilds[i].permissions & 0x00000020) == 0x00000020){ %> + <% if((req.session.guilds[i].permissions & 0x20) == 0x20){ %> <% if(!bot.guilds.cache.get(req.session.guilds[i].id)){ let icon = `https://cdn.discordapp.com/icons/${req.session.guilds[i].id}/${req.session.guilds[i].icon}.png`;