正在将API信息从控制台日志传输到机器人程序



我正在与Geolocation API合作开发我的discord.js机器人程序。我已经使API正常工作,并将我想要的信息接收到控制台日志中,但我无法将从控制台接收的数据集成到实际的机器人程序嵌入中。感谢提供的任何帮助

var IPGeolocationAPI = require('ip-geolocation-api-javascript-sdk');
var ipgeolocationApi = new IPGeolocationAPI("8c402b955bfb4b82a7353abc99c9dca9", false);
const Discord = require('discord.js');
module.exports = {
name: 'ip',
execute(message, args) {
var inp = args[0]
// Function to handle response from IP Geolocation API
function handleResponse(json) {
console.log(json);
message.author.send(json);
/*
//create embed
const embed = new Discord.MessageEmbed()
.setTitle(`**${inp}**`)
json.forEach((data, value) => {
embed.addField(data, value)
}) //end for each
message.author.send({
embed
});
*/
} //end handle
var GeolocationParams = require('ip-geolocation-api-javascript-sdk/GeolocationParams.js');

// Get complete geolocation in English for IP address input by argument
var geolocationParams = new GeolocationParams();
geolocationParams.setIPAddress(inp);
geolocationParams.setLang('en');
ipgeolocationApi.getGeolocation(handleResponse, geolocationParams);
}
}

错误:用于正常发送:DiscordAPIError: Cannot send an empty message用于嵌入:json.forEach is not a function

控制台输出:

{
ip: '123.456.789(ip removed)',
continent_code: 'NA',
continent_name: 'North America',
country_code2: 'US',
country_code3: 'USA',
country_name: 'United States',
country_capital: 'Washington',
state_prov: 'Georgia',
district: '',
city: 'Marietta',
zipcode: '30060',
latitude: '33.92530',
longitude: '-84.48170',
is_eu: false,
calling_code: '+1',
country_tld: '.us',
languages: 'en-US,es-US,haw,fr',
country_flag: 'https://ipgeolocation.io/static/flags/us_64.png',
geoname_id: '4207783',
isp: 'Total Server Solutions L.L.C.',
connection_type: '',
organization: 'Total Server Solutions L.L.C.',
currency: { code: 'USD', name: 'US Dollar', symbol: '$' },
time_zone: {
name: 'America/New_York',
offset: -5,
current_time: '2020-05-07 12:21:15.314-0400',
current_time_unix: 1588868475.314,
is_dst: true,
dst_savings: 1
}
}
代码中的json变量是一个对象。尝试而不是forEach(用于阵列(
for (var key in json){
//loop through all , pick what you want
}

最新更新