From 08dd29fd05bf00c6a946d966514c7ef9aae731c0 Mon Sep 17 00:00:00 2001 From: Hexagonn Date: Tue, 17 Sep 2024 21:24:28 +0700 Subject: [PATCH 1/7] feat(card): implement clan tags --- package.json | 2 ++ pages/api/[...id].ts | 23 +++++++++++++++++++++-- src/LanyardTypes.ts | 8 ++++++++ src/renderCard.tsx | 34 +++++++++++++++++++++++++++++++++- yarn.lock | 17 +++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index eb29606..0ca8549 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,14 @@ "dependencies": { "@types/escape-html": "^1.0.1", "@types/ioredis": "^4.28.8", + "@types/json-bigint": "^1.0.4", "@types/styled-components": "^5.1.10", "axios": "^0.21.1", "escape-html": "^1.0.3", "framer-motion": "^4.1.17", "image-to-base64": "^2.2.0", "ioredis": "^4.28.5", + "json-bigint": "^1.0.0", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0", diff --git a/pages/api/[...id].ts b/pages/api/[...id].ts index 4810697..2c92fcb 100644 --- a/pages/api/[...id].ts +++ b/pages/api/[...id].ts @@ -3,6 +3,7 @@ import axios from "axios"; import renderCard from "../../src/renderCard"; import { isSnowflake } from "../../src/snowflake"; import redis from "../../src/redis"; +import JSONbig from "json-bigint"; type Data = { id?: string | string[]; @@ -20,8 +21,24 @@ type Parameters = { animated?: string; }; +const convertBigIntToString = (obj: { [key: string]: any }): { [key: string]: any } => { + const result: { [key: string]: any } = Array.isArray(obj) ? [] : {}; + + for (const key in obj) { + if (typeof obj[key] === "bigint") { + result[key] = obj[key].toString(); + } else if (typeof obj[key] === "object" && obj[key] !== null) { + result[key] = convertBigIntToString(obj[key]); + } else { + result[key] = obj[key]; + } + } + + return result; +}; + export default async function handler(req: NextApiRequest, res: NextApiResponse) { - let getUser; + let getUser: any = {}; if (!req.query.id) return res.send({ @@ -37,7 +54,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }); try { - getUser = await axios(`https://api.lanyard.rest/v1/users/${userId}`); + getUser.data = await fetch(`https://api.lanyard.rest/v1/users/${userId}`) + .then(res => res.text()) + .then(res => convertBigIntToString(JSONbig.parse(res))); } catch (error: any) { if (error.response.data && error.response.data.error.message) return res diff --git a/src/LanyardTypes.ts b/src/LanyardTypes.ts index ae96cb3..d09ef5c 100644 --- a/src/LanyardTypes.ts +++ b/src/LanyardTypes.ts @@ -37,6 +37,14 @@ export interface DiscordUser { avatar: string; global_name: string; display_name: string; + clan: ClanTag | null; +} + +export interface ClanTag { + tag: string; + badge: string; + identity_enabled: boolean; + identity_guild_id: number; } export interface Activity { diff --git a/src/renderCard.tsx b/src/renderCard.tsx index a8c1480..b8ce6fd 100644 --- a/src/renderCard.tsx +++ b/src/renderCard.tsx @@ -9,6 +9,7 @@ import escape from "escape-html"; type Parameters = { theme?: string; bg?: string; + clanbg?: string; animated?: string; hideDiscrim?: string; hideStatus?: string; @@ -17,6 +18,7 @@ type Parameters = { hideProfile?: string; hideActivity?: string; hideSpotify?: string; + hideClanTag?: string; ignoreAppId?: string; showDisplayName?: string; borderRadius?: string; @@ -71,12 +73,13 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< let hideProfile = parseBool(params.hideProfile); let hideActivity = params.hideActivity ?? "false"; let hideSpotify = parseBool(params.hideSpotify); + let hideClanTag = parseBool(params.hideClanTag); let ignoreAppId = parseAppId(params.ignoreAppId); let hideDiscrim = parseBool(params.hideDiscrim); let showDisplayName = parseBool(params.showDisplayName); - if (parseBool(params.hideDiscrim) || body.data.discord_user.discriminator === "0") hideDiscrim = true; + if (!body.data.discord_user.clan) hideClanTag = true; if (data.activities[0]?.emoji?.animated) statusExtension = "gif"; if (data.discord_user.avatar && data.discord_user.avatar.startsWith("a_")) avatarExtension = "gif"; if (params.animated === "false") avatarExtension = "webp"; @@ -85,6 +88,8 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< theme = "light"; } if (params.bg) backgroundColor = params.bg; + let clanBackgroundColor: string = theme === "light" ? "#e0dede" : "#111214"; + if (params.clanbg) clanBackgroundColor = params.clanbg; if (params.idleMessage) idleMessage = params.idleMessage; if (params.borderRadius) borderRadius = params.borderRadius; @@ -103,6 +108,13 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< ); } + let clanBadge: string; + if (data.discord_user.clan) { + clanBadge = await encodeBase64( + `https://cdn.discordapp.com/clan-badges/${data.discord_user.clan.identity_guild_id}/${data.discord_user.clan.badge}.png?size=16` + ); + } + switch (data.discord_status) { case "online": avatarBorderColor = "#43B581"; @@ -230,6 +242,26 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< } + ${hideClanTag ? "" : ` + + +

${escape(data.discord_user.clan!.tag)}

+
+ `} + ${hideBadges ? "" : flags.map(v => ` Date: Sat, 21 Sep 2024 09:49:28 +0700 Subject: [PATCH 2/7] chore(dependencies): remove `json-bigint` package --- package.json | 2 -- pages/api/[...id].ts | 6 +----- yarn.lock | 17 ----------------- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/package.json b/package.json index 0ca8549..eb29606 100644 --- a/package.json +++ b/package.json @@ -11,14 +11,12 @@ "dependencies": { "@types/escape-html": "^1.0.1", "@types/ioredis": "^4.28.8", - "@types/json-bigint": "^1.0.4", "@types/styled-components": "^5.1.10", "axios": "^0.21.1", "escape-html": "^1.0.3", "framer-motion": "^4.1.17", "image-to-base64": "^2.2.0", "ioredis": "^4.28.5", - "json-bigint": "^1.0.0", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0", diff --git a/pages/api/[...id].ts b/pages/api/[...id].ts index 2c92fcb..a625571 100644 --- a/pages/api/[...id].ts +++ b/pages/api/[...id].ts @@ -1,9 +1,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import axios from "axios"; import renderCard from "../../src/renderCard"; import { isSnowflake } from "../../src/snowflake"; import redis from "../../src/redis"; -import JSONbig from "json-bigint"; type Data = { id?: string | string[]; @@ -54,9 +52,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }); try { - getUser.data = await fetch(`https://api.lanyard.rest/v1/users/${userId}`) - .then(res => res.text()) - .then(res => convertBigIntToString(JSONbig.parse(res))); + getUser.data = await fetch(`https://api.lanyard.rest/v1/users/${userId}`).then(res => res.json()); } catch (error: any) { if (error.response.data && error.response.data.error.message) return res diff --git a/yarn.lock b/yarn.lock index 7ed0288..ac5363d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -364,11 +364,6 @@ dependencies: "@types/node" "*" -"@types/json-bigint@^1.0.4": - version "1.0.4" - resolved "https://registry.yarnpkg.com/@types/json-bigint/-/json-bigint-1.0.4.tgz#250d29e593375499d8ba6efaab22d094c3199ef3" - integrity sha512-ydHooXLbOmxBbubnA7Eh+RpBzuaIiQjh8WGJYQB50JFGFrdxW7JzVlyEV7fAXw0T2sqJ1ysTneJbiyNLqZRAag== - "@types/json5@^0.0.29": version "0.0.29" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" @@ -657,11 +652,6 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== -bignumber.js@^9.0.0: - version "9.1.2" - resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c" - integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug== - brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" @@ -1719,13 +1709,6 @@ jsesc@^2.5.1: resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4" integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA== -json-bigint@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/json-bigint/-/json-bigint-1.0.0.tgz#ae547823ac0cad8398667f8cd9ef4730f5b01ff1" - integrity sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ== - dependencies: - bignumber.js "^9.0.0" - json-buffer@3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.1.tgz#9338802a30d3b6605fbe0613e094008ca8c05a13" From cf793f08fee759ebd38e118004a4f3b6d66444e4 Mon Sep 17 00:00:00 2001 From: Hexagonn Date: Sat, 21 Sep 2024 09:53:34 +0700 Subject: [PATCH 3/7] chore(bigint): clean up unneeded function --- pages/api/[...id].ts | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/pages/api/[...id].ts b/pages/api/[...id].ts index a625571..928c003 100644 --- a/pages/api/[...id].ts +++ b/pages/api/[...id].ts @@ -19,22 +19,6 @@ type Parameters = { animated?: string; }; -const convertBigIntToString = (obj: { [key: string]: any }): { [key: string]: any } => { - const result: { [key: string]: any } = Array.isArray(obj) ? [] : {}; - - for (const key in obj) { - if (typeof obj[key] === "bigint") { - result[key] = obj[key].toString(); - } else if (typeof obj[key] === "object" && obj[key] !== null) { - result[key] = convertBigIntToString(obj[key]); - } else { - result[key] = obj[key]; - } - } - - return result; -}; - export default async function handler(req: NextApiRequest, res: NextApiResponse) { let getUser: any = {}; From 9b817b7a31340a846d8333ecf5327172810d4971 Mon Sep 17 00:00:00 2001 From: Hexagonn <128217934+hexaaagon@users.noreply.github.com> Date: Sat, 21 Sep 2024 10:04:14 +0700 Subject: [PATCH 4/7] feat(readme): add `hideClanTag` params on README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 31c079a..d8bb665 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,9 @@ If you don't want people seeing your status, append the query param `hideStatus= If you don't want people seeing the elapsed time on an activity, append the query param `hideTimestamp=true` to the end of the URL. Elapsed time is shown by default. +## ____Hide Clan Tag___ +If you don't want people seeing your Clan Tag (formerly known as Guilds), append the query param `hideClanTag=true` to the end of the URL. Clan Tag is shown by default. + ### ___Hide Badges___ If you don't want people seeing the badges you have on Discord, append the query param `hideBadges=true` to the end of the URL. Badges are shown by default. From 24b496ffe442c7df069d225228daa00f8fee9e8c Mon Sep 17 00:00:00 2001 From: Hexagonn Date: Tue, 24 Sep 2024 15:34:17 +0700 Subject: [PATCH 5/7] refactor: change variable `hideClanTag` to `hideClan` --- README.md | 46 ++++++++++++++++++++++++---------------------- src/renderCard.tsx | 8 ++++---- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d8bb665..7dfb591 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ + # lanyard-profile-readme 🏷️ Utilize Lanyard to display your Discord Presence in your GitHub Profile @@ -25,66 +26,67 @@ When others click it, they will be directed to your actual Discord profile. Neat There are a few options to customize this display using query parameters: -### ___Theme___ +### **_Theme_** -Append the query param `theme=:theme` to the end of the URL, replacing `:theme` with either `light` or `dark`. This will change the background and the font colors, but the background can be overridden with the ___Background Color___ parameter. +Append the query param `theme=:theme` to the end of the URL, replacing `:theme` with either `light` or `dark`. This will change the background and the font colors, but the background can be overridden with the **_Background Color_** parameter. -### ___Background Color___ +### **_Background Color_** Append the query param `bg=:color` to the end of the URL, replacing `:color` with a hex color of your choice (omit the #) -### ___Border Radius___ +### **_Border Radius_** Append the query param `borderRadius=:radius` to the end of the URL, replacing `:radius` with a radius of your choice. (default `10px`) -### ___Toggle Animated Avatar___ +### **_Toggle Animated Avatar_** If you have an animated avatar, append the query param `animated=:bool` to the end of the URL, replacing `:bool` with `true` or `false`. This is set to `true` by default. -### ___Custom Idle Message___ +### **_Custom Idle Message_** -If you don't want the default "`I'm not currently doing anything!`" as your idle message, you can change it by appending `idleMessage=:yourmessage` to the end of the URL. +If you don't want the default "`I'm not currently doing anything!`" as your idle message, you can change it by appending `idleMessage=:yourmessage` to the end of the URL. -### ___Show Display Name___ +### **_Show Display Name_** -If you'd like to show your global display name as well as your username, append the query param `showDisplayName=true` to the end of the URL. This is set to `false` by default. +If you'd like to show your global display name as well as your username, append the query param `showDisplayName=true` to the end of the URL. This is set to `false` by default. -### ___Hide Status___ +### **_Hide Status_** -If you don't want people seeing your status, append the query param `hideStatus=true` to the end of the URL. Your status is shown by default if you have one. +If you don't want people seeing your status, append the query param `hideStatus=true` to the end of the URL. Your status is shown by default if you have one. -### ___Hide Elapsed Time___ +### **_Hide Elapsed Time_** If you don't want people seeing the elapsed time on an activity, append the query param `hideTimestamp=true` to the end of the URL. Elapsed time is shown by default. -## ____Hide Clan Tag___ -If you don't want people seeing your Clan Tag (formerly known as Guilds), append the query param `hideClanTag=true` to the end of the URL. Clan Tag is shown by default. +### **_Hide Clan Tag_** -### ___Hide Badges___ +If you don't want people seeing your Clan Tag (formerly known as Guilds), append the query param `hideClan=true` to the end of the URL. Clan Tag is shown by default. + +### **_Hide Badges_** If you don't want people seeing the badges you have on Discord, append the query param `hideBadges=true` to the end of the URL. Badges are shown by default. -### ___Hide Profile___ +### **_Hide Profile_** If you don't want people seeing the profile you have on Discord, append the query param `hideProfile=true` to the end of the URL. Profile are shown by default. -### ___Hide Activity___ +### **_Hide Activity_** If you don't want people seeing the your activity, append the query param `hideActivity=true` to the end of the URL or use `hideActivity=whenNotUsed` to hide activity section when there's no activity to display. Activity are shown by default. -### ___Hide Spotify___ +### **_Hide Spotify_** If you don't want people seeing your Spotify activity, append the query param `hideSpotify=true` to the end of the URL. Spotify activity is shown by default. -### ___Hide App by ID___ +### **_Hide App by ID_** If you don't want display a specific application, append the query param `ignoreAppId=:app_id` to the end of the URL, IDs separate by `,`. -### ___Hide Discriminator___ (DEPRECATED soon) +### **_Hide Discriminator_** (DEPRECATED soon) -If you don't want people seeing your discriminator (most likely for privacy reasons), append the query param `hideDiscrim=true` to the end of the URL. Your discriminator is shown by default. +If you don't want people seeing your discriminator (most likely for privacy reasons), append the query param `hideDiscrim=true` to the end of the URL. Your discriminator is shown by default. -## ___Example URL and result___ +## **_Example URL and result_** ``` [![Discord Presence](https://lanyard-profile-readme.vercel.app/api/94490510688792576?theme=light&bg=809ecf&animated=false&hideDiscrim=true&borderRadius=30px&idleMessage=Probably%20doing%20something%20else...)](https://discord.com/users/94490510688792576) diff --git a/src/renderCard.tsx b/src/renderCard.tsx index b8ce6fd..fdc5677 100644 --- a/src/renderCard.tsx +++ b/src/renderCard.tsx @@ -18,7 +18,7 @@ type Parameters = { hideProfile?: string; hideActivity?: string; hideSpotify?: string; - hideClanTag?: string; + hideClan?: string; ignoreAppId?: string; showDisplayName?: string; borderRadius?: string; @@ -73,13 +73,13 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< let hideProfile = parseBool(params.hideProfile); let hideActivity = params.hideActivity ?? "false"; let hideSpotify = parseBool(params.hideSpotify); - let hideClanTag = parseBool(params.hideClanTag); + let hideClan = parseBool(params.hideClan); let ignoreAppId = parseAppId(params.ignoreAppId); let hideDiscrim = parseBool(params.hideDiscrim); let showDisplayName = parseBool(params.showDisplayName); if (parseBool(params.hideDiscrim) || body.data.discord_user.discriminator === "0") hideDiscrim = true; - if (!body.data.discord_user.clan) hideClanTag = true; + if (!body.data.discord_user.clan) hideClan = true; if (data.activities[0]?.emoji?.animated) statusExtension = "gif"; if (data.discord_user.avatar && data.discord_user.avatar.startsWith("a_")) avatarExtension = "gif"; if (params.animated === "false") avatarExtension = "webp"; @@ -242,7 +242,7 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise< } - ${hideClanTag ? "" : ` + ${hideClan ? "" : ` Date: Tue, 24 Sep 2024 15:49:57 +0700 Subject: [PATCH 7/7] fix(readme): forgot to change the var --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c80c47c..7368182 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ If you don't want people seeing the elapsed time on an activity, append the quer ### ___Hide Clan Tag___ -If you don't want people seeing your Clan Tag (formerly known as Guilds), append the query param `hideClanTag=true` to the end of the URL. Clan Tag is shown by default. +If you don't want people seeing your Clan Tag (formerly known as Guilds), append the query param `hideClan=true` to the end of the URL. Clan Tag is shown by default. ### ___Hide Badges___