Discord.js | TypeError:无法读取未定义的属性“ 0” [英] Discord.js | TypeError: Cannot read property '0' of undefined

查看:147
本文介绍了Discord.js | TypeError:无法读取未定义的属性“ 0”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay so I'm trying to create a discord bot command (broadcast) and I want to get the args[0] as an ID to the broadcast channel, but when I try it this is what it logs:

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)

这是我在命令中的代码:

Here is my code in the command:

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(' '));
            }   
    }
}


推荐答案

无法读取未定义的属性'0'表示 args 是未定义的。

使用前需要定义args变量。

You need to define the args variable before using it.

您可以执行以下操作: const args = message.content.slice(PREFIX .length).trim()。split();

You can do something like this: const args = message.content.slice(PREFIX.length).trim().split(" ");

下次您看到这种错误时,请确保检查哪个变量是受未定义问题的影响。 ^

Next time you see this kind of error, make sure to check which variable is affected by the undefined issue. ^

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

For your index.js file, I should replace:

    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个参数,但没有两个参数。

Since your execute() method has 3 parameters and not two.

这篇关于Discord.js | TypeError:无法读取未定义的属性“ 0”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆