Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:ping不是一个函数_Javascript - Fatal编程技术网

Javascript TypeError:ping不是一个函数

Javascript TypeError:ping不是一个函数,javascript,Javascript,我试图制作一个显示我的minecraft服务器统计数据的discord机器人。它几乎完成了,但当我执行命令时,它在终端中出现了:TypeError:ping不是一个函数。这是我的密码: const {Client, RichEmbed } = require('discord.js') const bot = new Client() const ping = require('minecraft-server-util') const token = 'not gunna tell

我试图制作一个显示我的minecraft服务器统计数据的discord机器人。它几乎完成了,但当我执行命令时,它在终端中出现了:TypeError:ping不是一个函数。这是我的密码:

const {Client, RichEmbed } = require('discord.js')
 
const bot = new Client()
 
const ping = require('minecraft-server-util')
 
const token = 'not gunna tell u my token'

const ip = 'or ip'
 
const PREFIX = '!'
 
bot.on('ready', () =>{
    console.log('Bot has come online.')
})
 
bot.on('message', message =>{
 
    let args = message.content.substring(PREFIX.length).split(' ')
 
    switch(args[0]){
        case 'mc':
 
            ping(ip, parseInt(25565), (error, reponse) =>{
                if(error) throw error
                const Embed = new RichEmbed()
                .setTitle('Server Status')
                .addField('Server IP', reponse.host)
                .addField('Server Version', reponse.version)
                .addField('Online Players', reponse.onlinePlayers)
                .addField('Max Players', reponse.maxPlayers)
                
                message.channel.send(Embed)
            })
        break
 
    }
 
})
 
bot.login(token)
因为,
ping
是该模块导出的一个属性:它不是整个导出:

const util = require('minecraft-server-util');
util.ping('play.hypixel.net', { port: 25565 })
对于您的代码,请在导入时分解
ping
属性:

const { ping } = require('minecraft-server-util')
或者为其指定其他名称,然后对导入的对象调用
.ping

const util = require('minecraft-server-util');

// ...

util.ping(ip, /* etc */
此外,如果希望端口为默认的25565,则根本不需要传递它。模块还返回一个承诺,您应该使用该承诺而不是回调形式:

ping(ip)
  .then((response) => {
    // handle response
  })
  .catch((error) => {
    // handle errors
  });

require
ping
调用与您建议的示例新错误中的使用方式进行比较:预期“options”为对象或未定义,got numbers如果您打算在那里传递端口,请使用
{port:somePortNumber}
(不要使用
parseInt
,只使用数字文本)。端口默认为25565,因此如果您想要的端口,则根本不需要传递端口。我将发布我的新代码作为答案,但我仍然收到相同的错误。文档称您尝试使用的端口已经是默认端口,因此无需传递。你试过承诺版吗?应该像
ping(ip)一样简单。然后((response)=>{//handle response})