我最近想对 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");
答案 0 :(得分:0)
这显然是图书馆的问题,您应该负责 here 并告知作者。
问题是他们的 MessageBuilder
总是在 post 数据中指定一个 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!"
}