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

Discord机器人无法上线求助:Client实例化报错

Discord机器人启动报错(TypeError)解决方法

错误原因

TypeError: Class constructor Client cannot be invoked without 'new'的核心问题是:Discord.js中的Client是类,必须通过new关键字实例化对象,你的代码里遗漏了new。

修正后的完整代码

// 导入Discord.js v14+的必要模块
const { Client, GatewayIntentBits } = require('discord.js');

// 使用new关键字正确实例化Client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// 监听机器人就绪事件,确认上线状态
client.on('ready', () => {
  console.log(`已登录为 ${client.user.tag}!`);
});

// 替换为你的机器人Token,完成登录
client.login('你的机器人专属Token');

额外注意事项

  • 确保已安装Discord.js:执行npm install discord.js完成依赖安装
  • Intents需要根据机器人功能按需添加,仅Guilds只能支持服务器基础操作,如需消息处理等功能要补充对应Intent
  • 机器人Token不要直接写在代码中,建议用环境变量(如process.env.DISCORD_TOKEN)管理,避免泄露

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:43:19