带有 XMLHttpRequest 的 Discord Webhook

Discord Webhook with XMLHttp Request

我正在尝试触发 Discord webhook 执行此操作:

if ('1' !== '2') {
          console.log(`This is false!`)        
          sendMessage();

我要发送的消息是:

function sendMessage() {
          var request = new XMLHttpRequest();
          request.open("POST", "https://discord.com/api/webhooks/XYZ");
          request.setRequestHeader('Content-type', 'application/json');
          var myEmbed = {
              title: "Test",
              color: 0xFF7100,
              fields: [
                  {
                      name: 'Test',
                      value: 'Test',
                  }, {
                      name: 'Test 2',
                      value: "Test 2",
                  }],
              timestamp: new Date(),
              footer: {
                  text: 'v 1.6.2'
              }
          }
          var params = {
              username: "Test Webhook",
              avatar_url: "",
              color: "#FF7100",
              content: "",
              embeds: [myEmbed],
      
          }
          request.send(JSON.stringify(params));
      }

我已经在浏览器控制台中尝试了 sendMessage() 并且它成功发送了 webhook,表明我的项目中没有触发 sendMessage(),但是 console.log 有效。

谁能解释一下我哪里出错了?

通过移动到 axios 解决了这个问题:

const webhook= 'https://ptb.discord.com/api/webhooks/XYZ';

const axios = require("axios");

function sendWebhook() {
  const request = {
    "content" : `Any Text`, //This will be the regular message above the embed
    "username": "New User",
    "avatar_url": "",
    "embeds": [{
      "color": 0xFF700 ,
      "timestamp": new Date(),
      "author": {
        "name": "",
        "icon_url": "",
      },
      "title": "Any Title",
      "fields": [{
        
        "name": "Field 1",
        "value": "Field 1 value"},
        {        
        "name": "Field 2",
        "value": "Field 2 value"},
        {        
          "name": "Field 3",
          "value": "Field 2 value"},                          
    ],
      "url": "", //This will set an URL for the title
    }],
  };

    return axios.post(webhook, request)
      
}

也许这会对某人有所帮助♥

编辑:顺便说一句,触发器保持不变