【问题标题】:TypeError: Cannot read property 'map' of undefined | discord.jsTypeError:无法读取未定义的属性“地图”|不和谐.js
【发布时间】:2020-07-21 02:09:30
【问题描述】:

所以,我按照官方 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 });
    },
};

【问题讨论】:

  • ` data.push(commands.map(command => command.name).join(', '));` 某些原因导致 commands 未定义。检查您尝试从中提取命令的message.client 上的可用属性。

标签: javascript node.js discord.js


【解决方案1】:

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

既然你已经在传递消息和参数,你不妨从处理消息的地方传递客户端

execute(client, message, args)

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 2020-11-11
    • 2021-04-15
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多