Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
Discord.js Tenor API error [Displaying image from Embed]

I'm using the Tenor API to display a random gif in an embed in Discord.js. I did everything correctly, however, the image is not loading.

Here is my code:

require('dotenv').config();
const Discord = require('discord.js');
const fetch = require('node-fetch')
module.exports = {
    name: 'tenor',
    category: 'misc',
    aliases: ['gif', 't'],
    async execute(message) {
        const url = `https://g.tenor.com/v1/search?q=Anime_Kiss&key=${process.env.TENOR}&limit=20`
        let response = await fetch(url)
        let mom = await response.json();
        const makerandom = Math.floor(Math.random() * mom.results.length);
        let embed = new Discord.MessageEmbed()
        .setImage(mom.results[makerandom].itemurl)
        message.channel.send({ embeds: [embed]})
    }
};

Here's what comes up:

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's because itemurl is not a direct link to the file, but the full URL to view the post on tenor.com. You should use the media key instead. It's an array of dictionaries with the format (like gif, mp4, webm, etc.) as the key and a media object (with keys like size, duration, and url) as the value. It means you can get the URL of the gif image by using results[<random index>].media[0].gif.url.

I've updated your code, it works as expected now:

require('dotenv').config();
const Discord = require('discord.js');
const fetch = require('node-fetch');
module.exports = {
  name: 'tenor',
  category: 'misc',
  aliases: ['gif', 't'],
  async execute(message) {
      let url = `https://g.tenor.com/v1/search?q=Anime_Kiss&key=${process.env.TENOR}&limit=20`;
      try {
        let response = await fetch(url);
        let { results } = await response.json();
        let randomResult = results[Math.floor(Math.random() * results.length)];
        let { gif } = randomResult.media[0];
        let embed = new Discord.MessageEmbed().setImage(gif.url);

        message.channel.send({ embeds: [embed] });
      } catch (err) {
        console.log(err);
        message.channel.send('Oops, there was an error');
      }
  },
};

enter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!