I’m working on a Discord bot using discord.js and trying to integrate Giphy API to send random GIFs. However, my code isn’t working as expected and keeps jumping to the catch block without displaying any GIFs.
if (userCommand === 'countdown') {
var targetDate = new Date("dec 25, 2024 00:00:00").getTime();
var now = new Date().getTime();
var timeLeft = targetDate - now;
var daysLeft = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hoursLeft = Math.floor((timeLeft %(1000 * 60 * 60 * 24))/(1000 * 60 * 60));
var minutesLeft = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var secondsLeft = Math.floor((timeLeft % (1000 * 60)) / 1000);
giphyAPI.search('gifs', {"q": "celebration"})
.then((result) => {
console.log(result);
var totalResults = result.data.length;
var randomIndex = Math.floor((Math.random() * 10) + 1) % totalResults;
var selectedGif = result.data[randomIndex];
msg.channel.send('🎉 **C H R I S T M A S C O U N T D O W N** 🎉');
msg.channel.send(`**${daysLeft}** days, **${hoursLeft}** hrs, **${minutesLeft}** mins, **${secondsLeft}** secs`, {
files: [selectedGif.images.fixed_height.url]})
})
.catch (() => {
console.log('Something went wrong with the API call...')
})
}
The issue is that whenever I run this command, it skips the then block entirely and goes straight to the error handler. I’ve tried adding console logs to debug the process but nothing shows up in the console. What could be causing this problem with the Giphy integration?