【问题标题】:Discord.js | TypeError: Cannot read property '0' of undefined不和谐.js | TypeError:无法读取未定义的属性“0”
【发布时间】:2020-11-10 21:08:55
【问题描述】:

好的,所以我正在尝试创建一个不和谐的机器人命令(广播),我想将 args[0] 作为广播频道的 ID,但是当我尝试它时,它会记录以下内容:

TypeError: Cannot read property '0' of undefined
    at Object.execute (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\commands\broadcast.js:8:40)
    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: 'broadcast',
    aliases: ["bc"],
    usage: "[channel id] [message]",
    description: "Broadcasts a message in a channel!",
    execute(client, message, args){

        const bcchannel = parseInt(args[0]);
        const bcchannelsend = client.channels.cache.get(bcchannel);

        if (isNaN(bcchannel)) {
            return message.reply('that doesn\'t seem to be a valid channel ID!');
        } else if(args){
            bcchannelsend.send(args.join(' '));
            }   
    }
}

【问题讨论】:

    标签: discord.js typeerror


    【解决方案1】:

    Cannot read property '0' of undefined 表示args 未定义。

    您需要在使用前定义 args 变量。

    你可以这样做:const args = message.content.slice(PREFIX.length).trim().split(" ");

    下次您看到此类错误时,请务必检查哪个变量受到未定义问题的影响。 ^

    对于您的 index.js 文件,我应该替换:

        try {
            command.execute(message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }
    

    作者:

        try {
            command.execute(client, message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }
    

    因为你的 execute() 方法有 3 个参数,而不是两个。

    【讨论】:

    • 我已经在我的 index.js 中定义了 args,当我尝试将它放入命令中时,它只是说它已经被声明了。这是我的索引的 sn-p,它已经存在:client.on('message', message =&gt; { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName) || client.commands.find(cmd =&gt; cmd.aliases &amp;&amp; cmd.aliases.includes(commandName));
    • 哦,是的,我明白了,您的 execute() 方法有问题。由于您的 args 变量来自它。你在哪里定义它?
    • 我真的不明白我应该把整个 index.js 发给你吗?
    • 如果你可以在 pastebin/hastebin 的某个地方上传它 ^
    • 现在它给了我这个错误:hastebin.com/cesidopaya.js :/
    猜你喜欢
    • 2020-11-11
    • 2021-04-15
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2021-10-03
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多