javascript - 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)
解决方案
推荐阅读
- javascript - Select2 TokenSeparators 不适用于 S2MultiCheckboxes 扩展
- reactjs - 在反应钩子中一一悬停项目时出错
- azure - Terraform Azure AKS - Terraform 计划因“连接被拒绝”错误而失败
- python - 根据创建方法列出 __size__() 结果
- python - 在 Windows 终端上启动 Zsh 时出现错误消息 - Pyenv 配置错误
- django - Django:将数据从后端发送到前端而不刷新页面
- java - 我如何从 dynamodb 表中获取项目?
- c# - 返回 HTTP 状态和 JSON 响应 C#
- javascript - 不同格式的 Chrome 控制台日志记录元素
- reactjs - Next JS 和 Firebase 身份验证保护路由