feat(api): generate missing icons

This commit is contained in:
Isaac 2024-04-21 02:04:46 +01:00
parent c6a982e702
commit 46bd58daf6
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
5 changed files with 48 additions and 2 deletions

View File

@ -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`;

View File

@ -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)),

View File

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

View File

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

View File

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