From fa234d2d788e44a49490f5fadd3aea93b95da306 Mon Sep 17 00:00:00 2001 From: orangecoding Date: Sat, 27 Sep 2025 14:24:05 +0200 Subject: [PATCH] fixing code style issues in new discord adapter --- lib/notification/adapter/discord_webhook.js | 35 ++++++++++----------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/notification/adapter/discord_webhook.js b/lib/notification/adapter/discord_webhook.js index c62cc7d..dc561c5 100644 --- a/lib/notification/adapter/discord_webhook.js +++ b/lib/notification/adapter/discord_webhook.js @@ -11,18 +11,18 @@ import { normalizeImageUrl } from '../../utils.js'; * @returns {number} Generated decimal color code (0 - 16777215) */ const generateColorFromString = (str) => { - let hash = 5381; // initial value + let hash = 5381; // initial value const input = String(str); for (let i = 0; i < input.length; i++) { // hash * 33 + charCode - hash = ((hash << 5) + hash) + input.charCodeAt(i); + hash = (hash << 5) + hash + input.charCodeAt(i); // Ensure the hash is 32 bit - hash |= 0; + hash |= 0; } - let positiveHash = hash >>> 0; - const maxColorValue = 16777215; + let positiveHash = hash >>> 0; + const maxColorValue = 16777215; const colorDecimal = positiveHash % maxColorValue; return colorDecimal; @@ -37,10 +37,10 @@ const generateColorFromString = (str) => { * @returns {object} Discord webhook embed */ const buildEmbed = (jobKey, listing) => { - const maxTitleLength = 252; // Max embed title length is 256 characters + const maxTitleLength = 252; // Max embed title length is 256 characters let title = String(listing.title ?? 'N/A'); if (title.length > maxTitleLength) { - title = title.substring(0, maxTitleLength) + '...'; + title = title.substring(0, maxTitleLength) + '...'; } const fields = [ @@ -59,14 +59,14 @@ const buildEmbed = (jobKey, listing) => { value: String(listing.address ?? 'n/a'), inline: true, }, - ] + ]; const embed = { title: title, color: generateColorFromString(jobKey), url: listing.link, fields: fields, - } + }; if (listing.image) { embed.image = { @@ -74,38 +74,37 @@ const buildEmbed = (jobKey, listing) => { }; } - return embed + return embed; }; export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => { const adapter = notificationConfig.find((adapter) => adapter.id === config.id); const webhookUrl = adapter?.fields?.webhookUrl; - if (!webhookUrl || newListings.length == 0) return Promise.resolve([]); + if (!webhookUrl || newListings.length === 0) return Promise.resolve([]); const job = getJob(jobKey); const jobName = job?.name || jobKey; const embeds = newListings.map((listing) => buildEmbed(jobKey, listing)); - const maxEmbedsPerMessage = 10; // Discord only allows up to 10 embeds + const maxEmbedsPerMessage = 10; // Discord only allows up to 10 embeds const webhookPromises = []; for (let i = 0; i < embeds.length; i += maxEmbedsPerMessage) { // Send multiple Discord messages with up to 10 embeds per message const embedChunk = embeds.slice(i, i + maxEmbedsPerMessage); - + const content = i === 0 ? `*${jobName}:* ${serviceName} found **${newListings.length}** new listings.` : ''; const body = JSON.stringify({ - content: content, - embeds: embedChunk, - }) + content: content, + embeds: embedChunk, + }); const fetchPromise = fetch(webhookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body, - }) - .catch(error => { + }).catch((error) => { console.error(`Error sending Discord webhook for chunk starting at ${i}:`, error); return Promise.reject(new Error(`Webhook failed: ${error.message}`)); });