2

[这里的初学者] 由于某种原因,我目前正在使用“@giphy/js-fetch-api”,在执行获取 gif 的命令时,我返回“未捕获的错误:@giphy/js-fetch-api:错误获取”代码的目的是允许人们通过 args/arguments 搜索随机 gif。例如:!gif 食物

下面的代码

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]})
           


    }
}

奇怪的是,我也将它用于简单的硬币翻转,它在这里工作,所以我很困惑为什么它不适用于其他命令

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]})  
    }
  }
};

最后是我的 discord.js 索引文件,为我的工作增添了光彩

//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}\nArguments: ${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)
  }
  }
)
4

0 回答 0