如何解决使用 discord.js 获取 Giphy Api 时出错
[这里是初学者] 我目前正在使用“@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',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)
}
}
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。