简体   繁体   中英

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. Knowing that my Bot is hosted in a different server than my future dashboard.

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. I can't say if another ways are possible. I'm using node.js and the library discord.js

You can use express to do different things for GET and POST
Use POST for the webserver requests as the browsers use 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); to update the data variable and you need to parse the data on the client.
XMLHttpRequest should work well enough for that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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