discord-banners-js
Version:
Allows you to get a user banner in discord
39 lines (24 loc) • 1.24 kB
JavaScript
const { head } = require('axios');
const { Client } = require('discord.js');
const client = new Client({intents: 32767})
async function banner(user, token, { dynamicFormat = true, defaultFormat = "webp", size = 512, tkn = token } = {}) {
client.login(tkn)
if (![16, 32, 64, 128, 256, 512, 1024, 2048, 4096].includes(size)) {
throw new Error(`The size '${size}' is not supported`);
}
if (!["webp", "png", "jpg", "jpeg"].includes(defaultFormat)) {
throw new Error(`the format '${defaultFormat}' is not supported`);
}
const users = await client.api.users(user).get();
if (!users.banner) return null;
const query = `?size=${size}`;
const baseUrl = `https://cdn.discordapp.com/banners/${user}/${users.banner}`;
if (dynamicFormat) {
const { headers } = await head(baseUrl);
if (headers && headers.hasOwnProperty("content-type")) {
return baseUrl + (headers["content-type"] == "image/gif" ? ".gif" : `.${defaultFormat}`) + query;
}
}
return baseUrl + `.${defaultFormat}` + query;
}
module.exports = banner;