【问题标题】:I need my Discord Bot handling GET and POST requests for an external website我需要我的 Discord Bot 处理外部网站的 GET 和 POST 请求
【发布时间】:2019-06-13 02:52:30
【问题描述】:

我希望我的 Discord Bot 处理 GET 和 POST 请求,以通过仪表板控制我的 Bot 的某些功能。知道我的 Bot 托管在与我未来的仪表板不同的服务器中。

有人谈到设置 RESTFul API,但经过一些研究,我真的不知道该怎么做,以及它是否真的对我有帮助。我不能说是否有其他方法是可能的。 我正在使用 node.js 和库 discord.js

【问题讨论】:

  • 查看Express 并阅读一些关于它的教程,不能多说。
  • @MadWard 所说的内容,您能否展示您已经尝试过的内容,遇到的错误,... SA 不适用于此类广泛的问题,您必须更具体。您还应该查看 curl 和 Postman 等工具,以了解如何与 API 进行交互。

标签: node.js httprequest discord.js


【解决方案1】:

您可以使用 express 为 GET 和 POST 做不同的事情
对网络服务器请求使用 POST,因为浏览器使用 GET。

注意:机器人会运行它来为网络服务器提供服务

var express = require('express');
var app = express();

var data = '{"example":{"online":true,"status":"200"}}';

app.get('/', function(req, res) {
    res.send('<html><head><script>window.location.href = \'https://google.com/\'</script></head><body>You shouldn\'t be here! <a href=\'https://google.com\'>Exit</a></body></html>');
    /** Redirect the browser to google with window.location.href 
     *  Change this to your site */
});

app.post('/', function(req, res) {
    /** You could add some auth code here but
     *  if your sending it all to the client there isn't much of a difference 
     *  because people could read it from the website. */
    res.type('json');
    res.json(data);
    /* Webserver --> Bot */
    res.end();
});

app.listen(8080);
console.log('API Online');

您必须使用setInterval(() =&gt; {}, 15000); 来更新数据变量,并且您需要在客户端解析数据。
XMLHttpRequest 应该可以很好地解决这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 2013-01-17
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多