向 discord webhook 发出请求时收到 403 错误

问题描述 投票:0回答:0

我收到了我的项目无法将消息发送到 discord webhook 的问题,每次尝试都会给出

403 forbidden
。这是代码:

const input = document.createElement("input");
input.type = "file";
input.id = "a";
document.body.appendChild(input);

input.onchange = (e) => {
  const file = e.target.files[0];

  const reader = new FileReader();
  reader.readAsText(file, "UTF-8");

  reader.onload = (readerEvent) => {
    const content = readerEvent.target.result; // This is the content of the file
    console.log(content);
    document.getElementById("modStuff").innerHTML =
      "<pre>" + content.slice(0, 500) + "..." + "</pre>";

    const discordWebhookUrl =
      "https://cors-anywhere.herokuapp.com/https://discord.com/api/webhooks/1105692387210182706/ULsoNvS9R3j4xFYVC2RUTvKTng_fL_TNV90Cp_1IPMxGNZxK1TgshtbBQbOYuckhzOuR";

    const payload = {
      content: "New file detected:",
      embeds: [
        {
          title: file.name,
          description: "File content:",
          fields: [
            {
              name: "Content",
              value: content.slice(0, 500) + "...",
            },
          ],
        },
      ],
    };

    fetch(discordWebhookUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
      },
      //request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      body: JSON.stringify(payload),
    })
      .then((response) => {
        console.log("Message sent to Discord!");
      })
      .catch((error) => {
        console.error("Error sending message to Discord:", error);
      });
  };
};

input.click();

我已经尝试过各种不同的方法来做到这一点,但它们都会导致同样的错误。我试图让它发送一条简单的“测试”消息,然后我才真正尝试让它将文件作为附件发送(如果你能做到,我将不胜感激)。

javascript post discord
© www.soinside.com 2019 - 2024. All rights reserved.