You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord bot无法启动,已更新Node.js仍报错问题求助

Discord bot启动报错修复方案
  • 修复Client初始化缺失Intents的核心问题
    Discord.js v13及以上版本强制要求实例化Client时传入intents参数声明所需网关权限,你代码中直接new Discord.Client()未传参数是触发报错的主要原因,修复示例如下:
// 引入所需模块
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const fs = require('fs');

const prefix = '~';
const client = new Client({
  intents: [
    // 根据你的bot实际需求调整所需权限
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers
  ]
});
client.commands = new Collection();

注意:需要读取消息内容的话,必须先在Discord开发者后台的Bot设置页开启Message Content Intent权限。

  • 修复事件监听逻辑错误与废弃API问题
    你将消息事件监听写在了ready回调内部属于冗余写法,且旧版message事件在v14及以上版本已废弃,需替换为messageCreate,调整后的代码如下:
client.once('ready', () => {
  console.log('bot is now online');
});

client.on('messageCreate', message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;
  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();
  // 命令执行逻辑补充在此处
});
  • 修复代码转义与路径问题
    你现有代码中的=>是HTML转义后的符号,实际编码时要替换为标准箭头函数符号=>;同时确认项目根目录下存在./commands/文件夹,且文件夹内的所有JS命令文件都导出了name属性,否则读取命令时会抛出异常。

  • 检查依赖版本匹配
    执行npm list discord.js查看当前安装的Discord.js版本,确保代码写法和你使用的版本官方文档规范一致,避免跨版本API不兼容问题。

内容的提问来源于stack exchange,提问作者Athulrajtheno1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:00:01