mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-11-17 17:23:08 +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;
|
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
|
* @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) => {
|
module.exports.isStaff = async (guild, userId) => {
|
||||||
/** @type {import("client")} */
|
/** @type {import("client")} */
|
||||||
const client = guild.client;
|
const client = guild.client;
|
||||||
if (guild.client.supers.includes(userId)) return true;
|
if (client.supers.includes(userId)) return true;
|
||||||
const guildMember = await guild.members.fetch(userId);
|
const guildMember = guild.members.cache.get(userId) || await guild.members.fetch(userId);
|
||||||
if (guildMember?.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true;
|
if (guildMember.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true;
|
||||||
const { categories } = await client.prisma.guild.findUnique({
|
const staffRoles = await client.keyv.get(`cache/guild-staff:${guild.id}`) || await updateStaffRoles(guild);
|
||||||
select: { categories: true },
|
return staffRoles.some(r => guildMember.roles.cache.has(r));
|
||||||
where: { id: guild.id },
|
|
||||||
});
|
|
||||||
return categories.some(cat => cat.roles.some(r => guildMember.roles.cache.has(r)));
|
|
||||||
};
|
};
|
@ -1,16 +1,19 @@
|
|||||||
const { logAdminEvent } = require('../../../../../../../lib/logging');
|
const { logAdminEvent } = require('../../../../../../../lib/logging');
|
||||||
|
const { updateStaffRoles } = require('../../../../../../../lib/users');
|
||||||
const { randomUUID } = require('crypto');
|
const { randomUUID } = require('crypto');
|
||||||
|
|
||||||
module.exports.delete = fastify => ({
|
module.exports.delete = fastify => ({
|
||||||
handler: async (req, res) => {
|
handler: async (req, res) => {
|
||||||
/** @type {import('client')} */
|
/** @type {import('client')} */
|
||||||
const client = res.context.config.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 categoryId = Number(req.params.category);
|
||||||
const original = categoryId && await client.prisma.category.findUnique({ where: { id: categoryId } });
|
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 } });
|
const category = await client.prisma.category.delete({ where: { id: categoryId } });
|
||||||
|
|
||||||
|
await updateStaffRoles(guild);
|
||||||
|
|
||||||
logAdminEvent(client, {
|
logAdminEvent(client, {
|
||||||
action: 'delete',
|
action: 'delete',
|
||||||
guildId: req.params.guild,
|
guildId: req.params.guild,
|
||||||
@ -137,6 +140,8 @@ module.exports.patch = fastify => ({
|
|||||||
where: { id: categoryId },
|
where: { id: categoryId },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await updateStaffRoles(guild);
|
||||||
|
|
||||||
logAdminEvent(client, {
|
logAdminEvent(client, {
|
||||||
action: 'update',
|
action: 'update',
|
||||||
diff: {
|
diff: {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const { logAdminEvent } = require('../../../../../../lib/logging');
|
const { logAdminEvent } = require('../../../../../../lib/logging');
|
||||||
|
const { updateStaffRoles } = require('../../../../../../lib/users');
|
||||||
const emoji = require('node-emoji');
|
const emoji = require('node-emoji');
|
||||||
const { ChannelType: { GuildCategory } } = require('discord.js');
|
const { ChannelType: { GuildCategory } } = require('discord.js');
|
||||||
const ms = require('ms');
|
const ms = require('ms');
|
||||||
@ -93,6 +94,8 @@ module.exports.post = fastify => ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await updateStaffRoles(guild);
|
||||||
|
|
||||||
logAdminEvent(client, {
|
logAdminEvent(client, {
|
||||||
action: 'create',
|
action: 'create',
|
||||||
guildId: guild.id,
|
guildId: guild.id,
|
||||||
|
Loading…
Reference in New Issue
Block a user