From 37b05d8e4e37104e623efed27cc0c505e370189c Mon Sep 17 00:00:00 2001 From: Burlone <108557545+burlone0@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:36:33 +0200 Subject: [PATCH] Adds images to itemshop Embed --- package-lock.json | 4 +-- package.json | 2 +- structs/autorotate.js | 58 +++++++++++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index 28690b4..25c3cae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "reloadbackend", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "reloadbackend", - "version": "1.0.4", + "version": "1.0.5", "license": "GPL-3.0", "dependencies": { "axios": "^1.7.7", diff --git a/package.json b/package.json index 466c43d..f3d7f44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reloadbackend", - "version": "1.0.4", + "version": "1.0.5", "description": "Created by Burlone, This is a modded backend, all main backend credits to lawin", "main": "index.js", "dependencies": { diff --git a/structs/autorotate.js b/structs/autorotate.js index 9eefaee..c4e2b3d 100644 --- a/structs/autorotate.js +++ b/structs/autorotate.js @@ -322,6 +322,21 @@ function updatecfgomg(dailyItems, featuredItems) { log.AutoRotation("The item shop has rotated!"); } +async function fetchItemIcon(itemName) { + try { + const response = await axios.get(`https://fortnite-api.com/v2/cosmetics/br/search?name=${encodeURIComponent(itemName)}`); + if (response.data && response.data.data && response.data.data.images && response.data.data.images.smallIcon) { + return response.data.data.images.smallIcon; + } else { + log.error(`No small icon found for ${itemName}`); + return null; + } + } catch (error) { + log.error(`Error fetching icon for ${itemName}:`, error.message || error); + return null; + } +} + async function discordpost(itemShop) { const embeds = []; @@ -329,17 +344,18 @@ async function discordpost(itemShop) { return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase(); } - function formatItemEmbed(item, authorTitle = null) { + async function formatItemEmbed(item, authorTitle = null) { const itemName = `**${capitalizeFirstLetter(item.name || "Unknown Item")}**`; const itemRarity = `Rarity: **${capitalizeFirstLetter(item.rarity?.displayValue || "Unknown")}**`; const itemPrice = `Price: **${notproperpricegen(item)} V-Bucks**`; + const itemIcon = await fetchItemIcon(item.name); const embed = { title: itemName, color: 0x00FF7F, description: `${itemRarity}\n${itemPrice}`, thumbnail: { - url: item.images?.icon || "https://i.imgur.com/2RImwlb.png" + url: itemIcon || 'https://via.placeholder.com/150' // prevents crash with placeholder images } }; @@ -365,27 +381,24 @@ async function discordpost(itemShop) { } embeds.push({ - title: "Reload Item Shop", - description: `These are the cosmetics for today!`, - color: 0x00FF7F, - thumbnail: { - url: "https://i.imgur.com/2RImwlb.png" - }, - fields: [], + title: "Reload Item Shop", + description: `These are the cosmetics for today!`, + color: 0x00FF7F, + fields: [], }); if (itemShop.featured.length > 0) { - itemShop.featured.forEach((item, index) => { - const embed = formatItemEmbed(item, index === 0 ? "Feature Item" : null); + for (const [index, item] of itemShop.featured.entries()) { + const embed = await formatItemEmbed(item, index === 0 ? "Feature Item" : null); embeds.push(embed); - }); + } } if (itemShop.daily.length > 0) { - itemShop.daily.forEach((item, index) => { - const embed = formatItemEmbed(item, index === 0 ? "Daily Item" : null); + for (const [index, item] of itemShop.daily.entries()) { + const embed = await formatItemEmbed(item, index === 0 ? "Daily Item" : null); embeds.push(embed); - }); + } } const nextRotationTimestamp = getNextRotationTime(); @@ -396,18 +409,27 @@ async function discordpost(itemShop) { try { if (config.bEnableDiscordWebhook === true) { - await axios.post(webhook, { embeds: embeds }); + const chunkSize = 10; + for (let i = 0; i < embeds.length; i += chunkSize) { + const embedChunk = embeds.slice(i, i + chunkSize); + const response = await axios.post(webhook, { embeds: embedChunk }); + log.AutoRotation(`Item shop posted successfully to Discord (chunk ${i / chunkSize + 1}):`, response.status); + } } } catch (error) { - log.error("Error sending item shop to Discord:", error.message || error); + log.error(`Error sending item shop to Discord: ${error.message}`); + if (error.response && error.response.data) { + log.error(`Discord API response: ${JSON.stringify(error.response.data, null, 2)}`); + } } } + async function rotateshop() { try { const cosmetics = await fetchitems(); if (cosmetics.length === 0) { - log.error('No cosmetics found?'); + log.error('No cosmetics found?'); // target was here! return; }