首页 > 解决方案 > Discord.JS TypeError:ping 不是函数

问题描述

我正在尝试为我的机器人发出命令来 ping Minecraft 服务器(例如 Hypixel),但是每当我运行机器人并输入命令时,我都会得到以下信息:

ping('mc.hypixel.net', 25565, (error, response) =>{
            ^

TypeError: ping is not a function

这是我的代码:

client.on('message', message =>{

    let args = message.content.slice(prefix.length).split(' ')

    switch(args[0]){
        case 'mc':
            ping('mc.hypixel.net', 25565, (error, response) =>{
                if (error) throw error
            
                console.log(response)
            })
        break
    }



})

标签: discord.jstypeerror

解决方案


discord.js不支持这样的命令,但有其他包支持!只需minecraft-server-util安装npm i minecraft-server-util

之后把它放在你的代码中

const { ping } = require('minecraft-server-util');

接着


//use ping command
 ping(<name of the server>, { port: 25565 })
.then((response) => {

//we are sending a response to the channel where command was initiated
       return message.channel.send(response);
    })
    

//we are catching an eventual error
    .catch((error) => {
        throw error;
    });

推荐阅读