首页 > 解决方案 > 如何获取表单数据并将其发送到 discord webhook?

问题描述

我正在构建一个小型仪表板,它将以 webhook 的形式将数据发送到不和谐。我能够从表单中检索数据,但不确定如何使用 node.js 和 discord.js 将其发送到 discord。这就是我现在所拥有的。我做了一些研究,发现请求是获取数据的方式,但不知道如何启动它。

form = document.getElementById("submit-form");
sucessMsg = document.getElementById("success");

function sucess(event) {
sucessMsg.style.display = "inline"
event.preventDefault()
console.log("form data:", getData())


}

function getData()
{
const formData = new FormData(form),
ret = {};

for(let [name, value] of formData.entries())
ret[name] = value;

 return ret;
 } 


form.addEventListener("submit" , sucess )

标签: javascripthtmlnode.jsdiscord

解决方案


您需要 webhook id 和令牌,要获取这些信息,请转到 Server Settings>Integrations>Webhooks 并单击假装的 webhook 并复制 webhook URL。将 URL 粘贴到 google 上以查看 webhook 的 ID 和 Token。

const id = '';
const token = '';
const webhook = new Discord.WebhookClient(id, token);
webhook.send('Your message');

在此示例中,您不需要定义 channelID,因为 channelID 是在您创建机器人时定义的。

希望对你有帮助!如果您需要其他任何东西,请告诉我。


推荐阅读