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

使用discord.js v14.6.0开发Discord机器人遇client重复声明错误求助

Discord.js v14.6.0 报错 SyntaxError: Identifier 'client' has already been declared 解决方法

错误根源

你的代码存在三处关键问题导致报错:

  • 重复声明变量:第一行错误从discord.js解构导入client,但v14版本中并无此导出项,随后又用const client再次声明,触发变量重复定义错误。
  • 拼写错误:GatewayIntentBIts多写了一个大写I;最后一行clien.login少了字母t。
  • 导入方式错误:v14版本中Client是需要单独解构的类,不能通过Discord.Client访问(除非整体导入模块)。

修正后的完整index.js代码

const { Client, GatewayIntentBits } = require("discord.js");
const config = require("./config.json");

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});

client.login(config.BOT_TOKEN);

额外注意事项

  • 确认config.json中的MY BOT TOKEN已替换为Discord开发者门户获取的真实机器人令牌。
  • 若需要处理消息内容,需在Discord开发者门户的机器人设置中开启消息内容特权(Message Content Intent),否则GuildMessages intent无法正常接收消息内容。

内容的提问来源于stack exchange,提问作者Ryan Hung pubg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 19:35:22