Discord Roblox 机器人
Discord Roblox bot
我正在尝试制作一个输出玩家收藏品并汇总所列物品总数 recentAveragePrice
的命令。问题是当我试图输出这个 API 的任何部分时,它只输出 undefined
.
if (command === "inv"){
let getInv = async () => {
let response = await axios.get("https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100");
let inv = response.data;
return inv;
}
let invValue = await getInv();
console.log(invValue);
message.channel.send(`${invValue.data.name} \n ${invValue.data.recentAveragePrice}`);
}
因为返回的数据是对象数组。如果您想将它们全部作为消息发送,您可以遍历它们。如果您想将它们一一发送,可以使用以下方法:
if (command === 'inv') {
const getInv = async () => {
const response = await axios.get(
'https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100',
);
return response.data;
};
const invValue = await getInv();
let total = 0;
invValue.data.forEach((item) => {
message.channel.send(`${item.name} \n ${item.recentAveragePrice}`);
total += item.recentAveragePrice;
});
message.channel.send(`Total average price: ${total}`);
}
结果:
我正在尝试制作一个输出玩家收藏品并汇总所列物品总数 recentAveragePrice
的命令。问题是当我试图输出这个 API 的任何部分时,它只输出 undefined
.
if (command === "inv"){
let getInv = async () => {
let response = await axios.get("https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100");
let inv = response.data;
return inv;
}
let invValue = await getInv();
console.log(invValue);
message.channel.send(`${invValue.data.name} \n ${invValue.data.recentAveragePrice}`);
}
因为返回的数据是对象数组。如果您想将它们全部作为消息发送,您可以遍历它们。如果您想将它们一一发送,可以使用以下方法:
if (command === 'inv') {
const getInv = async () => {
const response = await axios.get(
'https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100',
);
return response.data;
};
const invValue = await getInv();
let total = 0;
invValue.data.forEach((item) => {
message.channel.send(`${item.name} \n ${item.recentAveragePrice}`);
total += item.recentAveragePrice;
});
message.channel.send(`Total average price: ${total}`);
}
结果: