diff --git a/src/lib/users.js b/src/lib/users.js index 23a959d..ca196f8 100644 --- a/src/lib/users.js +++ b/src/lib/users.js @@ -11,6 +11,29 @@ module.exports.getCommonGuilds = async (client, userId) => await client.guilds.c return !!member; }); +/** + * @param {import("discord.js").Guild} guild + * @returns {Promise} + */ +const updateStaffRoles = async guild => { + const { categories } = await guild.client.prisma.guild.findUnique({ + select: { categories: { select: { staffRoles: true } } }, + where: { id: guild.id }, + }); + const staffRoles = [ + ...new Set( + categories.reduce((acc, c) => { + acc.push(...c.staffRoles); + return acc; + }, []), + ), + ]; + await guild.client.keyv.set(`cache/guild-staff:${guild.id}`, staffRoles); + return staffRoles; +}; + +module.exports.updateStaffRoles = updateStaffRoles; + /** * * @param {import("discord.js").Guild} guild @@ -20,12 +43,9 @@ module.exports.getCommonGuilds = async (client, userId) => await client.guilds.c module.exports.isStaff = async (guild, userId) => { /** @type {import("client")} */ const client = guild.client; - if (guild.client.supers.includes(userId)) return true; - const guildMember = await guild.members.fetch(userId); - if (guildMember?.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true; - const { categories } = await client.prisma.guild.findUnique({ - select: { categories: true }, - where: { id: guild.id }, - }); - return categories.some(cat => cat.roles.some(r => guildMember.roles.cache.has(r))); + if (client.supers.includes(userId)) return true; + const guildMember = guild.members.cache.get(userId) || await guild.members.fetch(userId); + if (guildMember.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true; + const staffRoles = await client.keyv.get(`cache/guild-staff:${guild.id}`) || await updateStaffRoles(guild); + return staffRoles.some(r => guildMember.roles.cache.has(r)); }; \ No newline at end of file diff --git a/src/routes/api/admin/guilds/[guild]/categories/[category]/index.js b/src/routes/api/admin/guilds/[guild]/categories/[category]/index.js index 29fe1a7..3493c1e 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/[category]/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/[category]/index.js @@ -1,16 +1,19 @@ const { logAdminEvent } = require('../../../../../../../lib/logging'); +const { updateStaffRoles } = require('../../../../../../../lib/users'); const { randomUUID } = require('crypto'); module.exports.delete = fastify => ({ handler: async (req, res) => { /** @type {import('client')} */ const client = res.context.config.client; - const guildId = req.params.guild; + const guild = client.guilds.cache.get(req.params.guild); const categoryId = Number(req.params.category); const original = categoryId && await client.prisma.category.findUnique({ where: { id: categoryId } }); - if (!original || original.guildId !== guildId) return res.status(404).send(new Error('Not Found')); + if (!original || original.guildId !== guild.id) return res.status(404).send(new Error('Not Found')); const category = await client.prisma.category.delete({ where: { id: categoryId } }); + await updateStaffRoles(guild); + logAdminEvent(client, { action: 'delete', guildId: req.params.guild, @@ -137,6 +140,8 @@ module.exports.patch = fastify => ({ where: { id: categoryId }, }); + await updateStaffRoles(guild); + logAdminEvent(client, { action: 'update', diff: { diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js index 254a8d5..f92072a 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/index.js @@ -1,4 +1,5 @@ const { logAdminEvent } = require('../../../../../../lib/logging'); +const { updateStaffRoles } = require('../../../../../../lib/users'); const emoji = require('node-emoji'); const { ChannelType: { GuildCategory } } = require('discord.js'); const ms = require('ms'); @@ -93,6 +94,8 @@ module.exports.post = fastify => ({ }, }); + await updateStaffRoles(guild); + logAdminEvent(client, { action: 'create', guildId: guild.id,