118 lines
3.3 KiB
JavaScript
118 lines
3.3 KiB
JavaScript
const https = require("https");
|
|
|
|
|
|
const WEBHOOK_URL = "https://discord.com/api/webhooks/1456729066924277912/zf0-cSqmU18kX-S5UZJHNwsuqKtM2i7Bp8LMCyXM80n1RsDtFQCTccBIzafghf7q-U4q";
|
|
|
|
// sanitze text for discord
|
|
function sanitizeText(text) {
|
|
if (!text) return "";
|
|
|
|
// remove or replace problematic characters
|
|
return text
|
|
.replace(/[\u0000-\u001F\u007F-\u009F]/g, "") // remove control chars
|
|
.substring(0, 1800); // limit length to avoid discord limits
|
|
}
|
|
|
|
// 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
|
|
};
|
|
|
|
let data;
|
|
try {
|
|
data = JSON.stringify(payload);
|
|
} catch (err) {
|
|
console.error("Failed to stringify webhook payload:", err);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
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 = sanitizeText(config.username) || "@unknown";
|
|
const displayName = sanitizeText(config.displayName) || "Unknown User";
|
|
|
|
let content = `🖼️ **New Image Generated**\n`;
|
|
content += `User: ${displayName} (${username})\n`;
|
|
|
|
if (config.text) {
|
|
const sanitized = sanitizeText(config.text);
|
|
const shortText = sanitized.length > 100
|
|
? sanitized.substring(0, 100) + "..."
|
|
: sanitized;
|
|
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
|
|
};
|