Feat: Refactoring

This commit is contained in:
looskie
2021-06-22 14:07:25 -04:00
parent 4e0af9327f
commit 75870b6c0b
4 changed files with 78 additions and 81 deletions

View File

@@ -4,8 +4,8 @@ import renderCard from "../../src/renderCard";
import { isSnowflake } from "../../src/snowflake"; import { isSnowflake } from "../../src/snowflake";
type Data = { type Data = {
id?: string | string[]; id?: string | string[];
error?: any; error?: any;
}; };
type Parameters = { type Parameters = {
@@ -14,38 +14,31 @@ type Parameters = {
hideStatus?: string; hideStatus?: string;
hideDiscrim?: string; hideDiscrim?: string;
borderRadius?: string; borderRadius?: string;
animated?: string; animated?: string;
}; };
export default async function handler( export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
req: NextApiRequest, let axiosRes;
res: NextApiResponse<Data>, const params: Parameters = req.query,
) { userid = req.query.id[0];
const params: Parameters = req.query,
userid = req.query.id[0];
if (!isSnowflake(userid)) if (!isSnowflake(userid))
return res.send({ return res.send({
error: `Specify a valid Discord user ID! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`, error: `Specify a valid Discord user ID! If everything looks correct and this still occurs, please contact @cnraddd on Twitter.`,
}); });
let err: any; try {
const axiosRes = await axios axiosRes = await axios.get(`https://api.lanyard.rest/v1/users/${userid}`);
.get(`https://api.lanyard.rest/v1/users/${userid}`) } catch (err) {
.catch((e) => (err = e)); 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); res.setHeader("Content-Type", "image/svg+xml; charset=utf-8");
if (err || axiosRes.status != 200) res.setHeader("content-security-policy", "default-src 'none'; img-src * data:; style-src 'unsafe-inline'");
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"); let svg = await renderCard(axiosRes.data, params);
res.setHeader( res.status(200).send(svg as any);
"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);
} }

View File

@@ -1,16 +1,16 @@
export const getFlags = (flag: number): string[] => { export const getFlags = (flag: number): string[] => {
let flags: string[] = []; let flags: string[] = [];
if (flag & 1) flags.push("Discord_Employee") if (flag & 1) flags.push("Discord_Employee");
if (flag & 2) flags.push("Partnered_Server_Owner") if (flag & 2) flags.push("Partnered_Server_Owner");
if (flag & 4) flags.push("HypeSquad_Events") if (flag & 4) flags.push("HypeSquad_Events");
if (flag & 8) flags.push("Bug_Hunter_Level_1") if (flag & 8) flags.push("Bug_Hunter_Level_1");
if (flag & 64) flags.push("House_Bravery") if (flag & 64) flags.push("House_Bravery");
if (flag & 128) flags.push("House_Brilliance") if (flag & 128) flags.push("House_Brilliance");
if (flag & 256) flags.push("House_Balance") if (flag & 256) flags.push("House_Balance");
if (flag & 512) flags.push("Early_Supporter") if (flag & 512) flags.push("Early_Supporter");
if (flag & 16384) flags.push("Bug_Hunter_Level_2") if (flag & 16384) flags.push("Bug_Hunter_Level_2");
if (flag & 131072) flags.push("Early_Verified_Bot_Developer") if (flag & 131072) flags.push("Early_Verified_Bot_Developer");
return flags; return flags;
} };

View File

@@ -1,47 +1,46 @@
interface DeconstructedSnowflake { interface DeconstructedSnowflake {
timestamp: number; timestamp: number;
date: Date; date: Date;
workerID: number; workerID: number;
processID: number; processID: number;
increment: number; increment: number;
binary: string; binary: string;
} }
const EPOCH = 1420070400000; // Discord's EPOCH const EPOCH = 1420070400000; // Discord's EPOCH
export function isSnowflake(snowflake: string): boolean { export const isSnowflake = (snowflake: string): boolean => {
const { timestamp } = deconstruct(snowflake); const { timestamp } = deconstruct(snowflake);
if (timestamp > EPOCH && timestamp <= 3619093655551) { if (timestamp > EPOCH && timestamp <= 3619093655551) return true;
return true;
}
return false;
}
function deconstruct(snowflake: string): DeconstructedSnowflake { return false;
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,
};
}
function idToBinary(snowflake: string): string { const deconstruct = (snowflake: string): DeconstructedSnowflake => {
let bin = ""; const BINARY = idToBinary(snowflake).padStart(64, "0");
let high = parseInt(snowflake.slice(0, -10)) || 0; return {
let low = parseInt(snowflake.slice(-10)); timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
while (low > 0 || high > 0) { get date() {
bin = String(low & 1) + bin; return new Date(this.timestamp);
low = Math.floor(low / 2); },
if (high > 0) { workerID: parseInt(BINARY.substring(42, 47), 2),
low += 5000000000 * (high % 2); processID: parseInt(BINARY.substring(47, 52), 2),
high = Math.floor(high / 2); increment: parseInt(BINARY.substring(52, 64), 2),
} binary: BINARY,
} };
return bin; };
}
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;
};

View File

@@ -2553,6 +2553,11 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== 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: process-nextick-args@~2.0.0:
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"