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

时间:2020-05-07 16:28:59

标签: javascript node.js discord.js

我正在使用discord.js机器人上的Geolocation API。我的api正常工作,并且我将想要的信息接收到控制台日志中,但是我无法将我从控制台接收到的数据集成到实际的bot嵌入中。感谢您的帮助

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
  }
}

0 个答案:

没有答案