From b2b1f5c09b83007eec0fdcde0a7768bfe0feaf7f Mon Sep 17 00:00:00 2001 From: Mestima Date: Fri, 31 Mar 2023 00:48:56 +0300 Subject: [PATCH] cat & fox api request timeout set 1s, answer and log on bad request --- src/api/cat/index.js | 5 ++++- src/api/fox/index.js | 5 ++++- src/commands/cat/index.js | 8 +++++++- src/commands/fox/index.js | 8 +++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/api/cat/index.js b/src/api/cat/index.js index e061456..aa2f097 100644 --- a/src/api/cat/index.js +++ b/src/api/cat/index.js @@ -1,4 +1,7 @@ import axios from 'axios'; +import chalk from 'chalk'; + +axios.defaults.timeout = 1000; const cat = async () => { return await axios.get('https://aws.random.cat/meow') @@ -6,7 +9,7 @@ const cat = async () => { return res.data.file; }) .catch((e) => { - console.log(e); + console.log(`${chalk.red('Error:')} the Cat API is unavailable`); }); }; diff --git a/src/api/fox/index.js b/src/api/fox/index.js index 0ab7e29..656a00c 100644 --- a/src/api/fox/index.js +++ b/src/api/fox/index.js @@ -1,4 +1,7 @@ import axios from 'axios'; +import chalk from 'chalk'; + +axios.defaults.timeout = 1000; const fox = async () => { return await axios.get('https://randomfox.ca/floof/') @@ -6,7 +9,7 @@ const fox = async () => { return res.data.image; }) .catch((e) => { - console.log(e); + console.log(`${chalk.red('Error:')} the Fox API is unavailable`); }); }; diff --git a/src/commands/cat/index.js b/src/commands/cat/index.js index 9354b5e..d54e0b2 100644 --- a/src/commands/cat/index.js +++ b/src/commands/cat/index.js @@ -1,4 +1,4 @@ -import { SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; export default { data: new SlashCommandBuilder() @@ -8,6 +8,12 @@ export default { await api.get('cat')() .then(async (res) => { await interaction.reply(res); + }) + .catch(async (e) => { + const embed = new EmbedBuilder() + .setTitle('Sorry, the Cat service is unavailable right now') + .setColor(0xFF0000); + await interaction.reply({ embeds: [ embed ] }); }); } }; diff --git a/src/commands/fox/index.js b/src/commands/fox/index.js index 2248a79..48e7c2b 100644 --- a/src/commands/fox/index.js +++ b/src/commands/fox/index.js @@ -1,4 +1,4 @@ -import { SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; export default { data: new SlashCommandBuilder() @@ -8,6 +8,12 @@ export default { await api.get('fox')() .then(async (res) => { await interaction.reply(res); + }) + .catch(async (e) => { + const embed = new EmbedBuilder() + .setTitle('Sorry, the Fox service is unavailable right now') + .setColor(0xFF0000); + await interaction.reply({ embeds: [ embed ] }); }); } };