Search code examples
javascriptdiscord.jsgiphy-api

Giphy not working on Discord bot (javascript)


I was following this tutorial on how to make a discord bot, everything was working fine until 33:32 where he added the giphy stuff i had already installed giphy sdk/api, created an application, but after he made the search statement he said you can console log it so i did it, and there were some gif results coming out, which returned undefined on my console(i dunno why), then he added some math stuff, which i also did, then at the point where he added the messaging part where he also added this code files:[responseFinal.images.fixed_height.url] which then returned this on my console

(node:3136) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'images' of undefined
    at D:\Discord bots\Oboto v2.0\index.js:24:61
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:3136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

and this confused the flip outta me, then i picked an alt approach, instead of doing giphy.search i did giphy.random with the same arguments, removed the math stuff and console.log(response)the response and guess what it actually gave me a single gif!(in the console of course) then i implemented to my files:[]statement aaaaand it returned the same thing (cannot read property 'images' of undefined) im also kinda new to discord.js and javascript, also here is my entire code,

const Discord = require('discord.js');
const { prefix, token, giphyToken } = require('./config.json');
const client = new Discord.Client();

var GphApiClient = require('giphy-js-sdk-core')
giphy = GphApiClient(giphyToken)

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {
    if (message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMBERS"])){

        if (message.content.startsWith(`${prefix}kick`)) {

            let member = message.mentions.members.first();
            member.kick().then((member) =>{

                giphy.random('gifs', {'q':'fail'})  
                    .then((response) => {

                        console.log(response);
                        message.channel.send(":wave:",{files:[response.images.fixed_height.url]});

                })


            })
        }   
    }
})

client.login(token);

Solution

  • cannot read property 'images' of undefined, this means you are trying to access a null object. Same as null pointer exception in java. It means your response is null.

    And you are also getting UnhandledPromiseRejectionWarning which means your promise is throwing error which you are not catching anywhere. You can catch your error like this

    member.kick().then((member) =>{
        giphy.random('gifs', {'q':'fail'})  
        .then((response) => {
            console.log(response);
            message.channel.send(":wave:",{files:[response.images.fixed_height.url]});
        }).catch(e => { console.error(e}) }
    }).catch(e => { console.error(e) }
    

    Now you can see what error you are getting. You can also use try catch approach with async await.