Spaces:
Sleeping
Sleeping
File size: 3,699 Bytes
b12287d 94eed18 b12287d 94eed18 b12287d 94eed18 b12287d 94eed18 b12287d 94eed18 b12287d | 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 | import { Canvas, loadImage } from 'skia-canvas'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { downloadBinary } from '../storage/hfClient.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const DEFAULT_BACKGROUNDS = {
default1: path.join(__dirname, '../assets/backgrounds/default1.png'),
default2: path.join(__dirname, '../assets/backgrounds/default2.png'),
default3: path.join(__dirname, '../assets/backgrounds/default3.png'),
}
const WIDTH = 800
const HEIGHT = 200
const ACCENT_COLOR = '#ed4245'
// Pre-load all default backgrounds once at module load time
const defaultBackgroundCache = new Map()
await Promise.all(
Object.entries(DEFAULT_BACKGROUNDS).map(async ([key, filePath]) => {
try {
const image = await loadImage(filePath)
defaultBackgroundCache.set(key, image)
console.log(`[card] Pre-loaded background: ${key}`)
} catch (err) {
console.error(`[card] Failed to pre-load background "${key}":`, err.message)
}
})
)
export async function generateLeaveCard(member, config) {
const canvas = new Canvas(WIDTH, HEIGHT)
const ctx = canvas.getContext('2d')
try {
// STEP 1 β Load background
let bgImage
try {
const bgKey = config.leaveBackground
if (bgKey === 'custom') {
const buffer = await downloadBinary(`guilds/${config.guildId}/assets/leave_bg.png`)
if (buffer) {
bgImage = await loadImage(buffer)
} else {
bgImage = defaultBackgroundCache.get('default1')
}
} else if (defaultBackgroundCache.has(bgKey)) {
bgImage = defaultBackgroundCache.get(bgKey)
} else {
bgImage = defaultBackgroundCache.get('default1')
}
} catch (err) {
console.error('[card] Background load failed, using default:', err.message)
bgImage = defaultBackgroundCache.get('default1')
}
ctx.drawImage(bgImage, 0, 0, WIDTH, HEIGHT)
// STEP 2 β Dark overlay (slightly darker than welcome)
ctx.fillStyle = 'rgba(0, 0, 0, 0.55)'
ctx.fillRect(0, 0, WIDTH, HEIGHT)
// STEP 3 β Accent bar (Discord red, hardcoded)
ctx.fillStyle = ACCENT_COLOR
ctx.fillRect(0, 0, 6, HEIGHT)
// STEP 4 β Avatar
const avatarUrl = member.user.displayAvatarURL({ extension: 'png', size: 128 })
const avatarImage = await loadImage(avatarUrl)
const centerX = 110
const centerY = 100
const radius = 70
ctx.save()
ctx.beginPath()
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
ctx.clip()
ctx.drawImage(avatarImage, centerX - radius, centerY - radius, radius * 2, radius * 2)
ctx.restore()
ctx.beginPath()
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
ctx.strokeStyle = ACCENT_COLOR
ctx.lineWidth = 4
ctx.stroke()
// STEP 5 β Text
// Guild name label (replaces "WELCOME TO" label)
ctx.font = 'bold 15px DejaVu Sans'
ctx.fillStyle = ACCENT_COLOR
ctx.fillText(member.guild.name.toUpperCase(), 200, 70)
// Username
ctx.font = 'bold 32px DejaVu Sans'
ctx.fillStyle = config.cardTextColor
ctx.fillText(member.user.username, 200, 115)
// Member count line
ctx.font = '16px DejaVu Sans'
ctx.fillStyle = 'rgba(255,255,255,0.7)'
ctx.fillText('We now have ' + member.guild.memberCount + ' members', 200, 148)
// STEP 6 β Return buffer
const buffer = await canvas.toBuffer('png')
return buffer
} catch (err) {
console.error('[card] generateLeaveCard failed:', err.message)
throw err
}
} |