首页 > 解决方案 > Discord.js ReferenceError:通道未定义

问题描述

我正在使用 discordjs 机器人指南,并从 GitHub 复制了他们的帮助命令。但是代码给出了错误。对于遇到的 ReferenceError,我已经尝试了几乎所有可以找到的解决方案。我究竟做错了什么?

我的代码:

const { prefix } = require('../../config.json');

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

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

        data.push(`**Cooldown:** ${command.cooldown || 3} second(s)`);

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

错误:

ReferenceError: channel is not defined
    at Object.execute (C:\Users\user\JS Bot\commands\utility\help.js:64:5)
    at Client.<anonymous> (C:\Users\user\JS Bot\index.js:78:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\user\JS Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\user\JS Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\user\JS Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)        
    at WebSocketShard.onPacket (C:\Users\user\JS Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\user\JS Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\user\JS Bot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)

标签: javascriptnode.jsdiscorddiscord.js

解决方案


推荐阅读