diff --git a/pages/api/[...id].ts b/pages/api/[...id].ts index d9792c3..c578da8 100644 --- a/pages/api/[...id].ts +++ b/pages/api/[...id].ts @@ -4,8 +4,8 @@ import renderCard from "../../src/renderCard"; import { isSnowflake } from "../../src/snowflake"; type Data = { - id?: string | string[]; - error?: any; + id?: string | string[]; + error?: any; }; type Parameters = { @@ -14,38 +14,31 @@ type Parameters = { hideStatus?: string; hideDiscrim?: string; borderRadius?: string; - animated?: string; + animated?: string; }; -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const params: Parameters = req.query, - userid = req.query.id[0]; +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + let axiosRes; + const params: Parameters = req.query, + userid = req.query.id[0]; - if (!isSnowflake(userid)) - return res.send({ - error: `Specify a valid Discord user ID! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, - }); + if (!isSnowflake(userid)) + return res.send({ + error: `Specify a valid Discord user ID! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, + }); - let err: any; - const axiosRes = await axios - .get(`https://api.lanyard.rest/v1/users/${userid}`) - .catch((e) => (err = e)); + try { + axiosRes = await axios.get(`https://api.lanyard.rest/v1/users/${userid}`); + } catch (err) { + console.log(err); + return res.send({ + error: `Something went wrong! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, + }); + } - if (err) console.log(err); - if (err || axiosRes.status != 200) - return res.send({ - error: `Something went wrong! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, - }); + res.setHeader("Content-Type", "image/svg+xml; charset=utf-8"); + res.setHeader("content-security-policy", "default-src 'none'; img-src * data:; style-src 'unsafe-inline'"); - res.setHeader("Content-Type", "image/svg+xml; charset=utf-8"); - res.setHeader( - "content-security-policy", - "default-src 'none'; img-src * data:; style-src 'unsafe-inline'", - ); - - let svg = await renderCard(axiosRes.data, params); - res.status(200).send(svg as any); + let svg = await renderCard(axiosRes.data, params); + res.status(200).send(svg as any); } diff --git a/src/getFlags.ts b/src/getFlags.ts index 77b749f..36a9674 100644 --- a/src/getFlags.ts +++ b/src/getFlags.ts @@ -1,16 +1,16 @@ export const getFlags = (flag: number): string[] => { let flags: string[] = []; - if (flag & 1) flags.push("Discord_Employee") - if (flag & 2) flags.push("Partnered_Server_Owner") - if (flag & 4) flags.push("HypeSquad_Events") - if (flag & 8) flags.push("Bug_Hunter_Level_1") - if (flag & 64) flags.push("House_Bravery") - if (flag & 128) flags.push("House_Brilliance") - if (flag & 256) flags.push("House_Balance") - if (flag & 512) flags.push("Early_Supporter") - if (flag & 16384) flags.push("Bug_Hunter_Level_2") - if (flag & 131072) flags.push("Early_Verified_Bot_Developer") + if (flag & 1) flags.push("Discord_Employee"); + if (flag & 2) flags.push("Partnered_Server_Owner"); + if (flag & 4) flags.push("HypeSquad_Events"); + if (flag & 8) flags.push("Bug_Hunter_Level_1"); + if (flag & 64) flags.push("House_Bravery"); + if (flag & 128) flags.push("House_Brilliance"); + if (flag & 256) flags.push("House_Balance"); + if (flag & 512) flags.push("Early_Supporter"); + if (flag & 16384) flags.push("Bug_Hunter_Level_2"); + if (flag & 131072) flags.push("Early_Verified_Bot_Developer"); return flags; -} \ No newline at end of file +}; diff --git a/src/snowflake.ts b/src/snowflake.ts index a75ffec..8b4709f 100644 --- a/src/snowflake.ts +++ b/src/snowflake.ts @@ -1,47 +1,46 @@ interface DeconstructedSnowflake { - timestamp: number; - date: Date; - workerID: number; - processID: number; - increment: number; - binary: string; + timestamp: number; + date: Date; + workerID: number; + processID: number; + increment: number; + binary: string; } const EPOCH = 1420070400000; // Discord's EPOCH -export function isSnowflake(snowflake: string): boolean { - const { timestamp } = deconstruct(snowflake); - if (timestamp > EPOCH && timestamp <= 3619093655551) { - return true; - } - return false; -} +export const isSnowflake = (snowflake: string): boolean => { + const { timestamp } = deconstruct(snowflake); + if (timestamp > EPOCH && timestamp <= 3619093655551) return true; -function deconstruct(snowflake: string): DeconstructedSnowflake { - const BINARY = idToBinary(snowflake).padStart(64, "0"); - return { - timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH, - get date() { - return new Date(this.timestamp); - }, - workerID: parseInt(BINARY.substring(42, 47), 2), - processID: parseInt(BINARY.substring(47, 52), 2), - increment: parseInt(BINARY.substring(52, 64), 2), - binary: BINARY, - }; -} + return false; +}; -function idToBinary(snowflake: string): string { - let bin = ""; - let high = parseInt(snowflake.slice(0, -10)) || 0; - let low = parseInt(snowflake.slice(-10)); - while (low > 0 || high > 0) { - bin = String(low & 1) + bin; - low = Math.floor(low / 2); - if (high > 0) { - low += 5000000000 * (high % 2); - high = Math.floor(high / 2); - } - } - return bin; -} +const deconstruct = (snowflake: string): DeconstructedSnowflake => { + const BINARY = idToBinary(snowflake).padStart(64, "0"); + return { + timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH, + get date() { + return new Date(this.timestamp); + }, + workerID: parseInt(BINARY.substring(42, 47), 2), + processID: parseInt(BINARY.substring(47, 52), 2), + increment: parseInt(BINARY.substring(52, 64), 2), + binary: BINARY, + }; +}; + +const idToBinary = (snowflake: string): string => { + let bin = ""; + let high = parseInt(snowflake.slice(0, -10)) || 0; + let low = parseInt(snowflake.slice(-10)); + while (low > 0 || high > 0) { + bin = String(low & 1) + bin; + low = Math.floor(low / 2); + if (high > 0) { + low += 5000000000 * (high % 2); + high = Math.floor(high / 2); + } + } + return bin; +}; diff --git a/yarn.lock b/yarn.lock index c71d69f..a719565 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2553,6 +2553,11 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.1.tgz#76903c3f8c4449bc9ac597acefa24dc5ad4cbea6" + integrity sha512-p+vNbgpLjif/+D+DwAZAbndtRrR0md0MwfmOVN9N+2RgyACMT+7tfaRnT+WDPkqnuVwleyuBIG2XBxKDme3hPA== + process-nextick-args@~2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"