Spaces:
Runtime error
Runtime error
File size: 4,603 Bytes
2344c18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | /**
* profileRenderer.js - Visual Profile Card Generator
* Uses @napi-rs/canvas to render a professional FIFA-style player card
*/
const { createCanvas, loadImage } = require('@napi-rs/canvas');
const { AttachmentBuilder } = require('discord.js');
const https = require('https');
const http = require('http');
/**
* Download an image from a URL and return as a Buffer
*/
function downloadImage(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
client.get(url, { timeout: 5000 }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return downloadImage(res.headers.location).then(resolve).catch(reject);
}
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => resolve(Buffer.concat(chunks)));
res.on('error', reject);
}).on('error', reject);
});
}
/**
* Create a Discord AttachmentBuilder from the rendered profile
*/
async function createProfileAttachment(user, stats) {
const W = 600;
const H = 350;
const canvas = createCanvas(W, H);
const ctx = canvas.getContext('2d');
// 1. Background Gradient
const grad = ctx.createLinearGradient(0, 0, W, H);
grad.addColorStop(0, '#0a0a1a');
grad.addColorStop(1, '#1a1a3a');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, W, H);
// 2. Abstract Shapes (Decorations)
ctx.fillStyle = 'rgba(88, 101, 242, 0.05)';
ctx.beginPath();
ctx.arc(W, 0, 200, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(0, H, 150, 0, Math.PI * 2);
ctx.fill();
// 3. Draw Card Border
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 8;
ctx.strokeRect(10, 10, W - 20, H - 20);
// 4. Draw User Avatar
const avatarSize = 140;
const avatarX = 50;
const avatarY = (H - avatarSize) / 2;
try {
const avatarUrl = user.displayAvatarURL({ extension: 'png', size: 256 });
const imgBuf = await downloadImage(avatarUrl);
const img = await loadImage(imgBuf);
// Circular clip
ctx.save();
ctx.beginPath();
ctx.arc(avatarX + avatarSize/2, avatarY + avatarSize/2, avatarSize/2, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(img, avatarX, avatarY, avatarSize, avatarSize);
ctx.restore();
// Glow ring
ctx.strokeStyle = '#5865F2';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.arc(avatarX + avatarSize/2, avatarY + avatarSize/2, avatarSize/2 + 4, 0, Math.PI * 2);
ctx.stroke();
} catch (e) {
ctx.fillStyle = '#5865F2';
ctx.beginPath();
ctx.arc(avatarX + avatarSize/2, avatarY + avatarSize/2, avatarSize/2, 0, Math.PI * 2);
ctx.fill();
}
// 5. Draw Username
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 32px Arial, sans-serif';
ctx.textAlign = 'left';
ctx.fillText(user.username.toUpperCase(), 220, 80);
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctx.font = '16px Arial, sans-serif';
ctx.fillText('PRO PLAYER', 220, 105);
// 6. Draw Stats Grid
const statsData = [
{ label: 'MATCHES', value: stats.matches || 0, icon: '🏟️' },
{ label: 'GOALS', value: stats.goals || 0, icon: '⚽' },
{ label: 'MVPs', value: stats.mvps || 0, icon: '⭐' }
];
let statX = 220;
let statY = 160;
statsData.forEach((s, i) => {
// Stat Box
ctx.fillStyle = 'rgba(255, 255, 255, 0.05)';
const boxW = 110;
const boxH = 90;
const currX = statX + (i * (boxW + 15));
// Draw rounded box (simple version)
ctx.fillRect(currX, statY, boxW, boxH);
// Value
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 30px Arial, sans-serif';
ctx.textAlign = 'center';
ctx.fillText(s.value, currX + boxW/2, statY + 45);
// Label
ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
ctx.font = 'bold 12px Arial, sans-serif';
ctx.fillText(s.label, currX + boxW/2, statY + 75);
});
// 7. Footer
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.font = '12px Arial, sans-serif';
ctx.textAlign = 'right';
ctx.fillText('FRIENDLYBOT NEXT-GEN GRAPHICS', W - 40, H - 40);
const buffer = canvas.toBuffer('image/png');
return new AttachmentBuilder(buffer, { name: 'profile.png' });
}
module.exports = { createProfileAttachment };
|