javascript - Error with sending embed link with discord webhook using JS
问题描述
I have recently wanted to experiment with the node module for webhook-discord
where I got an error stating UnhandledPromiseRejectionWarning: Error: 400 Bad Request
https://www.npmjs.com/package/webhook-discord
I installed the module using vs code's built in terminal and have copied and pasted the exact code located under "Custom Messages" and have inputted my 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);
When I tried using other examples such as this, it works:
const webhook = require("webhook-discord");
const Hook = new webhook.Webhook("WEBHOOK URL");
Hook.info("WEBHOOK NAME","Info");
解决方案
这显然是图书馆的问题,你应该到这里让作者知道。
问题是他们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!"
}
推荐阅读
- reactjs - 如何在反应应用程序中更改和导入外部回购
- aws-lambda - aws lambda 函数可以处理多个 aws iot 对象吗?
- python - LightGBM 错误“无效的内核参数”
- python - python unittest迭代移动设备列表不起作用
- python - 从列表中创建一个键等于值的字典
- function - 函数递归调用已达到限制
- java - 这个递归程序中的计数器如何从 0 增加到 9?请帮我理解
- c++ - 致命错误:“dbus-cxx 至少需要 C++17”
- c# - 如何使用 C# 暂停/恢复 Windows 媒体?
- xml - XML 模式 - 在同一命名空间内扩展 complexType