Spaces:
Sleeping
Sleeping
Upload 19 files
Browse files- src/assets/backgrounds/default1.png +0 -0
- src/assets/backgrounds/default2.png +0 -0
- src/assets/backgrounds/default3.png +0 -0
- src/bot.js +56 -93
- src/canvas/leaveCard.js +99 -0
- src/canvas/welcomeCard.js +98 -0
- src/events/guildMemberAdd.js +11 -3
src/assets/backgrounds/default1.png
ADDED
|
src/assets/backgrounds/default2.png
ADDED
|
src/assets/backgrounds/default3.png
ADDED
|
src/bot.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import { Client, GatewayIntentBits, Partials
|
| 2 |
import { Agent } from 'undici'
|
| 3 |
import { readdirSync } from 'node:fs'
|
| 4 |
import { resolve, dirname } from 'node:path'
|
|
@@ -6,23 +6,14 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
| 6 |
|
| 7 |
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 8 |
|
| 9 |
-
const RACE_CONCURRENCY
|
| 10 |
-
const MAX_RETRIES
|
| 11 |
-
const
|
| 12 |
-
const
|
| 13 |
-
const ATTEMPT_TIMEOUT = 90_000 // per-slot timeout (ms) β long enough for slow HF network
|
| 14 |
-
|
| 15 |
-
function sleep(ms) {
|
| 16 |
-
return new Promise((r) => setTimeout(r, ms))
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
// Exponential backoff with jitter so all slots don't slam at once
|
| 20 |
-
function backoff(round) {
|
| 21 |
-
const exp = Math.min(BASE_RETRY_DELAY * 2 ** (round - 1), MAX_RETRY_DELAY)
|
| 22 |
-
const jitter = Math.random() * 1000
|
| 23 |
-
return exp + jitter
|
| 24 |
-
}
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
function createClient() {
|
| 27 |
return new Client({
|
| 28 |
intents: [
|
|
@@ -32,50 +23,31 @@ function createClient() {
|
|
| 32 |
GatewayIntentBits.MessageContent,
|
| 33 |
],
|
| 34 |
partials: [Partials.GuildMember],
|
| 35 |
-
|
| 36 |
-
// Override the REST HTTP agent β helps with the initial token auth request
|
| 37 |
rest: {
|
| 38 |
-
agent: new Agent({
|
| 39 |
-
connect: {
|
| 40 |
-
timeout: ATTEMPT_TIMEOUT,
|
| 41 |
-
rejectUnauthorized: false,
|
| 42 |
-
minVersion: 'TLSv1.2',
|
| 43 |
-
keepAlive: true,
|
| 44 |
-
},
|
| 45 |
-
}),
|
| 46 |
-
},
|
| 47 |
-
|
| 48 |
-
// WebSocket options β this is where the gateway connection lives
|
| 49 |
-
ws: {
|
| 50 |
-
large_threshold: 50,
|
| 51 |
},
|
| 52 |
})
|
| 53 |
}
|
| 54 |
|
| 55 |
// Single connection attempt β resolves with the ready client or rejects on failure
|
| 56 |
-
function attemptLogin(token,
|
| 57 |
return new Promise((resolve, reject) => {
|
| 58 |
const c = createClient()
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
c.destroy()
|
| 62 |
-
reject(new Error(`${
|
| 63 |
-
},
|
| 64 |
|
| 65 |
c.once('clientReady', () => {
|
| 66 |
-
clearTimeout(
|
| 67 |
resolve(c)
|
| 68 |
})
|
| 69 |
|
| 70 |
-
// Catch gateway-level errors before clientReady fires
|
| 71 |
-
c.ws.once('error', (err) => {
|
| 72 |
-
clearTimeout(timer)
|
| 73 |
-
c.destroy()
|
| 74 |
-
reject(err)
|
| 75 |
-
})
|
| 76 |
-
|
| 77 |
c.login(token).catch((err) => {
|
| 78 |
-
clearTimeout(
|
| 79 |
c.destroy()
|
| 80 |
reject(err)
|
| 81 |
})
|
|
@@ -88,19 +60,22 @@ function raceConnections(token, round) {
|
|
| 88 |
let settled = false
|
| 89 |
let failures = 0
|
| 90 |
|
| 91 |
-
Array.from({ length: RACE_CONCURRENCY }, (_, i) =>
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
p.then((winner) => {
|
| 97 |
-
if (settled) {
|
|
|
|
|
|
|
|
|
|
| 98 |
settled = true
|
| 99 |
-
console.log(`[bot]
|
| 100 |
resolve(winner)
|
| 101 |
}).catch((err) => {
|
| 102 |
failures++
|
| 103 |
-
console.warn(`[bot]
|
| 104 |
if (!settled && failures === RACE_CONCURRENCY) {
|
| 105 |
reject(new Error(`All ${RACE_CONCURRENCY} attempts failed in round ${round}`))
|
| 106 |
}
|
|
@@ -109,23 +84,6 @@ function raceConnections(token, round) {
|
|
| 109 |
})
|
| 110 |
}
|
| 111 |
|
| 112 |
-
async function connectWithRetry(token) {
|
| 113 |
-
let round = 0
|
| 114 |
-
while (true) {
|
| 115 |
-
round++
|
| 116 |
-
const label = MAX_RETRIES === -1 ? 'β' : MAX_RETRIES
|
| 117 |
-
console.log(`[bot] Connecting... (round ${round}/${label})`)
|
| 118 |
-
|
| 119 |
-
try {
|
| 120 |
-
return await raceConnections(token, round)
|
| 121 |
-
} catch (err) {
|
| 122 |
-
const delay = backoff(round)
|
| 123 |
-
console.error(`[bot] Round ${round} failed β retrying in ${(delay / 1000).toFixed(1)}s`)
|
| 124 |
-
await sleep(delay)
|
| 125 |
-
}
|
| 126 |
-
}
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
// Exported client reference β populated once connected
|
| 130 |
export let client = null
|
| 131 |
|
|
@@ -133,36 +91,41 @@ export async function startBot() {
|
|
| 133 |
const token = process.env.DISCORD_TOKEN
|
| 134 |
if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
|
| 135 |
|
| 136 |
-
// Load
|
| 137 |
const eventsDir = resolve(__dirname, 'events')
|
| 138 |
const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
|
| 139 |
const events = await Promise.all(
|
| 140 |
eventFiles.map((file) => import(pathToFileURL(resolve(eventsDir, file)).href))
|
| 141 |
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
-
}
|
| 151 |
-
}
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
registerEvents(newClient)
|
| 165 |
-
client = newClient
|
| 166 |
}
|
| 167 |
-
}
|
| 168 |
}
|
|
|
|
| 1 |
+
import { Client, GatewayIntentBits, Partials } from 'discord.js'
|
| 2 |
import { Agent } from 'undici'
|
| 3 |
import { readdirSync } from 'node:fs'
|
| 4 |
import { resolve, dirname } from 'node:path'
|
|
|
|
| 6 |
|
| 7 |
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 8 |
|
| 9 |
+
const RACE_CONCURRENCY = 5 // parallel attempts per round
|
| 10 |
+
const MAX_RETRIES = -1 // rounds before giving up (-1 = infinite)
|
| 11 |
+
const RETRY_DELAY_MS = 5_000 // wait between rounds (ms)
|
| 12 |
+
const CONNECT_TIMEOUT = 60_000 // undici connect timeout per attempt (ms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
// Passing a custom undici Agent via the `rest` option is the only way to
|
| 15 |
+
// override discord.js's hardcoded 10 s connect timeout β env vars and
|
| 16 |
+
// top-level Client options are ignored by undici entirely.
|
| 17 |
function createClient() {
|
| 18 |
return new Client({
|
| 19 |
intents: [
|
|
|
|
| 23 |
GatewayIntentBits.MessageContent,
|
| 24 |
],
|
| 25 |
partials: [Partials.GuildMember],
|
|
|
|
|
|
|
| 26 |
rest: {
|
| 27 |
+
agent: new Agent({ connect: { timeout: CONNECT_TIMEOUT } }),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
},
|
| 29 |
})
|
| 30 |
}
|
| 31 |
|
| 32 |
// Single connection attempt β resolves with the ready client or rejects on failure
|
| 33 |
+
function attemptLogin(token, attemptNum) {
|
| 34 |
return new Promise((resolve, reject) => {
|
| 35 |
const c = createClient()
|
| 36 |
|
| 37 |
+
// Our own outer guard β slightly longer than CONNECT_TIMEOUT so the
|
| 38 |
+
// undici error surfaces first with a meaningful message when possible.
|
| 39 |
+
const timeout = setTimeout(() => {
|
| 40 |
c.destroy()
|
| 41 |
+
reject(new Error(`Attempt ${attemptNum} timed out`))
|
| 42 |
+
}, CONNECT_TIMEOUT + 5_000)
|
| 43 |
|
| 44 |
c.once('clientReady', () => {
|
| 45 |
+
clearTimeout(timeout)
|
| 46 |
resolve(c)
|
| 47 |
})
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
c.login(token).catch((err) => {
|
| 50 |
+
clearTimeout(timeout)
|
| 51 |
c.destroy()
|
| 52 |
reject(err)
|
| 53 |
})
|
|
|
|
| 60 |
let settled = false
|
| 61 |
let failures = 0
|
| 62 |
|
| 63 |
+
const attempts = Array.from({ length: RACE_CONCURRENCY }, (_, i) =>
|
| 64 |
+
attemptLogin(token, `round=${round} slot=${i + 1}`)
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
attempts.forEach((p, i) => {
|
| 68 |
p.then((winner) => {
|
| 69 |
+
if (settled) {
|
| 70 |
+
winner.destroy()
|
| 71 |
+
return
|
| 72 |
+
}
|
| 73 |
settled = true
|
| 74 |
+
console.log(`[bot] Connection established (round=${round} slot=${i + 1})`)
|
| 75 |
resolve(winner)
|
| 76 |
}).catch((err) => {
|
| 77 |
failures++
|
| 78 |
+
console.warn(`[bot] Attempt failed (round=${round} slot=${i + 1}): ${err.message}`)
|
| 79 |
if (!settled && failures === RACE_CONCURRENCY) {
|
| 80 |
reject(new Error(`All ${RACE_CONCURRENCY} attempts failed in round ${round}`))
|
| 81 |
}
|
|
|
|
| 84 |
})
|
| 85 |
}
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
// Exported client reference β populated once connected
|
| 88 |
export let client = null
|
| 89 |
|
|
|
|
| 91 |
const token = process.env.DISCORD_TOKEN
|
| 92 |
if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
|
| 93 |
|
| 94 |
+
// Load events once β registered on whichever client wins the race
|
| 95 |
const eventsDir = resolve(__dirname, 'events')
|
| 96 |
const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
|
| 97 |
const events = await Promise.all(
|
| 98 |
eventFiles.map((file) => import(pathToFileURL(resolve(eventsDir, file)).href))
|
| 99 |
)
|
| 100 |
|
| 101 |
+
let round = 0
|
| 102 |
+
|
| 103 |
+
while (MAX_RETRIES === -1 || round < MAX_RETRIES) {
|
| 104 |
+
round++
|
| 105 |
+
console.log(`[bot] Starting Discord client... (round ${round}/${MAX_RETRIES === -1 ? 'β' : MAX_RETRIES})`)
|
| 106 |
+
|
| 107 |
+
try {
|
| 108 |
+
const winner = await raceConnections(token, round)
|
| 109 |
+
|
| 110 |
+
for (const event of events) {
|
| 111 |
+
if (event.once) {
|
| 112 |
+
winner.once(event.name, (...args) => event.execute(...args))
|
| 113 |
+
} else {
|
| 114 |
+
winner.on(event.name, (...args) => event.execute(...args))
|
| 115 |
+
}
|
| 116 |
}
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
client = winner
|
| 119 |
+
return
|
| 120 |
+
} catch (err) {
|
| 121 |
+
console.error(`[bot] Round ${round} failed: ${err.message}`)
|
| 122 |
+
|
| 123 |
+
if (MAX_RETRIES !== -1 && round >= MAX_RETRIES) {
|
| 124 |
+
throw new Error(`[bot] Giving up after ${MAX_RETRIES} rounds`)
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
console.log(`[bot] Retrying in ${RETRY_DELAY_MS / 1000}s...`)
|
| 128 |
+
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS))
|
|
|
|
|
|
|
| 129 |
}
|
| 130 |
+
}
|
| 131 |
}
|
src/canvas/leaveCard.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Canvas, loadImage } from 'skia-canvas'
|
| 2 |
+
import { fileURLToPath } from 'node:url'
|
| 3 |
+
import path from 'node:path'
|
| 4 |
+
import { downloadBinary } from '../storage/hfClient.js'
|
| 5 |
+
|
| 6 |
+
const __filename = fileURLToPath(import.meta.url)
|
| 7 |
+
const __dirname = path.dirname(__filename)
|
| 8 |
+
|
| 9 |
+
const DEFAULT_BACKGROUNDS = {
|
| 10 |
+
default1: path.join(__dirname, '../assets/backgrounds/default1.png'),
|
| 11 |
+
default2: path.join(__dirname, '../assets/backgrounds/default2.png'),
|
| 12 |
+
default3: path.join(__dirname, '../assets/backgrounds/default3.png'),
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const WIDTH = 800
|
| 16 |
+
const HEIGHT = 200
|
| 17 |
+
const ACCENT_COLOR = '#ed4245'
|
| 18 |
+
|
| 19 |
+
export async function generateLeaveCard(member, config) {
|
| 20 |
+
const canvas = new Canvas(WIDTH, HEIGHT)
|
| 21 |
+
const ctx = canvas.getContext('2d')
|
| 22 |
+
|
| 23 |
+
try {
|
| 24 |
+
// STEP 1 β Load background
|
| 25 |
+
let bgImage
|
| 26 |
+
try {
|
| 27 |
+
const bgKey = config.leaveBackground
|
| 28 |
+
if (bgKey === 'custom') {
|
| 29 |
+
const buffer = await downloadBinary(`guilds/${config.guildId}/assets/leave_bg.png`)
|
| 30 |
+
if (buffer) {
|
| 31 |
+
bgImage = await loadImage(buffer)
|
| 32 |
+
} else {
|
| 33 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 34 |
+
}
|
| 35 |
+
} else if (DEFAULT_BACKGROUNDS[bgKey]) {
|
| 36 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS[bgKey])
|
| 37 |
+
} else {
|
| 38 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 39 |
+
}
|
| 40 |
+
} catch (err) {
|
| 41 |
+
console.error('[card] Background load failed, using default:', err.message)
|
| 42 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
ctx.drawImage(bgImage, 0, 0, WIDTH, HEIGHT)
|
| 46 |
+
|
| 47 |
+
// STEP 2 β Dark overlay (slightly darker than welcome)
|
| 48 |
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.55)'
|
| 49 |
+
ctx.fillRect(0, 0, WIDTH, HEIGHT)
|
| 50 |
+
|
| 51 |
+
// STEP 3 β Accent bar (Discord red, hardcoded)
|
| 52 |
+
ctx.fillStyle = ACCENT_COLOR
|
| 53 |
+
ctx.fillRect(0, 0, 6, HEIGHT)
|
| 54 |
+
|
| 55 |
+
// STEP 4 β Avatar
|
| 56 |
+
const avatarUrl = member.user.displayAvatarURL({ extension: 'png', size: 128 })
|
| 57 |
+
const avatarImage = await loadImage(avatarUrl)
|
| 58 |
+
|
| 59 |
+
const centerX = 110
|
| 60 |
+
const centerY = 100
|
| 61 |
+
const radius = 70
|
| 62 |
+
|
| 63 |
+
ctx.save()
|
| 64 |
+
ctx.beginPath()
|
| 65 |
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
|
| 66 |
+
ctx.clip()
|
| 67 |
+
ctx.drawImage(avatarImage, centerX - radius, centerY - radius, radius * 2, radius * 2)
|
| 68 |
+
ctx.restore()
|
| 69 |
+
|
| 70 |
+
ctx.beginPath()
|
| 71 |
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
|
| 72 |
+
ctx.strokeStyle = ACCENT_COLOR
|
| 73 |
+
ctx.lineWidth = 4
|
| 74 |
+
ctx.stroke()
|
| 75 |
+
|
| 76 |
+
// STEP 5 β Text
|
| 77 |
+
// Guild name label (replaces "WELCOME TO" label)
|
| 78 |
+
ctx.font = 'bold 15px DejaVu Sans'
|
| 79 |
+
ctx.fillStyle = ACCENT_COLOR
|
| 80 |
+
ctx.fillText(member.guild.name.toUpperCase(), 200, 70)
|
| 81 |
+
|
| 82 |
+
// Username
|
| 83 |
+
ctx.font = 'bold 32px DejaVu Sans'
|
| 84 |
+
ctx.fillStyle = config.cardTextColor
|
| 85 |
+
ctx.fillText(member.user.username, 200, 115)
|
| 86 |
+
|
| 87 |
+
// Member count line
|
| 88 |
+
ctx.font = '16px DejaVu Sans'
|
| 89 |
+
ctx.fillStyle = 'rgba(255,255,255,0.7)'
|
| 90 |
+
ctx.fillText('We now have ' + member.guild.memberCount + ' members', 200, 148)
|
| 91 |
+
|
| 92 |
+
// STEP 6 β Return buffer
|
| 93 |
+
const buffer = await canvas.toBuffer('png')
|
| 94 |
+
return buffer
|
| 95 |
+
} catch (err) {
|
| 96 |
+
console.error('[card] generateLeaveCard failed:', err.message)
|
| 97 |
+
throw err
|
| 98 |
+
}
|
| 99 |
+
}
|
src/canvas/welcomeCard.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Canvas, loadImage } from 'skia-canvas'
|
| 2 |
+
import { fileURLToPath } from 'node:url'
|
| 3 |
+
import path from 'node:path'
|
| 4 |
+
import { downloadBinary } from '../storage/hfClient.js'
|
| 5 |
+
|
| 6 |
+
const __filename = fileURLToPath(import.meta.url)
|
| 7 |
+
const __dirname = path.dirname(__filename)
|
| 8 |
+
|
| 9 |
+
const DEFAULT_BACKGROUNDS = {
|
| 10 |
+
default1: path.join(__dirname, '../assets/backgrounds/default1.png'),
|
| 11 |
+
default2: path.join(__dirname, '../assets/backgrounds/default2.png'),
|
| 12 |
+
default3: path.join(__dirname, '../assets/backgrounds/default3.png'),
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const WIDTH = 800
|
| 16 |
+
const HEIGHT = 200
|
| 17 |
+
|
| 18 |
+
export async function generateWelcomeCard(member, config) {
|
| 19 |
+
const canvas = new Canvas(WIDTH, HEIGHT)
|
| 20 |
+
const ctx = canvas.getContext('2d')
|
| 21 |
+
|
| 22 |
+
try {
|
| 23 |
+
// STEP 1 β Load background
|
| 24 |
+
let bgImage
|
| 25 |
+
try {
|
| 26 |
+
const bgKey = config.welcomeBackground
|
| 27 |
+
if (bgKey === 'custom') {
|
| 28 |
+
const buffer = await downloadBinary(`guilds/${config.guildId}/assets/welcome_bg.png`)
|
| 29 |
+
if (buffer) {
|
| 30 |
+
bgImage = await loadImage(buffer)
|
| 31 |
+
} else {
|
| 32 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 33 |
+
}
|
| 34 |
+
} else if (DEFAULT_BACKGROUNDS[bgKey]) {
|
| 35 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS[bgKey])
|
| 36 |
+
} else {
|
| 37 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 38 |
+
}
|
| 39 |
+
} catch (err) {
|
| 40 |
+
console.error('[card] Background load failed, using default:', err.message)
|
| 41 |
+
bgImage = await loadImage(DEFAULT_BACKGROUNDS.default1)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
ctx.drawImage(bgImage, 0, 0, WIDTH, HEIGHT)
|
| 45 |
+
|
| 46 |
+
// STEP 2 β Dark overlay
|
| 47 |
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.45)'
|
| 48 |
+
ctx.fillRect(0, 0, WIDTH, HEIGHT)
|
| 49 |
+
|
| 50 |
+
// STEP 3 β Accent bar
|
| 51 |
+
ctx.fillStyle = config.cardAccentColor
|
| 52 |
+
ctx.fillRect(0, 0, 6, HEIGHT)
|
| 53 |
+
|
| 54 |
+
// STEP 4 β Avatar
|
| 55 |
+
const avatarUrl = member.user.displayAvatarURL({ extension: 'png', size: 128 })
|
| 56 |
+
const avatarImage = await loadImage(avatarUrl)
|
| 57 |
+
|
| 58 |
+
const centerX = 110
|
| 59 |
+
const centerY = 100
|
| 60 |
+
const radius = 70
|
| 61 |
+
|
| 62 |
+
ctx.save()
|
| 63 |
+
ctx.beginPath()
|
| 64 |
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
|
| 65 |
+
ctx.clip()
|
| 66 |
+
ctx.drawImage(avatarImage, centerX - radius, centerY - radius, radius * 2, radius * 2)
|
| 67 |
+
ctx.restore()
|
| 68 |
+
|
| 69 |
+
ctx.beginPath()
|
| 70 |
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
|
| 71 |
+
ctx.strokeStyle = config.cardAccentColor
|
| 72 |
+
ctx.lineWidth = 4
|
| 73 |
+
ctx.stroke()
|
| 74 |
+
|
| 75 |
+
// STEP 5 β Text
|
| 76 |
+
// Welcome label
|
| 77 |
+
ctx.font = 'bold 15px DejaVu Sans'
|
| 78 |
+
ctx.fillStyle = config.cardAccentColor
|
| 79 |
+
ctx.fillText('WELCOME TO ' + member.guild.name.toUpperCase(), 200, 70)
|
| 80 |
+
|
| 81 |
+
// Username
|
| 82 |
+
ctx.font = 'bold 32px DejaVu Sans'
|
| 83 |
+
ctx.fillStyle = config.cardTextColor
|
| 84 |
+
ctx.fillText(member.user.username, 200, 115)
|
| 85 |
+
|
| 86 |
+
// Member count
|
| 87 |
+
ctx.font = '16px DejaVu Sans'
|
| 88 |
+
ctx.fillStyle = 'rgba(255,255,255,0.7)'
|
| 89 |
+
ctx.fillText('Member #' + member.guild.memberCount, 200, 148)
|
| 90 |
+
|
| 91 |
+
// STEP 6 β Return buffer
|
| 92 |
+
const buffer = await canvas.toBuffer('png')
|
| 93 |
+
return buffer
|
| 94 |
+
} catch (err) {
|
| 95 |
+
console.error('[card] generateWelcomeCard failed:', err.message)
|
| 96 |
+
throw err
|
| 97 |
+
}
|
| 98 |
+
}
|
src/events/guildMemberAdd.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import {
|
| 2 |
loadGuildConfig,
|
| 3 |
getMemberData,
|
|
@@ -5,6 +6,7 @@ import {
|
|
| 5 |
setMemberData,
|
| 6 |
} from '../storage/cache.js'
|
| 7 |
import { parseTemplate } from '../utils/templateParser.js'
|
|
|
|
| 8 |
|
| 9 |
export const name = 'guildMemberAdd'
|
| 10 |
export const once = false
|
|
@@ -70,14 +72,20 @@ export async function execute(member) {
|
|
| 70 |
`[guildMemberAdd] Welcome channel ${config.welcomeChannelId} not found in guild ${guild.name}`
|
| 71 |
)
|
| 72 |
} else {
|
| 73 |
-
const
|
| 74 |
user: member.toString(),
|
| 75 |
server: guild.name,
|
| 76 |
count: guild.memberCount,
|
| 77 |
})
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
}
|
| 83 |
} catch (err) {
|
|
|
|
| 1 |
+
import { AttachmentBuilder } from 'discord.js'
|
| 2 |
import {
|
| 3 |
loadGuildConfig,
|
| 4 |
getMemberData,
|
|
|
|
| 6 |
setMemberData,
|
| 7 |
} from '../storage/cache.js'
|
| 8 |
import { parseTemplate } from '../utils/templateParser.js'
|
| 9 |
+
import { generateWelcomeCard } from '../canvas/welcomeCard.js'
|
| 10 |
|
| 11 |
export const name = 'guildMemberAdd'
|
| 12 |
export const once = false
|
|
|
|
| 72 |
`[guildMemberAdd] Welcome channel ${config.welcomeChannelId} not found in guild ${guild.name}`
|
| 73 |
)
|
| 74 |
} else {
|
| 75 |
+
const parsedWelcome = parseTemplate(config.welcomeMessage, {
|
| 76 |
user: member.toString(),
|
| 77 |
server: guild.name,
|
| 78 |
count: guild.memberCount,
|
| 79 |
})
|
| 80 |
|
| 81 |
+
const cardBuffer = await generateWelcomeCard(member, config)
|
| 82 |
+
const attachment = new AttachmentBuilder(cardBuffer, {
|
| 83 |
+
name: 'welcome.png',
|
| 84 |
+
})
|
| 85 |
+
await channel.send({
|
| 86 |
+
content: parsedWelcome,
|
| 87 |
+
files: [attachment],
|
| 88 |
+
})
|
| 89 |
}
|
| 90 |
}
|
| 91 |
} catch (err) {
|