diff --git a/commands/misc/tdoss.js b/commands/misc/tdoss.js index 09b5447..8385638 100644 --- a/commands/misc/tdoss.js +++ b/commands/misc/tdoss.js @@ -46,12 +46,12 @@ module.exports = { // TODO: Download with correct extension. message.channel.sendTyping(); let downloadResult = await this.downloadImage(url, path.resolve(directory, "input.png")); - if(downloadResult.value != 0){ - if(downloadResult.value === 3){ + if(downloadResult.value != this.ERROR_CODES.SUCCESS){ + if(downloadResult.value === this.ERROR_CODES.FETCH_ERROR){ message.channel.send(`Failed to download the provided image, got error '${downloadResult.errorMessage}'`); - }else if (downloadResult.value === 1){ + }else if (downloadResult.value === this.ERROR_CODES.HTTP_ERROR){ message.channel.send(`Failed to download the provided image, got response status '${downloadResult.errorMessage}'`); - }else if(downloadResult.value === 2){ + }else if(downloadResult.value === this.ERROR_CODES.NOT_IMAGE){ message.channel.send(`The provided url was not an image.`) } fs.rmSync(`${directory}`, {recursive: true}) @@ -90,7 +90,7 @@ module.exports = { }, // https://stackoverflow.com/a/77210219 async downloadImage(url, path) { - let res = new Response() + let res; try { res = await fetch(url); } catch (error) { @@ -102,6 +102,12 @@ module.exports = { const stream = Readable.fromWeb(res.body) await writeFile(path, stream); return {value: 0, errorMessage: ""}; + }, + ERROR_CODES: { + SUCCESS: 0, + HTTP_ERROR: 1, + NOT_IMAGE: 2, + FETCH_ERROR: 3 } }