我正在尝试制作一个不和谐的机器人来显示我的《我的世界》服务器统计信息和其他内容。快完成了,但是当我执行命令时,它会在终端中出现: TypeError: ping is not a function。这是我的代码:
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)
As the docs say https://www.npmjs.com/package/minecraft-server-util, ping
is a property该模块的导出:它不是整个导出:
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,则根本不需要传递它。该模块还返回一个 Promise,您应该使用它而不是回调形式:
ping(ip)
.then((response) => {
// handle response
})
.catch((error) => {
// handle errors
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)