![](/img/trans.png)
[英]Discord.js: TypeError: Cannot read properties of undefined (reading 'add')
[英]discord.js using axios: TypeError: Cannot read properties of undefined (reading 'ephemeral')
我正在嘗試使用 Axios 為我的 Discord 機器人發出 GET 請求,但我遇到了問題。
錯誤:
TypeError:無法讀取未定義的屬性(讀取“短暫”)
我使用discordjs 指南創建了這個機器人,並按照 Axios GET 請求的教程進行操作。
編碼:
const { SlashCommandBuilder } = require('discord.js'); const axios = require('axios'); module.exports = { data: new SlashCommandBuilder().setName('cat2').setDescription('Random cat'), async execute(interaction) { await interaction.reply(getRandomCat()); }, }; function getRandomCat(){ axios.get('https://api.thecatapi.com/v1/images/search').then ((res) => { var data = res.data[0].url console.log('res: ', data) return data }).catch((err) => {console.error('err: ', err)}) }
該命令用於從貓 API中獲取貓的隨機圖片。 我剛剛開始了解 discord js,所以歡迎任何幫助。 提前致謝。
嘗試將getRandomCat
設為異步 function。
const { SlashCommandBuilder } = require('discord.js');
const axios = require('axios');
module.exports = {
data: new SlashCommandBuilder()
.setName('cat2')
.setDescription('Random cat'),
async execute(interaction) {
await interaction.reply(await getRandomCat());
},
};
async function getRandomCat(){
const res = await axios.get('https://api.thecatapi.com/v1/images/search').catch(e => console.error);
var data = res.data[0].url
console.log('res: ', data);
return data;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.