162 lines
4.1 KiB
JavaScript
162 lines
4.1 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": Buffer.byteLength(data)
|
|
}
|
|
};
|
|
|
|
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";
|
|
|
|
const fields = [
|
|
{
|
|
name: "User",
|
|
value: `${displayName} (${username})`,
|
|
inline: false
|
|
}
|
|
];
|
|
|
|
if (config.text) {
|
|
const sanitized = sanitizeText(config.text);
|
|
const shortText = sanitized.length > 1000
|
|
? sanitized.substring(0, 1000) + "..."
|
|
: sanitized;
|
|
fields.push({
|
|
name: "Text",
|
|
value: shortText || "No text",
|
|
inline: false
|
|
});
|
|
}
|
|
|
|
if (ipAddress) {
|
|
fields.push({
|
|
name: "IP Address",
|
|
value: ipAddress,
|
|
inline: true
|
|
});
|
|
}
|
|
|
|
const embed = {
|
|
title: "🖼️ New Image Generated",
|
|
color: 0x5865F2, // blurple
|
|
fields: fields,
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
return sendDiscordNotification("", { embeds: [embed] });
|
|
}
|
|
|
|
// notify about new snapshot creation
|
|
function notifySnapshotCreated(sessionId, token, ipAddress) {
|
|
const fields = [
|
|
{
|
|
name: "Session ID",
|
|
value: sessionId,
|
|
inline: false
|
|
},
|
|
{
|
|
name: "Token",
|
|
value: `\`${token}\``,
|
|
inline: false
|
|
},
|
|
{
|
|
name: "Link",
|
|
value: `/v2/snapshot/${token}`,
|
|
inline: false
|
|
}
|
|
];
|
|
|
|
if (ipAddress) {
|
|
fields.push({
|
|
name: "IP Address",
|
|
value: ipAddress,
|
|
inline: true
|
|
});
|
|
}
|
|
|
|
const embed = {
|
|
title: "📸 New Snapshot Created",
|
|
color: 0x57F287, // green
|
|
fields: fields,
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
return sendDiscordNotification("", { embeds: [embed] });
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
sendDiscordNotification,
|
|
notifyImageGenerated,
|
|
notifySnapshotCreated
|
|
};
|