【问题标题】:"Cannot read property 'user' of undefined" discord.js issue“无法读取未定义的属性‘用户’” discord.js 问题
【发布时间】:2020-11-30 12:32:41
【问题描述】:

我的discord bot 的代码存在问题,目前我正试图让我的bot 能够从服务器中踢出用户。

每次我尝试我的代码时,都会显示错误:

无法读取未定义的属性“用户”

代码如下:

bot.on('message', msg => {

    let args = msg.content.substring(PREFIX.length).split(" ")

    switch (args[0]) {
        case 'kick':

            const user = msg.mention.user.first();

            if (user) {
                const member = member.guild.member(user);

                if (member){
                    member.kick('You have been kicked from the server').then(() =>{
                        msg.reply(`The user ${user.tag} has been kick from the server`);
                    }).catch(err => {
                        msg.reply(`I was unable to kick this user`);
                        console.log(err);
                    });
                } else {
                    msg.reply(`This user isn\'t in the server`)
                }
            } else {
                msg.reply(`You need to specify a person`)
            }

            break;

    }
})

【问题讨论】:

  • 你必须让用户首先定义msgmsg.mention
  • 表示msg.mention未定义。我猜你想用mentions

标签: javascript discord discord.js


【解决方案1】:

原因是它支持mentions而不是mention

bot.on('message', msg => {

let args = msg.content.substring(PREFIX.length).split(" ")

switch (args[0]) {
    case 'kick':

        const user = msg.mentions.user.first();

        if (user) {
            const member = member.guild.member(user);

            if (member){
                member.kick('You have been kicked from the server').then(() =>{
                    msg.reply(`The user ${user.tag} has been kick from the server`);
                }).catch(err => {
                    msg.reply(`I was unable to kick this user`);
                    console.log(err);
                });
            } else {
                msg.reply(`This user isn\'t in the server`)
            }
        } else {
            msg.reply(`You need to specify a person`)
        }

        break;

}
})

另见:Discord official documentation

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2021-04-12
    • 2021-06-27
    • 2020-07-20
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多