Discord bot fails to retrieve GIFs from Giphy API - goes straight to error handler

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?

Found the bug in your random index calculation. This line: Math.floor((Math.random() * 10) + 1) % totalResults is broken, especially when totalResults is under 10. Adding 1 can push your index out of bounds before the modulo kicks in. Just use Math.floor(Math.random() * totalResults) instead. You’ll always get a valid index from 0 to totalResults-1. Also, that empty catch block isn’t helping you debug. Throw in console.error(error) so you can see what’s actually failing. With Giphy’s API, it’s usually auth problems or rate limiting that cause silent crashes.

I’ve encountered this issue before as well. The root of the problem often lies in the setup of the giphyAPI object. Your code seems properly structured, but it’s possible that the SDK is not initialized or the authentication is missing.

First, ensure you have it installed using npm install giphy-api. Then, at the top of your file, initialize it with const giphy = require('giphy-api')('YOUR_API_KEY_HERE');. Make sure to use giphy in your search call instead of giphyAPI.

Also, I recommend temporarily removing that catch block or modifying it to include console.error(error); this way, you can identify the specific error that is occurring. Lastly, double-check the permissions for your API key, as Giphy has usage limits that may lead to silent failures when exceeded.