Discord.js | TypeError:无法读取未定义的属性“ toLowerCase”

时间:2020-07-21 15:35:46

标签: discord.js

我正在尝试创建一个不一致的bot命令(/ role),当我尝试执行该命令时,它可以工作,但是当我更新到最新的节点版本时,它不起作用,并且记录了以下内容:

TypeError: Cannot read property 'toLowerCase' of undefined
    at Object.execute (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\commands\role.js:8:34)
    at Client.<anonymous> (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\index.js:69:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\ws\lib\event-target.js:125:16)        
    at WebSocket.emit (events.js:315:20)

这是代码:

module.exports = {
    name: 'role',
    description: "Assign yourself a role!",
    usage: "[role]",
    cooldown: 5,
    execute(message, args){

        const roleName = args[0].toLowerCase();
        let takeroles = ['Legend', 'Ultra', 'Onlylegend']

        if(roleName === "legend"){
            message.member.roles.add('734832820710801550').catch(console.error);
            message.reply("gave you the Legend role!");
        } else if(roleName === "ultra"){
            message.member.roles.add('734832749063700550').catch(console.error);
            message.reply("gave you the Ultra role!");
        } else if (roleName === "onlylegend" && message.member.roles.cache.has('734832820710801550')) {
            message.member.roles.add('734836969133309962').catch(console.error);
            message.reply("gave you the Only Legend role!");
        } else if (roleName === "onlylegend" && !message.member.roles.cache.has('734832820710801550')) {
            message.reply("you don't have permission to take that role!");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

foo.bar;

无法读取“ foo”的属性“ bar” 在这里,foo未定义。

在您的实例中,args[0]未定义。您应该进行一些检查,以确保通过if (!args.length) return message.reply(...)

提供了至少一个参数。

基本上,您必须输入!role而不是!role legend or whatever