![](/img/trans.png)
[英]How can i make a request to a website to get dog pictures for my discord bot?
[英]I need my Discord Bot handling GET and POST requests for an external website
我希望我的 Discord Bot 處理 GET 和 POST 請求,以使用儀表板控制我的 Bot 的某些功能。 知道我的 Bot 托管在與我未來的儀表板不同的服務器中。
有人談到設置 RESTFul API,但經過一些研究,我真的不知道該怎么做,以及它是否真的對我有幫助。 我不能說是否有其他方法可能。 我正在使用 node.js 和庫 discord.js
您可以使用 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(() => {}, 15000);
要更新數據變量,您需要解析客戶端上的數據。
XMLHttpRequest 應該可以很好地工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.