mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-11-17 09:23:07 +02:00
perf: cache staff roles
This commit is contained in:
parent
db94ab770d
commit
d7e1b05586
@ -11,6 +11,29 @@ module.exports.getCommonGuilds = async (client, userId) => await client.guilds.c
|
||||
return !!member;
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {import("discord.js").Guild} guild
|
||||
* @returns {Promise<string[]>}
|
||||
*/
|
||||
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));
|
||||
};
|
@ -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: {
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user