feat: update command permissions (closes #392)

This commit is contained in:
Isaac 2023-03-10 23:28:46 +00:00
parent b8c2a7cc13
commit b14f057dd0
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 68 additions and 1 deletions

View File

@ -1,6 +1,7 @@
const { logAdminEvent } = require('../../../../../../../lib/logging'); const { logAdminEvent } = require('../../../../../../../lib/logging');
const { updateStaffRoles } = require('../../../../../../../lib/users'); const { updateStaffRoles } = require('../../../../../../../lib/users');
const { randomUUID } = require('crypto'); const { randomUUID } = require('crypto');
const { ApplicationCommandPermissionType } = require('discord.js');
module.exports.delete = fastify => ({ module.exports.delete = fastify => ({
handler: async (req, res) => { handler: async (req, res) => {
@ -71,6 +72,7 @@ module.exports.patch = fastify => ({
const client = res.context.config.client; const client = res.context.config.client;
const guildId = req.params.guild; const guildId = req.params.guild;
const categoryId = Number(req.params.category); const categoryId = Number(req.params.category);
/** @type {import('discord.js').Guild} */
const guild = client.guilds.cache.get(req.params.guild); const guild = client.guilds.cache.get(req.params.guild);
const data = req.body; const data = req.body;
@ -144,6 +146,37 @@ module.exports.patch = fastify => ({
await client.tickets.getCategory(categoryId, true); await client.tickets.getCategory(categoryId, true);
await updateStaffRoles(guild); await updateStaffRoles(guild);
if (req.user.payload.accessToken && JSON.stringify(category.staffRoles) !== JSON.stringify(original.staffRoles)) {
Promise.all([
'Create ticket for user',
'claim',
'force-close',
'move',
'priority',
'release',
].map(name =>
client.application.commands.permissions.set({
command: client.application.commands.cache.find(cmd => cmd.name === name),
guild,
permissions: [
{
id: guild.id, // @everyone
permission: false,
type: ApplicationCommandPermissionType.Role,
},
...category.staffRoles.map(id => ({
id,
permission: true,
type: ApplicationCommandPermissionType.Role,
})),
],
token: req.user.payload.accessToken,
}),
))
.then(() => client.log.success('Updated application command permissions in "%s"', guild.name))
.catch(error => client.log.error(error));
}
logAdminEvent(client, { logAdminEvent(client, {
action: 'update', action: 'update',
diff: { diff: {

View File

@ -1,7 +1,10 @@
const { logAdminEvent } = require('../../../../../../lib/logging'); const { logAdminEvent } = require('../../../../../../lib/logging');
const { updateStaffRoles } = require('../../../../../../lib/users'); const { updateStaffRoles } = require('../../../../../../lib/users');
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const { ChannelType: { GuildCategory } } = require('discord.js'); const {
ApplicationCommandPermissionType,
ChannelType: { GuildCategory },
} = require('discord.js');
const ms = require('ms'); const ms = require('ms');
module.exports.get = fastify => ({ module.exports.get = fastify => ({
@ -98,6 +101,37 @@ module.exports.post = fastify => ({
await client.tickets.getCategory(category.id, true); await client.tickets.getCategory(category.id, true);
await updateStaffRoles(guild); await updateStaffRoles(guild);
if (req.user.payload.accessToken) {
Promise.all([
'Create ticket for user',
'claim',
'force-close',
'move',
'priority',
'release',
].map(name =>
client.application.commands.permissions.set({
command: client.application.commands.cache.find(cmd => cmd.name === name),
guild,
permissions: [
{
id: guild.id, // @everyone
permission: false,
type: ApplicationCommandPermissionType.Role,
},
...category.staffRoles.map(id => ({
id,
permission: true,
type: ApplicationCommandPermissionType.Role,
})),
],
token: req.user.payload.accessToken,
}),
))
.then(() => client.log.success('Updated application command permissions in "%s"', guild.name))
.catch(error => client.log.error(error));
}
logAdminEvent(client, { logAdminEvent(client, {
action: 'create', action: 'create',
guildId: guild.id, guildId: guild.id,