mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
fixing code style issues in new discord adapter
This commit is contained in:
@@ -11,18 +11,18 @@ import { normalizeImageUrl } from '../../utils.js';
|
|||||||
* @returns {number} Generated decimal color code (0 - 16777215)
|
* @returns {number} Generated decimal color code (0 - 16777215)
|
||||||
*/
|
*/
|
||||||
const generateColorFromString = (str) => {
|
const generateColorFromString = (str) => {
|
||||||
let hash = 5381; // initial value
|
let hash = 5381; // initial value
|
||||||
const input = String(str);
|
const input = String(str);
|
||||||
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
for (let i = 0; i < input.length; i++) {
|
||||||
// hash * 33 + charCode
|
// hash * 33 + charCode
|
||||||
hash = ((hash << 5) + hash) + input.charCodeAt(i);
|
hash = (hash << 5) + hash + input.charCodeAt(i);
|
||||||
// Ensure the hash is 32 bit
|
// Ensure the hash is 32 bit
|
||||||
hash |= 0;
|
hash |= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let positiveHash = hash >>> 0;
|
let positiveHash = hash >>> 0;
|
||||||
const maxColorValue = 16777215;
|
const maxColorValue = 16777215;
|
||||||
const colorDecimal = positiveHash % maxColorValue;
|
const colorDecimal = positiveHash % maxColorValue;
|
||||||
|
|
||||||
return colorDecimal;
|
return colorDecimal;
|
||||||
@@ -37,10 +37,10 @@ const generateColorFromString = (str) => {
|
|||||||
* @returns {object} Discord webhook embed
|
* @returns {object} Discord webhook embed
|
||||||
*/
|
*/
|
||||||
const buildEmbed = (jobKey, listing) => {
|
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');
|
let title = String(listing.title ?? 'N/A');
|
||||||
if (title.length > maxTitleLength) {
|
if (title.length > maxTitleLength) {
|
||||||
title = title.substring(0, maxTitleLength) + '...';
|
title = title.substring(0, maxTitleLength) + '...';
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = [
|
const fields = [
|
||||||
@@ -59,14 +59,14 @@ const buildEmbed = (jobKey, listing) => {
|
|||||||
value: String(listing.address ?? 'n/a'),
|
value: String(listing.address ?? 'n/a'),
|
||||||
inline: true,
|
inline: true,
|
||||||
},
|
},
|
||||||
]
|
];
|
||||||
|
|
||||||
const embed = {
|
const embed = {
|
||||||
title: title,
|
title: title,
|
||||||
color: generateColorFromString(jobKey),
|
color: generateColorFromString(jobKey),
|
||||||
url: listing.link,
|
url: listing.link,
|
||||||
fields: fields,
|
fields: fields,
|
||||||
}
|
};
|
||||||
|
|
||||||
if (listing.image) {
|
if (listing.image) {
|
||||||
embed.image = {
|
embed.image = {
|
||||||
@@ -74,38 +74,37 @@ const buildEmbed = (jobKey, listing) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return embed
|
return embed;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
||||||
const adapter = notificationConfig.find((adapter) => adapter.id === config.id);
|
const adapter = notificationConfig.find((adapter) => adapter.id === config.id);
|
||||||
const webhookUrl = adapter?.fields?.webhookUrl;
|
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 job = getJob(jobKey);
|
||||||
const jobName = job?.name || jobKey;
|
const jobName = job?.name || jobKey;
|
||||||
|
|
||||||
const embeds = newListings.map((listing) => buildEmbed(jobKey, listing));
|
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 = [];
|
const webhookPromises = [];
|
||||||
|
|
||||||
for (let i = 0; i < embeds.length; i += maxEmbedsPerMessage) {
|
for (let i = 0; i < embeds.length; i += maxEmbedsPerMessage) {
|
||||||
// Send multiple Discord messages with up to 10 embeds per message
|
// Send multiple Discord messages with up to 10 embeds per message
|
||||||
const embedChunk = embeds.slice(i, i + maxEmbedsPerMessage);
|
const embedChunk = embeds.slice(i, i + maxEmbedsPerMessage);
|
||||||
|
|
||||||
const content = i === 0 ? `*${jobName}:* ${serviceName} found **${newListings.length}** new listings.` : '';
|
const content = i === 0 ? `*${jobName}:* ${serviceName} found **${newListings.length}** new listings.` : '';
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
content: content,
|
content: content,
|
||||||
embeds: embedChunk,
|
embeds: embedChunk,
|
||||||
})
|
});
|
||||||
|
|
||||||
const fetchPromise = fetch(webhookUrl, {
|
const fetchPromise = fetch(webhookUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body,
|
body,
|
||||||
})
|
}).catch((error) => {
|
||||||
.catch(error => {
|
|
||||||
console.error(`Error sending Discord webhook for chunk starting at ${i}:`, error);
|
console.error(`Error sending Discord webhook for chunk starting at ${i}:`, error);
|
||||||
return Promise.reject(new Error(`Webhook failed: ${error.message}`));
|
return Promise.reject(new Error(`Webhook failed: ${error.message}`));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user