Skip to content

Commit

Permalink
Merge pull request #59 from hexaaagon/main
Browse files Browse the repository at this point in the history
feat: add support of clan tag
  • Loading branch information
cnrad authored Sep 25, 2024
2 parents c783ae1 + 5615bc5 commit 3158aaf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ 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 `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.
Expand Down
5 changes: 2 additions & 3 deletions pages/api/[...id].ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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";
Expand All @@ -21,7 +20,7 @@ type Parameters = {
};

export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
let getUser;
let getUser: any = {};

if (!req.query.id)
return res.send({
Expand All @@ -37,7 +36,7 @@ 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.json());
} catch (error: any) {
if (error.response.data && error.response.data.error.message)
return res
Expand Down
8 changes: 8 additions & 0 deletions src/LanyardTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 33 additions & 1 deletion src/renderCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import escape from "escape-html";
type Parameters = {
theme?: string;
bg?: string;
clanbg?: string;
animated?: string;
hideDiscrim?: string;
hideStatus?: string;
Expand All @@ -17,6 +18,7 @@ type Parameters = {
hideProfile?: string;
hideActivity?: string;
hideSpotify?: string;
hideClan?: string;
ignoreAppId?: string;
showDisplayName?: string;
borderRadius?: string;
Expand Down Expand Up @@ -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 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) 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";
Expand All @@ -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;

Expand All @@ -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";
Expand Down Expand Up @@ -230,6 +242,26 @@ const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise<
}
</h1>
${hideClan ? "" : `
<span style="
background-color: ${clanBackgroundColor};
border-radius: 0.375rem;
padding-left: 0.5rem;
padding-right: 0.5rem;
margin-left: -6px;
margin-right: 12px;
display: flex;
align-items: center;
gap: 0.25rem;
font-size: 16px;
font-weight: 500;
height: 100%;
">
<img src="data:image/png;base64,${clanBadge!}" />
<p style="margin-bottom: 1.1rem">${escape(data.discord_user.clan!.tag)}</p>
</span>
`}
${hideBadges ? "" : flags.map(v => `
<img src="data:image/png;base64,${Badges[v]}" style="
width: auto;
Expand Down

0 comments on commit 3158aaf

Please sign in to comment.