【问题标题】:Discord.JS TypeError: ping is not a functionDiscord.JS TypeError:ping 不是函数
【发布时间】:2020-12-22 23:55:34
【问题描述】:

我正在尝试为我的机器人发出命令来 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.js typeerror


    【解决方案1】:

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

    【讨论】:

    • 嗨,我尝试了你所做的,但现在我的错误是:“ping(mc.hypixel.net, { port: 25565 }) ^ ReferenceError: mc is not defined”
    • 哦,你必须把它放在" ",所以"mc.hypixel.net"
    • 现在显示“ping("mc.hypixel.net", { port: 25565 }) ^ TypeError: ping is not a function" :/
    • 试试"play.hypixel.net"而不是"mc.hypixel.net"
    • 还是一样的:(
    猜你喜欢
    • 2020-12-31
    • 2020-12-07
    • 2020-08-23
    • 2020-01-22
    • 2020-11-13
    • 2017-11-28
    • 2021-06-15
    • 2021-01-27
    • 2021-06-17
    相关资源
    最近更新 更多