cat & fox api request timeout set 1s, answer and log on bad request

This commit is contained in:
Mestima 2023-03-31 00:48:56 +03:00
parent 4e27150999
commit b2b1f5c09b
4 changed files with 22 additions and 4 deletions

View File

@ -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`);
});
};

View File

@ -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`);
});
};

View File

@ -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 ] });
});
}
};

View File

@ -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 ] });
});
}
};