From 7528d17db71a2420ad583863208ca8724028c725 Mon Sep 17 00:00:00 2001 From: KwikKill Date: Thu, 5 Sep 2024 10:32:23 +0200 Subject: [PATCH] Catch champion gathering error --- util/lol_api.js | 28 ++++++++++++++++------------ util/lol_functions.js | 37 ++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/util/lol_api.js b/util/lol_api.js index 6a191b5..87dc3de 100644 --- a/util/lol_api.js +++ b/util/lol_api.js @@ -357,22 +357,26 @@ class LolApi { * @returns {Object} champion list */ async championList() { + try { + // Get New Champion List from Data Dragon + const championList = await axios.get("https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json"); - // Get New Champion List from Data Dragon - const championList = await axios.get("https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json"); - - // Champions Array - const champions = []; + // Champions Array + const champions = []; - // Add Champion Names & IDs to the array - for (const champion in championList["data"]) { - if (championList["data"][champion]['id'] === -1) { - continue; + // Add Champion Names & IDs to the array + for (const champion in championList["data"]) { + if (championList["data"][champion]['id'] === -1) { + continue; + } + champions[championList["data"][champion]['id']] = championList["data"][champion]['name']; } - champions[championList["data"][champion]['id']] = championList["data"][champion]['name']; - } - return champions; + return champions; + } catch (error) { + logger.error(error); + return []; + } } /** diff --git a/util/lol_functions.js b/util/lol_functions.js index ac96472..a0f822c 100644 --- a/util/lol_functions.js +++ b/util/lol_functions.js @@ -70,6 +70,9 @@ module.exports = { this.client = client; this.champions = []; const list = await this.lol_api.championList(); + if (list.length === 0) { + throw new Error("Error while fetching champion list"); + } this.champions = list; this.client.champions = []; for (let i = 0; i < list.length; i++) { @@ -100,10 +103,10 @@ module.exports = { const imageHeight = imageMetadata.height; const zoomedImage = await sharp(imageBuffer) .extract({ - left: imageWidth / 2 - imageWidth/10, - top: imageHeight / 2 - imageWidth/10, - width: imageWidth/5, - height: imageWidth/5 + left: imageWidth / 2 - imageWidth / 10, + top: imageHeight / 2 - imageWidth / 10, + width: imageWidth / 5, + height: imageWidth / 5 }) .toBuffer(); @@ -297,7 +300,7 @@ module.exports = { * @param {*} number summoner in client.requests["updates"] */ async update_pseudo(number, gamename, tagline) { - if(gamename === undefined || tagline === undefined) { + if (gamename === undefined || tagline === undefined) { return; } const puuid = number["puuid"]; @@ -1026,20 +1029,24 @@ module.exports = { async main(debug = false) { // Start by updating the champion list const list = await this.lol_api.championList(); - this.champions = list; - const temp_champion = []; - for (let i = 0; i < list.length; i++) { - if (list[i] !== undefined) { - temp_champion.push(list[i]); + if (list.length === 0) { + logger.error("Error while fetching champions list, fallback to previous list"); + } else { + this.champions = list; + const temp_champion = []; + for (let i = 0; i < list.length; i++) { + if (list[i] !== undefined) { + temp_champion.push(list[i]); + } } - } - temp_champion.sort(); + temp_champion.sort(); - this.client.champions = temp_champion; + this.client.champions = temp_champion; + } // Fail if the champions list is empty if (this.champions.length === 0) { - logger.error("Error while fetching champions list"); + logger.error("Champions list is empty, cancelling update"); return; } @@ -1079,7 +1086,7 @@ module.exports = { if (debug) { logger.log("lol (summoner)[" + route + "] : total took " + (checkpoint1 - start) + " ms"); } - while ( this.services[route]["queue"]["updates"].length > 0) { + while (this.services[route]["queue"]["updates"].length > 0) { const timer1 = Date.now(); if (this.services[route]["queue"]["summoners"].length > 0) { this.services[route]["running"] = false;