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

Discord.js v14.6.0发送Webhook报InvalidType错误求助

Discord.js v14 WebhookClient 「InvalidType」错误修复方案

问题背景

环境:Discord.js API v14.6.0,Node.js v18.12.1
旧版本代码可正常通过变量指定用户名、头像发送Webhook消息,但升级到v14后,创建WebhookClient时触发以下错误:

TypeError [InvalidType]: Supplied options is not an object.

修改webhook.send的参数结构后仍无法解决。

错误原因

Discord.js v14对WebhookClient的构造函数做了破坏性变更:

  • v13及以前支持直接传入(id, token)两个参数
  • v14要求必须传入包含id和token属性的对象,或直接传入Webhook的完整URL

你的代码中new WebhookClient(thisID, thisTOKEN)的写法不符合v14的参数要求,导致触发类型错误。

修复代码

以下是修正后的完整代码,关键修改点已标注:

const { Client, GatewayIntentBits, EmbedBuilder, WebhookClient } = require('discord.js');
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.MessageContent,
  ] 
});

client.on('messageCreate', msg => {
  if(msg.content === 'send') {
    msg.channel.fetchWebhooks()
      .then(hooks => {
        try {
          console.log('1');
          setTimeout(sodps, 350);
          function sodps() {
            // 关键修改1:WebhookClient构造函数传入包含id和token的对象
            let wwebhook = new WebhookClient({ id: thisID, token: thisTOKEN });
            
            // 关键修改2:send方法仅接受一个对象参数,整合content、username、avatarURL
            wwebhook.send({
              content: thisCONTENT,
              username: THISusername,
              avatarURL: THISavatarURL
            });
          }
        } catch(err) {
          console.error(err);
        }
      }).catch(console.error);
  }
});

额外注意事项

  1. 确保thisID、thisTOKEN、thisCONTENT、THISusername、THISavatarURL这些变量已正确定义并赋值
  2. 如果是从fetchWebhooks()获取到的Webhook对象,可以直接调用hook.send(),无需重新实例化WebhookClient,示例:
// 假设hooks是获取到的Webhook集合,取第一个为例
const targetHook = hooks.first();
targetHook.send({
  content: thisCONTENT,
  username: THISusername,
  avatarURL: THISavatarURL
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:50:30