Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

javascript - Error when fetching Giphy Api using discord.js

[beginner here] I am currently using "@giphy/js-fetch-api" for some reason when executing my command to fetch the gifs, I get back "Uncaught Error: @giphy/js-fetch-api: Error fetching" the code's purpose to allow people to search for random gifs via the args/arguments. example: !gif food

Code Below

module.exports = {
    name: 'fig',
    description: 'returns back a random gif base on the 1st word',
    async execute(message, args) {  
        var { GiphyFetch } = require("@giphy/js-fetch-api");
        const gf = new GiphyFetch('MY KEY')
        var usrInput = args
    
            
            const { data: gif } = await gf.random({ tag: usrInput, type: 'gif' })
            var info = {data: gif.images.fixed_height.url}
            var url = info.data
            let urlString = url.toString()

            message.channel.send({files: [urlString]})
           


    }
}

The odd thing is that I am also using it for a simple coin flip and it works here so I'm very confused as to why it doesn't work for the other command

module.exports = {
  name: 'flip',
  description: 'Random number Gen for Coin Flipping',
  async execute(message, args) {
    const { GiphyFetch } = require('@giphy/js-fetch-api')
    const gf = new GiphyFetch('MY KEY')
    var coin = Math.floor(Math.random() * 2) + 1
    const db = require('quick.db')
    if (coin === 1) {
      db.add('coin.head',1);
      const coinHead = db.get('coin.head') 
      const { data: gif } = await gf.random({ tag: "heads", type: 'gifs' })
      var info = {data: gif.images.fixed_height.url}
      var url = info.data
      let urlString = url.toString()
      message.channel.send(`you got heads, its popped up `+coinHead+` times!`, {files: [urlString]}) 

     } else {
      db.add('coin.tails',1);
      const coinTails = db.get('coin.tails')
      const { data: gif } = await gf.random({ tag: "tails", type: 'gifs' })
      var info = {data: gif.images.fixed_height.url}
      var url = info.data
      let urlString = url.toString()  
      message.channel.send(`you got tails, its popped up `+coinTails+` times!`,{files: [urlString]})  
    }
  }
};

Lastly is my discord.js index file to add full light to what I got going

//require('dotenv').config();
const fs = require('fs');
const Discord = require('discord.js');
const {prefix, token} = require('./config.json');
const db = require('quick.db')
var http = require("http");
require("isomorphic-fetch");
var { GiphyFetch } = require("@giphy/js-fetch-api");
const { arch } = require('os');
const gf = new GiphyFetch('KEY')
const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

http
  .createServer(function(req, res) {
    const fetchGifs = async () => {
      const gifs = await gf.trending();
      res.write(`${gifs.data.length} gifs fetched!`);
      res.end();
    };
    fetchGifs();
  })
  .listen(8080); 



for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}




//const TOKEN = process.env.TOKEN;

client.login(token);

client.once('ready', () => {
  console.info(`Logged in as ${client.user.tag}!`);
  client.user.setActivity('!help', { type: 'WATCHING' });
});



client.on('message', message => {
  console.log(message.content)
  // do not touch .bot here on line 32
if (!message.content.startsWith(prefix) || message.author.bot) return;


const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
console.log(`Command name: ${command}
Arguments: ${args}`)

if (command === 'ping') {
    client.commands.get('ping').execute(message, args);
} else if (command === 'flip'){
    client.commands.get('flip').execute(message,args);
}
  else if (command === 'shore'){
    client.commands.get('shore').execute(message, args);
  }
  else if (command === 'help'){
    client.commands.get('help').execute(message, args);
  }
  else if (command === 'ocean'){
    client.commands.get('ocean').execute(message, args);
  }
  else if (command === 'vibe'){
    client.commands.get('vibe').execute(message, args);
  }
  else if (command === 'fig'){
    client.commands.get('fig').execute(message, args)
  }
  }
)
question from:https://stackoverflow.com/questions/65865574/error-when-fetching-giphy-api-using-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...