Add Discord notifications for image generation and snapshot creation

This commit is contained in:
ImBenji
2026-01-02 19:46:42 +00:00
parent e935513ba1
commit 18c7304327
3 changed files with 140 additions and 0 deletions

View File

@@ -5,10 +5,20 @@ const crypto = require('crypto');
const router = express.Router();
const { createSession, getSession, updateSession, deleteSession, createSnapshot, getSnapshot, touchSnapshot } = require('./db');
const { renderHtml } = require('./browserPool');
const { notifyImageGenerated, notifySnapshotCreated } = require('./discordWebhook');
const CACHE_DIR = path.join(__dirname, 'cache');
// get client ip address
function getClientIp(req) {
const forwarded = req.headers['x-forwarded-for'];
if (forwarded) {
return forwarded.split(',')[0].trim();
}
return req.ip || req.socket.remoteAddress || 'unknown';
}
// caching functions
function normalizeConfig(config) {
const normalized = {};
@@ -326,6 +336,10 @@ router.get('/quote/:id/image', async (req, res) => {
image = await generateQuoteBuffer(config);
cacheImage(config, image);
fromCache = false;
// notify discord about new image
const clientIp = getClientIp(req);
notifyImageGenerated(config, clientIp).catch(err => console.error('Discord notification failed:', err));
}
res.setHeader('Content-Type', 'image/png');
@@ -359,6 +373,10 @@ router.post('/quote/:id/snapshot-link', (req, res) => {
const config = buildConfigFromSession(session);
const snapshot = createSnapshot(session.id, config);
// notify discord about new snapshot
const clientIp = getClientIp(req);
notifySnapshotCreated(session.id, snapshot.token, clientIp).catch(err => console.error('Discord notification failed:', err));
const baseUrl = `${req.protocol}://${req.get('host')}`;
res.status(201).json({
token: snapshot.token,
@@ -392,6 +410,10 @@ router.get('/snapshot/:token', async (req, res) => {
image = await generateQuoteBuffer(config);
cacheImage(config, image);
fromCache = false;
// notify discord about new image
const clientIp = getClientIp(req);
notifyImageGenerated(config, clientIp).catch(err => console.error('Discord notification failed:', err));
}
res.setHeader('Content-Type', 'image/png');