TypeError:无法读取未定义的属性“ map” discord.js

时间:2020-07-18 00:34:03

标签: javascript node.js discord.js

因此,我遵循的是正式的discord.js文档教程,但它不起作用。我是js和discord.js的新手,所以我可能会有所遗漏。我做了一些修改,使其适合我的项目,可能也是如此。就是这样。

TypeError: Cannot read property 'map' of undefined
    at Object.execute (C:\bot1\commands\help.js:12:23)
    at Client.<anonymous> (C:\bot1\index.js:40:32)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\bot1\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\bot1\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\bot1\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\bot1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\bot1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\bot1\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)

这是代码

module.exports = {
    name: 'help',
    description: 'List all of my commands or info about a specific command.',
    execute(message, args) {
        const data = [];
        const { commands } = message.client;

        var prefix = "e!";

        if (!args.length) {
            data.push('Here\'s a list of all my commands:');
            data.push(commands.map(command => command.name).join(', '));
            data.push(`\nYou can send \`${prefix}help [command name]\` to get info on a specific command`);

            return message.author.send(data, { split: true })
                .then(() => {
                    if (message.channel.type === 'dm') return;
                    message.reply('I\'ve sent you a DM with all my commands');
                })
                .catch(error => {
                    console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
                    message.reply('It seems like I can\'t DM you');
                });
        }

        const name = args[0].toLowerCase();
        const command = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name));

        if (!command) {
            return message.channel.send('That\'s not a valid command.');
        }

        data.push(`**Name:** ${command.name}`);

        if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`);
        if (command.description) data.push(`**Description:** ${command.description}`);
        if (command.usage) data.push(`**Usage:** ${prefix}${command.name} ${command.usage}`);

        message.channel.send(data, { split: true });
    },
};

1 个答案:

答案 0 :(得分:0)

确保您实际上是在分配client.commands = new Discord.Collection();

由于您已经在传递消息和args,因此您最好从处理消息的地方传递客户端

execute(client, message, args)