Spaces:
Sleeping
Sleeping
Download src/canvas/welcomeCard.js from Discord-Core-Bot/gateway-bot: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/spaces/Discord-Core-Bot/gateway-bot/resolve/main/src/canvas/welcomeCard.js
- Command line
-
hf download hf://spaces/Discord-Core-Bot/gateway-bot/src/canvas/welcomeCard.js
-
curl -L -o welcomeCard.js https://huggingface.co/spaces/Discord-Core-Bot/gateway-bot/resolve/main/src/canvas/welcomeCard.js
3.61 kB
| 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 | |
| // 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 generateWelcomeCard(member, config) { | |
| const canvas = new Canvas(WIDTH, HEIGHT) | |
| const ctx = canvas.getContext('2d') | |
| try { | |
| // STEP 1 β Load background | |
| let bgImage | |
| try { | |
| const bgKey = config.welcomeBackground | |
| if (bgKey === 'custom') { | |
| const buffer = await downloadBinary(`guilds/${config.guildId}/assets/welcome_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 | |
| ctx.fillStyle = 'rgba(0, 0, 0, 0.45)' | |
| ctx.fillRect(0, 0, WIDTH, HEIGHT) | |
| // STEP 3 β Accent bar | |
| ctx.fillStyle = config.cardAccentColor | |
| 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 = config.cardAccentColor | |
| ctx.lineWidth = 4 | |
| ctx.stroke() | |
| // STEP 5 β Text | |
| // Welcome label | |
| ctx.font = 'bold 15px DejaVu Sans' | |
| ctx.fillStyle = config.cardAccentColor | |
| ctx.fillText('WELCOME TO ' + 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 | |
| ctx.font = '16px DejaVu Sans' | |
| ctx.fillStyle = 'rgba(255,255,255,0.7)' | |
| ctx.fillText('Member #' + member.guild.memberCount, 200, 148) | |
| // STEP 6 β Return buffer | |
| const buffer = await canvas.toBuffer('png') | |
| return buffer | |
| } catch (err) { | |
| console.error('[card] generateWelcomeCard failed:', err.message) | |
| throw err | |
| } | |
| } |