Hello. I’m new to glitch and I’m having problems with some of my code. I want a randomized gif from giphy when a certain command is used, and I’ve got the code. The only problem is that I get the error:
TypeError: GphApiClient is not a function
It worked while I used it in VSCode, but not after I moved it to Glitch. My project’s name is weeabot1.
Any help is appreciated, as I’m completely new to Glitch.
Wecome to Glitch, @TheGameGamerInt !
According to what the error signifies, it seems that GphApiClient is not a function and you’re still trying to call that function, which would further throw a TypeError, as it did.
Solution: Do not call the GphApiClient as function.
Cheerio!
Then how would I get a gif from Giphy?
Can you please provide the code you were recently working on?
const GphApiClient = require('giphy-js-sdk-core')
var giphy = GphApiClient(giphyToken)
giphy.search('gifs', {"q": "punch, anime"})
.then((response) => {
var totalResponses = response.data.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
var responseFinal = response.data[responseIndex]
Which package are you working with to connect to the Giphy API?
I’m using giphy-js-sdk-core
So do you have any advice for that, or know someone who has any?
Is giphy-js-sdk-core
installed/added in the package.json
file? That could be the issue.
Yeah, but I think I fixed the problem now. Just had to change the code to
var giphy = GphApiClient + giphyToken
Hmm, according to the API docs on the NPM package page this is how you initialise a GphApiClient:
yeah, and when i did that i got an error
but now, whenever i try to boot up the bot, i get the error code (node:3927)UnhandledPromiseRejectionWarning error: an invalid token was provided
not sure if it’s connected or not, but it’s still a problem
1 Like
Looks like you’ve supplied invalid KEY to the client, which causes the error.
Ok, fixed. And why must a post be 20 char-long?
I am not quite sure about that, please ask the Giphy staff for the internal questions.
To avoid people posting ‘spam’ answers/replies. However half of them aren’t.
Well, I fixed it somehow. Now everything is working. May be a new post where I need help with some of the stuff I plan on implementing
1 Like
Linking a solution with example since this bug came up again!
The issue was as follows:
I was wrong! We could use Promises!
We needed to both move the code server-side (so that we could reference the process.env variables) and also instantiate the SDK like this:
const { apiClient } = require('giphy-js-sdk-core');
const giphy = new apiClient(process.env.GIPHY_KEY);
You can see a working version of this at jeweled-wormhole – credit to @khalby786 for the base code!
const giphy = require("giphy-api")();
client.on('message', async (message) => {
//gifs
if(message.content.includes("!gif")){
const args = message.content.slice().trim().split(/ +/g);
const command = args.shift().toLowerCase();
let converts = args.join(" ");
if(converts ==""){converts = "random"}
message.delete(1)
giphy.search({q: converts,rating: 'g'}, function (err, response) {
if(response.data.length == 0){
let embed2 = new Discord.RichEmbed()
.setColor(0x00ae86)
.setDescription(`🎬 Sorry I Can't find a gif related with your words.`)
message.channel.send({embed:embed2})
return;
}
var totalResponses = response.data.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
let embed = new Discord.RichEmbed()
.setColor(0x00ae86)
.setDescription(`🎬**Name:** "${response.data[responseIndex].title}".`)
.setImage(response.data[responseIndex].images.original.url)
message.channel.send({embed:embed}).then(embedMessage => {
embedMessage.react("👍")
embedMessage.react("👎")
.catch(() => console.error("One of the emojis failed to react."));
embedMessage.delete(15000)
})
})
}
})
TypeError: Cannot read property ‘length’ of undefined
@NathanCombrun , the latest version (v2.0.3) of the package is actually deprecated (Glitch prompts you to upgrade to the latest version), I suggest you downgrade the package to v1.0.6.