Add GET & POST guild categories routes

This commit is contained in:
Isaac 2022-05-07 21:16:18 +01:00
parent 66b46cf3e2
commit 41ccd778c6
2 changed files with 59 additions and 1 deletions

View File

@ -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[]

View File

@ -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],
});