diff --git a/src/lib/misc.js b/src/lib/misc.js index 35885e6..3f23301 100644 --- a/src/lib/misc.js +++ b/src/lib/misc.js @@ -1,3 +1,6 @@ const { createHash } = require('crypto'); module.exports.md5 = str => createHash('md5').update(str).digest('hex'); module.exports.msToMins = ms => Number((ms / 1000 / 60).toFixed(2)); +module.exports.iconURL = guildLike => guildLike.icon + ? guildLike.client.rest.cdn.icon(guildLike.id, guildLike.icon) + : `https://api.dicebear.com/8.x/initials/png?seed=${encodeURIComponent(guildLike.name)}&size=96&backgroundType=gradientLinear&fontWeight=600`; diff --git a/src/routes/api/admin/guilds/[guild]/index.js b/src/routes/api/admin/guilds/[guild]/index.js index 39e49b9..06f4c65 100644 --- a/src/routes/api/admin/guilds/[guild]/index.js +++ b/src/routes/api/admin/guilds/[guild]/index.js @@ -1,4 +1,5 @@ /* eslint-disable no-underscore-dangle */ +const { iconURL } = require('../../../../../lib/misc'); const { getAvgResolutionTime, getAvgResponseTime, @@ -37,7 +38,7 @@ module.exports.get = fastify => ({ cached = { createdAt: settings.createdAt, id: guild.id, - logo: guild.iconURL(), + logo: iconURL(guild), name: guild.name, stats: { avgResolutionTime: ms(getAvgResolutionTime(closedTickets)), diff --git a/src/routes/api/admin/guilds/index.js b/src/routes/api/admin/guilds/index.js index c635440..b4779ab 100644 --- a/src/routes/api/admin/guilds/index.js +++ b/src/routes/api/admin/guilds/index.js @@ -1,4 +1,5 @@ const { PermissionsBitField } = require('discord.js'); +const { iconURL } = require('../../../../lib/misc'); module.exports.get = fastify => ({ handler: async (req, res) => { @@ -10,7 +11,14 @@ module.exports.get = fastify => ({ .map(guild => ({ added: client.guilds.cache.has(guild.id), id: guild.id, - logo: `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp`, + logo: iconURL( + client.guilds.cache.get(guild.id) || + { + client, + icon: guild.icon, + id: guild.id, + }, + ), name: guild.name, })), ); diff --git a/src/routes/api/guilds/[guild]/index.js b/src/routes/api/guilds/[guild]/index.js new file mode 100644 index 0000000..caff864 --- /dev/null +++ b/src/routes/api/guilds/[guild]/index.js @@ -0,0 +1,16 @@ +const { iconURL } = require('../../../../lib/misc'); + +module.exports.get = fastify => ({ + handler: async (req, res) => { + const { client } = req.routeOptions.config; + const guild = client.guilds.cache.get(req.params.guild); + if (!guild) return res.status(404).send(new Error('Not Found')); + res.send({ + id: guild.id, + logo: iconURL(guild), + name: guild.name, + }); + }, + onRequest: [fastify.authenticate], +}); + diff --git a/src/routes/api/guilds/index.js b/src/routes/api/guilds/index.js new file mode 100644 index 0000000..e608167 --- /dev/null +++ b/src/routes/api/guilds/index.js @@ -0,0 +1,18 @@ +const { iconURL } = require('../../../lib/misc'); + +module.exports.get = fastify => ({ + handler: async (req, res) => { + const { client } = req.routeOptions.config; + const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.accessToken}` } })).json(); + res.send( + guilds + .filter(guild => client.guilds.cache.has(guild.id)) + .map(guild => ({ + id: guild.id, + logo: iconURL(client.guilds.cache.get(guild.id)), + name: guild.name, + })), + ); + }, + onRequest: [fastify.authenticate], +});