从 Tenor API 获取 GIFs

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

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:

  1. client.on('interactionCreate', async (interaction) => {
  2. if (!interaction.isCommand()) return;
  3. if (interaction.commandName === 'cat') {
  4. const api_key = 'API_KEY';
  5. const search_term = 'kitty';
  6. const limit = 50;
  7. const url = `https://tenor.googleapis.com/v2/search?q=${search_term}&key=${api_key}&limit=${limit}`;
  8. const command = `curl --request GET ${url}`;
  9. try {
  10. const response = await axios({
  11. method: 'GET',
  12. url: url,
  13. headers: {
  14. 'User-Agent': 'PostmanRuntime/7.28.4'
  15. }
  16. });
  17. const data = response.data;
  18. if (!data.results || data.results.length === 0) {
  19. console.log('Sorry, no results found for kitty review');
  20. await interaction.reply('Sorry, no results found for kitty review');
  21. return;
  22. }
  23. const index = Math.floor(Math.random() * data.results.length);
  24. const result = data.results[index];
  25. const gif = result.gif && result.gif.url;
  26. if (!gif) {
  27. console.log('Sorry, no gif URL found in the search result');
  28. await interaction.reply('Sorry, no gif URL found in the search result');
  29. return;
  30. }
  31. console.log(`Found kitty review: ${gif}`);
  32. // send the gif in an embed
  33. const embed = new MessageEmbed()
  34. .setTitle('Here is your kitty review!')
  35. .setColor('#FFC0CB')
  36. .setImage(gif);
  37. await interaction.reply({ embeds: [embed] });
  38. } catch (error) {
  39. console.error(error);
  40. await interaction.reply('Sorry, an error occurred.');
  41. }
  42. }
  43. });

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:

  1. client.on('interactionCreate', async (interaction) => {
  2. if (!interaction.isCommand()) return;
  3. if (interaction.commandName === 'cat') {
  4. const api_key = 'API_KEY';
  5. const search_term = 'kitty';
  6. const limit = 50;
  7. const url = `https://tenor.googleapis.com/v2/search?q=${search_term}&key=${api_key}&limit=${limit}`;
  8. const command = `curl --request GET ${url}`;
  9. try {
  10. const response = await axios({
  11. method: 'GET',
  12. url: url,
  13. headers: {
  14. 'User-Agent': 'PostmanRuntime/7.28.4'
  15. }
  16. });
  17. const data = response.data;
  18. if (!data.results || data.results.length === 0) {
  19. console.log('Sorry, no results found for kitty review');
  20. await interaction.reply('Sorry, no results found for kitty review');
  21. return;
  22. }
  23. const index = Math.floor(Math.random() * data.results.length);
  24. const result = data.results[index];
  25. const gif = result.gif && result.gif.url;
  26. if (!gif) {
  27. console.log('Sorry, no gif URL found in the search result');
  28. await interaction.reply('Sorry, no gif URL found in the search result');
  29. return;
  30. }
  31. console.log(`Found kitty review: ${gif}`);
  32. // send the gif in an embed
  33. const embed = new MessageEmbed()
  34. .setTitle('Here is your kitty review!')
  35. .setColor('#FFC0CB')
  36. .setImage(gif);
  37. await interaction.reply({ embeds: [embed] });
  38. } catch (error) {
  39. console.error(error);
  40. await interaction.reply('Sorry, an error occurred.');
  41. }
  42. }
  43. });

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”未定义。

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

  1. const gif = result.url;

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

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

英文:

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

You should change this line:

  1. const gif = result.gif.url;

to:

  1. 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:

确定