我試圖添加一個函式來為我在 Heroku 上運行的 Discord 機器人生成隨機 meme,但每次我嘗試運行它時,它都會給我這個錯誤:FetchError: invalid json response body at https://some-random-api.ml/meme reason: Unexpected token < in JSON at position 0. 我想知道 Heroku 是否有類似的東西?如果沒有,我的代碼是否有問題(發布在下面):
const memes = require("random-memes");
const { randomHexColor } = require('../admin/colorgen.js');
module.exports = {
name: 'meme',
description: 'Never mind what the description is!',
async execute(message, args, Discord, Client, bot) {
memes.random().then(meme => {
const newEmbed = new Discord.MessageEmbed()
.setColor(randomHexColor())
.setTitle(meme.caption)
// .setURL(meme.image)
.setDescription(`category: ${meme.category}`)
.setImage(meme.image);
message.channel.send({ embeds: [newEmbed] });
})
}
}
uj5u.com熱心網友回復:
API當時可能剛剛關閉,我之前遇到過同樣的錯誤。嘗試使用.catchafter.then(... => { ... })來處理錯誤并可能進行除錯。
memes.random().then(meme => {
console.log(meme)
}).catch(err => {
console.log(err)
});
如果您仍然收到 NPM 包錯誤,請嘗試發送一個節點獲取請求,https://some-random-api.ml/meme該請求是random-memes使用的 API(源代碼)。
const fetch = require('node-fetch');
const response = await fetch('https://some-random-api.ml/meme');
const data = await response.json();
console.log(data);
資料應該回傳類似
{
"id": number,
"image": string,
"caption": string,
"category": string
}
然后你可以從那里去。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486270.html
上一篇:Heroku無法識別用戶
下一篇:如何優雅地關閉nginx
