简体   繁体   English

我需要我的 Discord Bot 处理外部网站的 GET 和 POST 请求

[英]I need my Discord Bot handling GET and POST requests for an external website

I want my Discord Bot handling GET and POST requests, to control some features of my Bot with a dashboard.我希望我的 Discord Bot 处理 GET 和 POST 请求,以使用仪表板控制我的 Bot 的某些功能。 Knowing that my Bot is hosted in a different server than my future dashboard.知道我的 Bot 托管在与我未来的仪表板不同的服务器中。

Someone talked about setting a RESTFul API but after some researches I don't really know how to do it and if it will really help me.有人谈到设置 RESTFul API,但经过一些研究,我真的不知道该怎么做,以及它是否真的对我有帮助。 I can't say if another ways are possible.我不能说是否有其他方法可能。 I'm using node.js and the library discord.js我正在使用 node.js 和库 discord.js

You can use express to do different things for GET and POST您可以使用 express 为 GET 和 POST 做不同的事情
Use POST for the webserver requests as the browsers use GET.对网络服务器请求使用 POST,因为浏览器使用 GET。

Note: the bot would run this to serve the webserver注意:机器人将运行它来为网络服务器提供服务

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');

You will have to use setInterval(() => {}, 15000);你将不得不使用setInterval(() => {}, 15000); to update the data variable and you need to parse the data on the client.要更新数据变量,您需要解析客户端上的数据。
XMLHttpRequest should work well enough for that XMLHttpRequest 应该可以很好地工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM