从 Tenor API 获取 GIFs

huangapple go评论62阅读模式
英文:

Fetching gifs from the Tenor API

问题

I'm having issues fetching any gifs from the Tenor API - I've also tried to use tenor.googleapis.com.

But so far, I've gotten this error over and over again:
Sorry, no media found in the search result

Here's the code I've worked with:

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'cat') {
    const api_key = 'API_KEY';
    const search_term = 'kitty';
    const limit = 50;
    const url = `https://tenor.googleapis.com/v2/search?q=${search_term}&key=${api_key}&limit=${limit}`;
    const command = `curl --request GET ${url}`;

    try {
      const response = await axios({
        method: 'GET',
        url: url,
        headers: {
          'User-Agent': 'PostmanRuntime/7.28.4'
        }
      });
      const data = response.data;
      if (!data.results || data.results.length === 0) {
        console.log('Sorry, no results found for kitty review');
        await interaction.reply('Sorry, no results found for kitty review');
        return;
      }
      const index = Math.floor(Math.random() * data.results.length);
      const result = data.results[index];
      const gif = result.gif && result.gif.url;
      if (!gif) {
        console.log('Sorry, no gif URL found in the search result');
        await interaction.reply('Sorry, no gif URL found in the search result');
        return;
      }
      console.log(`Found kitty review: ${gif}`);

      // send the gif in an embed
      const embed = new MessageEmbed()
        .setTitle('Here is your kitty review!')
        .setColor('#FFC0CB')
        .setImage(gif);

      await interaction.reply({ embeds: [embed] });
    } catch (error) {
      console.error(error);
      await interaction.reply('Sorry, an error occurred.');
    }
  }
});

I've edited the code so that it returns if no URL was found in the result, and the issue seems to be that it's not fetching the GIF URL: Sorry, no gif URL found in the search result.

英文:

I'm having issues fetching any gifs from the Tenor API - I've also tried to use tenor.googleapis.com.

But so far, I've gotten this error over and over again:
Sorry, no media found in the search result

Here's the code I've worked with:

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'cat') {
    const api_key = 'API_KEY';
    const search_term = 'kitty';
    const limit = 50;
    const url = `https://tenor.googleapis.com/v2/search?q=${search_term}&key=${api_key}&limit=${limit}`;
    const command = `curl --request GET ${url}`;

    try {
      const response = await axios({
        method: 'GET',
        url: url,
        headers: {
          'User-Agent': 'PostmanRuntime/7.28.4'
        }
      });
      const data = response.data;
      if (!data.results || data.results.length === 0) {
        console.log('Sorry, no results found for kitty review');
        await interaction.reply('Sorry, no results found for kitty review');
        return;
      }
      const index = Math.floor(Math.random() * data.results.length);
      const result = data.results[index];
      const gif = result.gif && result.gif.url;
      if (!gif) {
        console.log('Sorry, no gif URL found in the search result');
        await interaction.reply('Sorry, no gif URL found in the search result');
        return;
      }
      console.log(`Found kitty review: ${gif}`);

      // send the gif in an embed
      const embed = new MessageEmbed()
        .setTitle('Here is your kitty review!')
        .setColor('#FFC0CB')
        .setImage(gif);

      await interaction.reply({ embeds: [embed] });
    } catch (error) {
      console.error(error);
      await interaction.reply('Sorry, an error occurred.');
    }
  }
});

I've edited the code, so that it returns if no URL was found in the result, and the issue seems to be, that it's not fetching the GIF URL: Sorry, no gif URL found in the search result

答案1

得分: 1

它不返回“无结果”,而是报错“result.gif”未定义。

你应该将这行代码更改为:

const gif = result.url;

然后它就可以正常工作。至少在我这里是这样。

附言:这个答案是基于最初发布的代码,而不是更改后的代码。不会再次检查新的代码。

英文:

It doesn't return no results, but error that result.gif is undefined.

You should change this line:

const gif = result.gif.url;

to:

const gif = result.url;

Then it works fine. At least in my case.

PS. This answer is based on originally posted code, not the changed one. Won't check the new one again.

huangapple
  • 本文由 发表于 2023年4月11日 15:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定