Files
lanyard-profile-readme/src/renderCard.tsx
2021-06-20 22:15:43 -04:00

339 lines
18 KiB
TypeScript

//probably the messiest code i've ever written but it works so :)
import * as LanyardTypes from './LanyardTypes';
import { getFlags } from "./getFlags";
import encodeBase64 from './toBase64';
import { Badges } from '../public/assets/badges/BadgesEncoded'
type Parameters = {
theme?: string;
bg?: string;
animated?: string;
hideDiscrim?: string;
hideStatus?: string;
}
const renderCard = async (body: LanyardTypes.Root, params: Parameters): Promise<any> => {
let avatarBorderColor: string = "#747F8D";
let userStatus: string = "";
let avatarExtension: string = "webp";
let statusExtension: string = "webp";
let activity: any = false;
let backgroundColor: string = '1a1c1f';
let theme = 'dark';
let discrim = 'show';
let hideStatus = 'false';
if(body.data.activities[0]){
if(body.data.activities[0].emoji && body.data.activities[0].emoji.animated === true) statusExtension = "gif";
}
if(body.data.activities.length > 0) {
if(body.data.activities[Object.keys(body.data.activities).length - 1].type === 0) activity = body.data.activities[Object.keys(body.data.activities).length - 1];
}
let defaultAvatar: string = "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAAY1BMVEVYZfJib/OMlfahqPeWn/eBi/XLz/vq6/7////19f5tePTq7P22vPnV2Pyrsvirsvl3gvT09f7Axfp3gfRtePNsePPg4v22vPq2u/qCi/WhqPjf4/zf4v2Xn/essvjLzvuXnvdbidFTAAAETElEQVR4AezBgQAAAACAoP2pF6kCAAAAAAAAAAAAAAAAAAAAAIDZudMtV1UlDuCFRKlWIEJ6uOwbzXn/lzzzYc/GWiT6zya/79WrLeYSc5Vq9IFWa3Sr6JehWt0ZZn5RtFJvmHnodPsrPLx1/B9PKx1ziLOPnIRRO84EXaAP/CWnR3pArTWcybpA5G8NsX20pw+cSbpAngEeOQenY+Cf8KIZ4FuDfSV4Ko/7hS7wNjYH7W3MvNeHtn2jvxn+OXcgaP0x8KJo43vgnwqu85EXDfGVULWON9G1BOmDN/M/AnTgDSWC0xve0KAITeSsykFw4qzOQWB4YwNBOfLmPAHpe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+aA8OZAAAAAAG+Vvf46sAAAAAAAAAAAAAAAAAAAAAAFYCeHSjWah9hFcAAAAASUVORK5CYII=";
if(body.data.discord_user.avatar !== null && body.data.discord_user.avatar.startsWith("a_")) avatarExtension = "gif";
if(params.animated === "false") avatarExtension = "webp";
if(params.hideDiscrim === "true") discrim = "hide";
if(params.theme === 'light'){
backgroundColor = '#eee';
theme = 'light';
}
if(params.bg) backgroundColor = params.bg;
if(params.hideStatus === 'true') hideStatus = 'true';
switch(body.data.discord_status){
case "online":
avatarBorderColor = "#43B581";
break;
case "idle":
avatarBorderColor = "#FAA61A";
break;
case "dnd":
avatarBorderColor = "#F04747";
break;
case "offline":
avatarBorderColor = "#747F8D";
break;
}
let flags: string[] = getFlags(body.data.discord_user.public_flags);
if(body.data.activities[0] && body.data.activities[0].type === 4) userStatus = body.data.activities[0].state;
console.log(Object.keys(body.data.activities).length);
return `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xhtml="http://www.w3.org/1999/xhtml" width="410px" height="218px">
<foreignObject x="0" y="0" width="410" height="218">
<div xmlns="http://www.w3.org/1999/xhtml" style="
position: absolute;
width: 400px;
height: 200px;
inset: 0;
background-color: #${backgroundColor};
color: ${theme === 'dark' ? '#fff' : '#000'};
font-family: 'Century Gothic', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
font-size: 16px;
display: flex;
flex-direction: column;
padding: 5px;
border-radius: 10px;
">
<div style="
width: 400px;
height: 100px;
inset: 0;
display: flex;
flex-direction: row;
padding-bottom: 5px;
border-bottom: solid 0.5px ${theme === 'dark' ? 'hsl(0, 0%, 100%, 10%)' : 'hsl(0, 0%, 0%, 10%)'};
">
<div style="
display: flex;
flex-direction: row;
height: 80px;
width: 80px;
">
<img ${body.data.discord_user.avatar !== null ?
`src="data:image/png;base64,${await encodeBase64(`https://cdn.discordapp.com/avatars/${body.data.discord_user.id}/${body.data.discord_user.avatar}.${avatarExtension}?size=256`)}"` :
`src="data:image/png;base64,${defaultAvatar}"`}
style="
border: solid 3px ${avatarBorderColor};
border-radius: 50%;
width: 50px;
height: 50px;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
"/>
</div>
<div style="
height: 80px;
width: 260px;
">
<div style="
display: flex;
flex-direction: row;
position: relative;
top: ${userStatus.length > 0 && hideStatus !== "true" ? "35%" : "50%"};
transform: translate(0, -50%);
height: 25px;
">
<h1 style="
font-size: 1.15rem;
margin: 0 5px 0 0;
">
${body.data.discord_user.username}${discrim !== 'hide' ?
`<span style="color: ${theme === 'dark' ? '#ccc' : '#666'}; font-weight: lighter;">#${body.data.discord_user.discriminator}</span>`
: ''}
</h1>
${
flags.map((v) => {
return(
`<img src="data:image/png;base64,${Badges[v]}" style="
width: 20px;
height: 20px;
position: relative;
top: 50%;
transform: translate(0%, -50%);
margin: 0 0 0 4px;
" />`
)
}).join('')
}
</div>
${userStatus.length > 0 && hideStatus !== "true" ? `
<h1 style="
font-size: 0.9rem;
margin-top: 16px;
color: ${theme === 'dark' ? '#aaa' : '#333'};
font-weight: lighter;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
">
${body.data.activities[0].emoji && body.data.activities[0].emoji.id ?
`<img src="data:image/png;base64,${await encodeBase64(`https://cdn.discordapp.com/emojis/${body.data.activities[0].emoji.id}.${statusExtension}`)}" style="
width: 15px;
height: 15px;
position: relative;
top: 10px;
transform: translate(0%, -50%);
margin: 0 2px 0 0;
" />`
: ``}
${body.data.activities[0].emoji && !body.data.activities[0].emoji.id ?
body.data.activities[0].emoji.name + ' ' + userStatus
:
userStatus
}
</h1>`
: ``}
</div>
</div>
${activity ?
`
<div style="
display: flex;
flex-direction: row;
height: 120px;
margin-left: 15px;
font-size: 0.75rem;
padding-top: 18px;
">
<div style="
margin-right: 15px;
width: auto;
height: auto;
">
${activity.assets && activity.assets.large_image ?
`
<img src="data:image/png;base64,${await encodeBase64(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.webp`)}" style="
width: 80px;
height: 80px;
border: solid 0.5px #222;
border-radius: 10px;
"/>
`
: `
<img src="data:image/png;base64,${await encodeBase64(`https://lanyard-profile-readme.vercel.app/assets/unknown.png`)}" style="
width: 70px;
height: 70px;
margin-top: 4px;
filter: invert(100);
"/>
`}
${activity.assets && activity.assets.small_image ?
`<img src="data:image/png;base64,${await encodeBase64(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.small_image}.webp`)}" style="
width: 30px;
height: 30px;
border-radius: 50%;
margin-left: -26px;
margin-bottom: -8px;
"/>`
: ``}
</div>
<div style="
color: #999;
margin-top: -3px;
line-height: 1;
width: 279px;
">
<p style="font-size: 0.7rem; color: ${theme === 'dark' ? '#7289DA' : '#334da6'}; margin-bottom: 15px;">PLAYING A GAME...</p>
<p style="
color: ${theme === 'dark' ? '#fff' : '#000'}; \
font-weight: bold;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
height: 15px;
margin: 7px 0;
">${activity.name}</p>
${activity.details ?
`<p style="
color: ${theme === 'dark' ? '#ccc' : '#777'};
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
height: 15px;
margin: 7px 0;
">${activity.details}</p>`
: ``}
</div>
</div>
`
: ``}
${body.data.listening_to_spotify === true && body.data.activities[Object.keys(body.data.activities).length - 1].type === 2 ?
`
<div style="
display: flex;
flex-direction: row;
height: 120px;
margin-left: 15px;
font-size: 0.8rem;
padding-top: 18px;
">
<img src="data:image/png;base64,${await encodeBase64(body.data.spotify.album_art_url)}" style="
width: 80px;
height: 80px;
border: solid 0.5px #222;
border-radius: 10px;
margin-right: 15px;
"/>
<div style="
color: #999;
margin-top: -3px;
line-height: 1;
width: 279px;
">
<p style="font-size: 0.75rem; color: ${theme === 'dark' ? '#1CB853' : '#0d943d'}; margin-bottom: 15px;">LISTENING NOW...</p>
<p style="
height: 15px;
color: ${theme === 'dark' ? '#fff' : '#000'};
font-weight: bold;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
margin: 7px 0;
">${body.data.spotify.song.replace(/\&/g, "and")}</p>
<p style="
margin: 7px 0;
height: 15px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
color: ${theme === 'dark' ? '#ccc' : '#777'};
">By ${body.data.spotify.artist.replace(/\;/g, ",").replace(/\&/g, "and")}</p>
</div>
</div>
`
: ``}
${!activity && body.data.listening_to_spotify === false ?
`<div style="
display: flex;
flex-direction: row;
height: 150px;
justify-content: center;
align-items: center;
">
<p style="
font-style: italic;
font-size: 0.8rem;
color: ${theme === 'dark' ? '#aaa' : '#444'};
height: auto;
">
I'm not currently doing anything!
</p>
</div>`
: ``}
</div>
</foreignObject>
</svg>
`;
}
export default renderCard;