【问题标题】:Error with sending embed link with discord webhook using JS使用 JS 发送带有不和谐 webhook 的嵌入链接时出错
【发布时间】:2021-10-07 01:42:41
【问题描述】:

我最近想对webhook-discord 的节点模块进行试验,在该模块中出现UnhandledPromiseRejectionWarning: Error: 400 Bad Request 的错误提示

https://www.npmjs.com/package/webhook-discord

我使用 vs code 的内置终端安装了模块,并复制并粘贴了位于“自定义消息”下的确切代码,并输入了我的 webhook URL。

const webhook = require("webhook-discord");

const Hook = new webhook.Webhook("WEBHOOK URL");

const msg = new webhook.MessageBuilder()
                .setName("Username")
                .setColor("#aabbcc")
                .setText("This is my webhook!");
Hook.send(msg);

当我尝试使用诸如此类的其他示例时,它可以工作:

const webhook = require("webhook-discord");
const Hook = new webhook.Webhook("WEBHOOK URL");
Hook.info("WEBHOOK NAME","Info");

【问题讨论】:

  • 如果我的回答解决了您在问题中提出的问题,您应该考虑通过单击复选标记将其标记为已接受。或者至少在提出新问题之前向您之前问题的回答者提供反馈。谢谢。

标签: javascript discord discord.js


【解决方案1】:

这显然是库的问题,您应该前往here 并让作者知道。

问题是他们的MessageBuilder 总是在帖子数据中指定一个fields 数组。而当你不添加任何字段时,会出现错误,因为数组不能为空。所以使用.addField()方法让它工作。

const Hook = new webhook.Webhook("webhook url");

const msg = new webhook.MessageBuilder()
    .setName("Username")
    .setColor("#aabbcc")
    .setText("This is my webhook!")
    .addField("Webhook Discord", "Oh, now the library works!");

Hook.send(msg);

结果如下:

图书馆的作者应该更新他们的主页或修改图书馆,使其不需要您总是指定一些fields

基本上允许您像这样发送有效负载:

{
    "username": "Username",
    "text": "This is my webhook!"
}

【讨论】:

    猜你喜欢
    • 2021-05-28
    • 2021-02-12
    • 2020-03-31
    • 2021-04-30
    • 2018-09-12
    • 2022-01-19
    • 1970-01-01
    • 2021-04-19
    • 2022-08-19
    相关资源
    最近更新 更多