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

100
discordWebhook.js Normal file
View File

@@ -0,0 +1,100 @@
const https = require("https");
const WEBHOOK_URL = "https://discord.com/api/webhooks/1456729066924277912/zf0-cSqmU18kX-S5UZJHNwsuqKtM2i7Bp8LMCyXM80n1RsDtFQCTccBIzafghf7q-U4q";
// send notfication to discord
function sendDiscordNotification(content, additionalData = {}) {
if (!WEBHOOK_URL) {
console.warn("Discord webhook URL not configured");
return Promise.resolve();
}
const payload = {
content: content,
...additionalData
};
const data = JSON.stringify(payload);
const url = new URL(WEBHOOK_URL);
const options = {
hostname: url.hostname,
path: url.pathname + url.search,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let responseData = "";
res.on("data", (chunk) => {
responseData += chunk;
});
res.on("end", () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(responseData);
} else {
console.error(`Discord webhook failed: ${res.statusCode} - ${responseData}`);
resolve(); // dont reject, just log the error
}
});
});
req.on("error", (error) => {
console.error("Discord webhook error:", error.message);
resolve(); // dont reject to avoid breaking the main flow
});
req.write(data);
req.end();
});
}
// notify about new image generation
function notifyImageGenerated(config, ipAddress) {
const username = config.username || "@unknown";
const displayName = config.displayName || "Unknown User";
let content = `🖼️ **New Image Generated**\n`;
content += `User: ${displayName} (${username})\n`;
if (config.text) {
const shortText = config.text.length > 100
? config.text.substring(0, 100) + "..."
: config.text;
content += `Text: ${shortText}\n`;
}
if (ipAddress) {
content += `IP: ${ipAddress}\n`;
}
return sendDiscordNotification(content);
}
// notify about new snapshot creation
function notifySnapshotCreated(sessionId, token, ipAddress) {
let content = `📸 **New Snapshot Created**\n`;
content += `Session ID: ${sessionId}\n`;
content += `Token: ${token}\n`;
content += `Link: \`/v2/snapshot/${token}\`\n`;
if (ipAddress) {
content += `IP: ${ipAddress}`;
}
return sendDiscordNotification(content);
}
module.exports = {
sendDiscordNotification,
notifyImageGenerated,
notifySnapshotCreated
};