【问题标题】:TypeError: Cannot read property 'guild' of undefined Discord.jsTypeError:无法读取未定义 Discord.js 的属性“公会”
【发布时间】:2021-06-25 11:55:49
【问题描述】:

我目前遇到 Discord.js 的问题,它说:无法读取未定义的属性 'guild'。

msg.guild.id ^

它只是发送那个错误,代码:

client.on('message', (message, msg) => {
    if(db.get(`${msg.guild.id}_prefix`)) {
      var prefix = defaultPrefix;
    } else { 
        var prefix = db.get(`${msg.guild.id}_prefix`); 
    }
    if(msg.content.startsWith(prefix + `ping`)) {
        msg.channel.send(`Fetching Latency...`).then((m) => {
        const ping = new Discord.MessageEmbed()
        .setTitle(`Pong! ????`)
        .addFields( { name: `API Latency`, value: `${client.ws.ping}ms` },
                    { name: `Bot Latency`, value: `${msg.createdTimestamp - m.createdTimestamp}ms` } )
            m.delete()
           msg.channel.send(ping)
    })}
}); 

请帮帮我

【问题讨论】:

    标签: node.js


    【解决方案1】:

    我用一些 cmets 重写了您的代码,以便更好地向您解释一切如何工作以及解决您的问题:

    //! YOUR MAIN ERROR -> There is only one callback given to the on 'message' event. Which is a Discord.Message Object.
    client.on('message', (msg) => {
        if (db.get(`${msg.guild.id}_prefix`)) {
            //let is a better option than var, since its newer it will likely be interpreted longer.
            let prefix = defaultPrefix;
        } else {
            //Once Again let is a better option than var, since its newer it will likely be interpreted longer.
            let prefix = db.get(`${msg.guild.id}_prefix`);
        }
        /*
        Since you're using the `` to define a string you dont need the (prefix + `ping`)
        Instead you can use (`${prefix}ping`)
        */
        if (msg.content.startsWith(`${prefix}ping`)) {
            /*
            Here I would recommend ; instead of ... it indicates a pause.
            You can also use msg.channel.startTyping() to validate the bot is in fact responding.
            */
            msg.channel.send("Fetching Latency...").then((m) => {
                //pingEmbed is a better naming option than ping, as per use of camelCasing and its own use.
                const pingEmbed = new Discord.MessageEmbed()
                    .setTitle(`Pong! ?`)
                    .addFields({ name: `API Latency`, value: `${client.ws.ping}ms` },
                        { name: `Bot Latency`, value: `${msg.createdTimestamp - m.createdTimestamp}ms` })
                m.delete({timeout:200, reason:"Needed To Clean Up Chat!"}).catch(err => {console.log(err)}) //Make sure you are specifying a timeout (in mili-seconds)... Reason is always optional but recommended as well.
                //If you took the above advice down here is where you would call msg.channel.stopTyping();
                msg.channel.send(pingEmbed)
            }).catch(err => {console.log(err)}); // Be Sure To Catch anything that could error. 
            //If you have multiple .then() statements, its "USUALLY" better to use a try{}catch(err){} block instead.
        }
    });
    

    【讨论】:

      【解决方案2】:

      通常client.on('message', (message, msg) => {} 只接受一个输出,如您所见:discord.js

      所以你应该只使用message

      【讨论】:

        猜你喜欢
        • 2021-05-30
        • 2021-07-02
        • 2021-11-13
        • 1970-01-01
        • 2023-01-14
        • 2020-07-14
        • 2019-07-11
        • 2021-05-29
        • 2020-08-17
        相关资源
        最近更新 更多