创建Discord机器人时如何解决“ TypeError:无法读取未定义的属性'kick'”?

时间:2019-03-01 02:03:46

标签: javascript discord

JavaScript的新手,并试图创建一个非常基本的机器人来管理小型服务器。在完成了一些教程之后,我到了这一点:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json')
const client = new Discord.Client();

client.once('ready', () => {
    console.log('Ready!')
})

client.on('message', message => {
    if (message.member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS'])) {

        if(message.content.startsWith(`${prefix}kick`)) {

            let member = message.mentions.members.first();
            member.kick().then((member) => {
                message.channel.send(":wave: " + member.displayName + " has been kicked!")

            })
        }

    }
})

client.login(token);

运行“ node”之后。并尝试用我的“ *”前缀踢人,它返回此错误消息:

TypeError: Cannot read property 'kick' of undefined
    at Client.client.on.message (C:\Users\Danny\Desktop\DiscordBot\index.js:15:20)
    at Client.emit (events.js:189:13)
    at MessageCreateHandler.handle (C:\Users\Danny\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Danny\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Danny\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Danny\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Danny\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver._receiver.onmessage (C:\Users\Danny\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Danny\Desktop\DiscordBot\node_modules\ws\lib\receiver.js:409:14)
PS C:\Users\Danny\Desktop\DiscordBot>

我对此还很陌生,但是有其他编码语言的经验。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

member变量被分配了未定义的值。因此,请检查message.mentions.members.first();message响应的值。