Skip to content

Commit

Permalink
Make sure pkm image folder exists
Browse files Browse the repository at this point in the history
Create it if it doesn't exist
  • Loading branch information
Glazelf committed Jan 5, 2025
1 parent 32e5eda commit 89ffb29
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion util/pokemon/getWhosThatPokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ export default async ({ interaction, winner, pokemonList, pokemon, reveal }) =>
messageObject.components = [];
} else {
// Initiate image context. If "socket hang up" error occurs, error seems to be in this block of code.
let localImagePath = `./assets/pkm/${pokemonID}.png`;
let localImageFolder = "./assets/pkm/";
let localImagePath = `${localImageFolder}${pokemonID}.png`;
let localImageExists = fs.existsSync(localImagePath);
let img = new Canvas.Image();
// Fetch image from Serebii if it doesn't exist locally
if (localImageExists) {
img.src = localImagePath;
} else {
let imageBuffer = await axios.get(serebiiRender, { responseType: 'arraybuffer' }).then(response => response.data);
fs.mkdirSync(localImageFolder, { recursive: true });
fs.appendFileSync(localImagePath, Buffer.from(imageBuffer));
img.src = imageBuffer;
};
Expand Down

0 comments on commit 89ffb29

Please sign in to comment.