name: Update dist.css and Avatars on: push: branches: - main paths: - ".github/workflows/generate-db.yml" - ".github/scripts/generate/**/*.*" - "db/template.css" - "db/datadesktop.json" # Add the path to your JSON file here pull_request: branches: - main workflow_dispatch: jobs: generate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: 18.x - name: Setup PNPM working-directory: .github/scripts run: | npm i -g pnpm pnpm i - name: Copy data.json to datadesktop.json run: | cp db/data.json db/datadesktop.json - name: Update dist.css and Avatars id: update-dist-css run: | # Define the paths to your JSON and CSS files JSON_FILE="db/datadesktop.json" CSS_FILE="db/dist.css" # Read the JSON data into a variable JSON_DATA=$(cat "$JSON_FILE") # Initialize an empty CSS content variable for badges BADGES_CSS_CONTENT="" # Initialize an empty CSS content variable for avatars (pfps) AVATARS_CSS_CONTENT="" # Loop through each badge entry in the JSON data for DISCORD_ID in $(echo "$JSON_DATA" | jq -r '.badges | keys[]'); do BADGE_URL=$(echo "$JSON_DATA" | jq -r ".badges[\"$DISCORD_ID\"]") # Create CSS snippet for the badge BADGES_CSS_SNIPPET=".userProfileInner-1ngKnf:has(.avatar-31d8He[src^=\"https://cdn.discordapp.com/avatars/$DISCORD_ID\"]) > .topSection-13QKHs > header > .header-S26rhB > .headerTop-1PNKck > .container-1gYwHN:before, .userPopoutInner-nv9Y92:has(.avatar-31d8He[src^=\"https://cdn.discordapp.com/avatars/$DISCORD_ID\"]) > .container-1gYwHN:before, .userInfo-regn9W:has([src^=\"https://cdn.discordapp.com/avatars/$DISCORD_ID\"]) > .wrapper-3Un6-K + div > .container-1gYwHN:before, .userPopoutInner-nv9Y92:has(.imageUploaderInner-IIRaFr[style*=\"$DISCORD_ID\"]) > .container-1gYwHN:before { content: \"\"; width: 22px; height: 22px; background: url($BADGE_URL) center / 100% 100%; }" # Append the badge CSS snippet to the badge CSS content BADGES_CSS_CONTENT="${BADGES_CSS_CONTENT}\n\n$BADGES_CSS_SNIPPET" done # Loop through each avatar (pfp) entry in the JSON data for DISCORD_ID in $(echo "$JSON_DATA" | jq -r '.avatars | keys[]'); do AVATAR_URL=$(echo "$JSON_DATA" | jq -r ".avatars[\"$DISCORD_ID\"]") # Create CSS snippet for the avatar (pfp) AVATARS_CSS_SNIPPET="[style^=\"https://cdn.discordapp.com/avatars/$DISCORD_ID\"],[src^=\"https://cdn.discordapp.com/avatars/$DISCORD_ID\"],.userAvatar-3Hwf1F:is([style*=\"$DISCORD_ID\"]) {content: url($AVATAR_URL); background-image: url($AVATAR_URL) !important;}" # Append the avatar (pfp) CSS snippet to the avatar (pfp) CSS content AVATARS_CSS_CONTENT="${AVATARS_CSS_CONTENT}\n\n$AVATARS_CSS_SNIPPET" done # Use awk to replace the existing badge and avatar (pfp) sections in the CSS file awk -v badges_content="$BADGES_CSS_CONTENT" -v avatars_content="$AVATARS_CSS_CONTENT" '/\/\* Discord Badges \*\// {print; print badges_content; f=1; next} /\/\* End of Discord Badges \*\// {if (f) f=0; else print} !f' "$CSS_FILE" > tmpfile && mv tmpfile "$CSS_FILE" awk -v avatars_content="$AVATARS_CSS_CONTENT" '/\/\* Discord Avatars \*\// {print; print avatars_content; f=1; next} /\/\* End of Discord Avatars \*\// {if (f) f=0; else print} !f' "$CSS_FILE" > tmpfile && mv tmpfile "$CSS_FILE" - name: Push changes continue-on-error: ${{ success() }} run: | git config --global user.name "github-actions[bot]" git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" # Commit and push the updated dist.css and datadesktop.json files git add db/dist.css db/datadesktop.json git commit -m "chore(db): update dist.css and datadesktop.json (${{ github.sha || 'manual trigger' }})" || true git push