feat(api): show guilds that the bot isn't in

This commit is contained in:
Isaac
2023-03-10 00:24:33 +00:00
parent 25d7cdaee8
commit ea9d3e4e33
6 changed files with 82 additions and 74 deletions

View File

@@ -27,7 +27,7 @@ module.exports = async client => {
},
},
name: 'discord',
scope: ['identify'],
scope: ['applications.commands.permissions.update', 'guilds', 'identify'],
startRedirectPath: '/auth/login',
});

View File

@@ -3,18 +3,17 @@ const { PermissionsBitField } = require('discord.js');
module.exports.get = fastify => ({
handler: async (req, res) => {
const { client } = res.context.config;
const guilds = client.guilds.cache
.filter(async guild => {
const member = await guild.members.fetch(req.user.payload.id);
if (!member) return false;
return member.permissions.has(PermissionsBitField.Flags.ManageGuild);
})
.map(guild => ({
id: guild.id,
logo: guild.iconURL(),
name: guild.name,
}));
res.send(guilds);
const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.payload.access_token}` } })).json();
res.send(
guilds
.filter(guild => guild.owner || new PermissionsBitField(guild.permissions.toString()).has(PermissionsBitField.Flags.ManageGuild))
.map(guild => ({
added: client.guilds.cache.has(guild.id),
id: guild.id,
logo: `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp`,
name: guild.name,
})),
);
},
onRequest: [fastify.authenticate],
});

View File

@@ -1,16 +1,19 @@
const { domain } = require('../../lib/http');
module.exports.get = () => ({
handler: async function (req, res) { // must NOT use arrow function syntax
handler: async function (req, res) { // MUST NOT use arrow function syntax
const {
access_token, expires_in,
access_token,
expires_in,
} = await this.discord.getAccessTokenFromAuthorizationCodeFlow(req);
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${access_token}` } })).json();
const payload = {
access_token,
avatar: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.webp`,
discriminator: user.discriminator,
expiresAt: Date.now() + (expires_in * 1000),
id: user.id,
locale: user.locale,
username: user.username,
};

View File

@@ -1,5 +1,5 @@
module.exports.get = () => ({
handler: async function (req, res) { // must NOT use arrow function syntax
handler: async function (req, res) {
res
.clearCookie('token', '/')
.send('Logged out.');