From 41ccd778c646848896ada34df7d54daa211637c5 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sat, 7 May 2022 21:16:18 +0100 Subject: [PATCH] Add GET & POST guild categories routes --- prisma/schema.prisma | 2 +- .../admin/guilds/[guild]/categories/index.js | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ecf4641..e6abcc5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -84,7 +84,7 @@ model Category { pingRoles Json @default("[]") questions Question[] ratelimit Int? - requiredRoles Json + requiredRoles Json @default("[]") requireTopic Boolean @default(false) staffRoles Json tickets Ticket[] diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js index e69de29..b702ba4 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/index.js @@ -0,0 +1,58 @@ +module.exports.get = fastify => ({ + handler: async (req, res) => { + /** @type {import('../../../../../../client')} */ + const client = res.context.config.client; + + const categories = await client.prisma.guild.findUnique({ where: { id: req.params.guild } }).categories(); + + res.send(categories); + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); + +module.exports.post = fastify => ({ + handler: async (req, res) => { + /** @type {import('../../../../../../client')} */ + const client = res.context.config.client; + + const user = await client.users.fetch(req.user.payload.id); + const guild = client.guilds.cache.get(req.params.guild); + const data = req.body; + const allow = ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES']; + + if (!data.discordCategory) { + const channel = await guild.channels.create(data.name, { + permissionOverwrites: [ + ...[ + { + deny: ['VIEW_CHANNEL'], + id: guild.roles.everyone, + }, + { + allow: allow, + id: client.user.id, + }, + ], + ...data.staffRoles.map(id => ({ + allow: allow, + id, + })), + ], + position: 1, + reason: `Tickets category created by ${user.tag}`, + type: 'GUILD_CATEGORY', + }); + data.discordCategory = channel.id; + } + + const category = await client.prisma.category.create({ + data: { + guild: { connect: { id: guild.id } }, + ...data, + }, + }); + + res.send(category); + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +});