Reputation: 81
So I was trying to make a command that sends random GIF in the chat, but it kept giving me errors. WHat should I do?
I am using Discord.js to code it, and the GIF is supported by Giphy API.
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("message", async message => {
if (message.content.startsWith(`${prefix}gif`)) {
fetch(
"http://api.giphy.com/v1/gifs/random?api_key=PRIVATE"
).then(body => {
var body = JSON.parse(body)
message.channel.send({
embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "**GIF Machine**",
description: "Here's your GIF!",
fields: [],
timestamp: new Date(),
image: {
files: [body.data.image_original_url]
},
footer: {
text: "Made with ❤️ created by Raymond#1725"
}
}
});
})
}
}
It gives out the error:
(node:6732) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
Upvotes: 0
Views: 1616
Reputation: 936
try this:
client.on("message", async message => {
if (message.content.startsWith(`${prefix}gif`)) {
fetch(
"http://api.giphy.com/v1/gifs/random?api_key=PRIVATE"
)
.then(res=>res.json()) // changed
.then(body => { // changed
message.channel.send({
embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "**GIF Machine**",
description: "Here's your GIF!",
fields: [],
timestamp: new Date(),
image: {
files: [body.data.image_original_url]
},
footer: {
text: "Made with ❤️ created by Raymond#1725"
}
}
});
})
}
}
Upvotes: 2