【问题标题】:Discord Webhook with XMLHttp Request使用 XMLHttpRequest 的 Discord Webhook
【发布时间】:2021-11-25 06:05:47
【问题描述】:

我正在尝试触发 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 可以工作。

谁能解释我在哪里出错了?

【问题讨论】:

    标签: typescript discord xmlhttprequest webhooks


    【解决方案1】:

    通过迁移到 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)
          
    }
    

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

    编辑:触发器保持不变

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-01
      • 2020-11-26
      • 2017-05-22
      • 1970-01-01
      • 2022-07-24
      • 1970-01-01
      • 2020-09-25
      • 2021-10-31
      相关资源
      最近更新 更多