I'm making a command for my Discord bot, that gets GIFs or Stickers from this GIPHY API Endpoint: https://developers.giphy.com/docs/api/endpoint#search
I have provided the limit to be equal to the number selected by the user, however, it only returns 1, no matter what number I provide.
The command:
const fetch = require('node-fetch')
module.exports = {
name: 'gifsearch',
description: 'Returns a gif based on your search term.',
options: [{
name: 'type',
type: 'STRING',
description: 'Whether to search for a GIF or sticker.',
choices: [
{
name: 'GIF',
value: 'gif'
},
{
name: 'Sticker',
value: 'sticker'
}
],
required: true,
},
{
name: 'search_term',
type: 'STRING',
description: 'The search term to use when searching for gifs.',
required: true,
},
{
name: 'count',
type: 'INTEGER',
description: 'The number of results to be returned.',
required: true,
}],
async execute(interaction) {
let resultCount = interaction.options.getInteger('count')
let searchType = interaction.options.getString('type')
let searchTerm = interaction.options.getString('search_term')
if (resultCount < 1) return interaction.reply('You have to search for at least 1 result.')
if (searchType === 'gif') {
const res = fetch(`https://api.giphy.com/v1/gifs/search?q=${searchTerm}&api_key=${process.env.giphyAPIkey}&limit=${resultCount}&rating=g`)
.then((res) => res.json())
.then((json) => {
if (json.data.length <= 0) return interaction.reply({ content: `No gifs found!` })
interaction.reply({content: `${json.data[0].url}`})
})
}
else if (searchType === 'sticker') {
const res = fetch(`https://api.giphy.com/v1/stickers/search?q=${searchTerm}&api_key=${process.env.giphyAPIkey}&limit=${resultCount}&rating=g`)
.then((res) => res.json())
.then((json) => {
if (json.data.length <= 0) return interaction.reply({ content: `No gifs found!` })
interaction.reply({content: `${json.data[0].url}`})
})
}
},
};
As I said above, the user can use the count
option to specify the number of results to return. However, my bot returns only one result in every case. But each number results in a different GIF or Sticker.
I want it to return the number of results that the user provides.
In your code, you are only sending json.data[0].url
, regardless of the count
the user provides.
To send all images, you could do something like:
interaction.reply({
// assumes the limit === number chosen
content: json.data.map(result => result.url).join("\n")
});