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

discord.js创建WebhookClient时报shardCount字符串属性错误如何解决

报错原因

你当前使用的是discord.js v14及更高版本,该版本对WebhookClient的初始化参数做了不兼容更新:旧版本支持直接传入webhook ID、token两个独立参数的写法已被废弃,新版本要求传入包含id和token属性的配置对象。你之前的写法将token字符串传入了预期为配置对象的参数位,代码尝试在字符串上新增shardCount属性才触发了类型错误。

解决方案

方案1:适配v14版本写法(推荐)

调整WebhookClient初始化参数为配置对象格式即可,修正后代码如下:

const Discord = require('discord.js')
const webhookID = '888151030620106792'; 
const webhookToken = 'token-here';

const webhook = new Discord.WebhookClient({
    id: webhookID,
    token: webhookToken
});

webhook.send('I am here')

你提供的第二种写法同理,修正后如下:

const { WebhookClient } = require('discord.js');

const webhook = new WebhookClient({
  id: '888151030620106792',
  token: 'zFutoKYe-OfZrfPdh0UE5yCk2wMOe-B84Xcs_Af6a76DXTx6bt3WB48qZwH-KvUriqtI'
})

webhook.send('Hello World;)')

方案2:降级discord.js版本

如果你不想调整代码,可以将discord.js降级到仍支持旧写法的v13版本,执行以下命令即可:
npm install discord.js@13

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

相关产品推荐
方舟 Agent Plan

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

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