Update guilds route

This commit is contained in:
Isaac 2022-05-07 18:28:38 +01:00
parent ad9bd98933
commit c082552fae
2 changed files with 35 additions and 5 deletions

View File

@ -1,6 +1,6 @@
const fastify = require('fastify')(); const fastify = require('fastify')();
const oauth = require('@fastify/oauth2'); const oauth = require('@fastify/oauth2');
const { randomBytes } = require('crypto'); // const { randomBytes } = require('crypto');
const { short } = require('leeks.js'); const { short } = require('leeks.js');
const { join } = require('path'); const { join } = require('path');
const { readFiles } = require('node-dir'); const { readFiles } = require('node-dir');
@ -31,7 +31,8 @@ module.exports = client => {
cookieName: 'token', cookieName: 'token',
signed: false, signed: false,
}, },
secret: randomBytes(16).toString('hex'), // secret: randomBytes(16).toString('hex'),
secret: process.env.DB_ENCRYPTION_KEY,
}); });
// auth // auth
@ -44,6 +45,27 @@ module.exports = client => {
} }
}); });
fastify.decorate('isAdmin', async (req, res) => {
try {
const userId = req.user.payload.id;
const guildId = req.params.guild;
const guild = client.guilds.cache.get(guildId);
const guildMember = await guild.members.fetch(userId);
const isAdmin = guildMember.permissions.has('MANAGE_GUILD');
if (!isAdmin) {
return res.code(401).send({
error: 'Unauthorised',
message: 'User is not authorised for this action',
statusCode: 401,
});
}
} catch (err) {
res.send(err);
}
});
// logging // logging
fastify.addHook('onResponse', (req, res, done) => { fastify.addHook('onResponse', (req, res, done) => {
done(); done();

View File

@ -1,9 +1,17 @@
module.exports.get = fastify => ({ module.exports.get = fastify => ({
handler: async (req, res) => { handler: async (req, res) => {
const { client } = res.context.config; const { client } = res.context.config;
const user = await client.users.fetch(req.user.payload.id); const guilds = client.guilds.cache
console.log(req.user.payload.username, user?.tag); .filter(async guild => {
res.send(client.guilds.cache); const member = await guild.members.fetch(req.user.payload.id);
return member.permissions.has('MANAGE_GUILD');
})
.map(guild => ({
id: guild.id,
logo: guild.iconURL(),
name: guild.name,
}));
res.send(guilds);
}, },
onRequest: [fastify.authenticate], onRequest: [fastify.authenticate],
}); });