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

@@ -17,10 +17,8 @@ type Parameters = {
animated?: string;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>,
) {
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
let axiosRes;
const params: Parameters = req.query,
userid = req.query.id[0];
@@ -29,22 +27,17 @@ export default async function handler(
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));
if (err) console.log(err);
if (err || axiosRes.status != 200)
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.`,
});
}
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-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[] => {
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;
}
};

View File

@@ -9,15 +9,14 @@ interface DeconstructedSnowflake {
const EPOCH = 1420070400000; // Discord's EPOCH
export function isSnowflake(snowflake: string): boolean {
export const isSnowflake = (snowflake: string): boolean => {
const { timestamp } = deconstruct(snowflake);
if (timestamp > EPOCH && timestamp <= 3619093655551) {
return true;
}
return false;
}
if (timestamp > EPOCH && timestamp <= 3619093655551) return true;
function deconstruct(snowflake: string): DeconstructedSnowflake {
return false;
};
const deconstruct = (snowflake: string): DeconstructedSnowflake => {
const BINARY = idToBinary(snowflake).padStart(64, "0");
return {
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
@@ -29,9 +28,9 @@ function deconstruct(snowflake: string): DeconstructedSnowflake {
increment: parseInt(BINARY.substring(52, 64), 2),
binary: BINARY,
};
}
};
function idToBinary(snowflake: string): string {
const idToBinary = (snowflake: string): string => {
let bin = "";
let high = parseInt(snowflake.slice(0, -10)) || 0;
let low = parseInt(snowflake.slice(-10));
@@ -44,4 +43,4 @@ function idToBinary(snowflake: string): string {
}
}
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"
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"