Search code examples
javascriptnode.jsprotocolsdiscord.js

What protocol should I use and what are the docs for it?


So I'm trying to build a Discord bot. These types of threads tend to get downvoted a lot on stackoverflow, so I'm hoping this doesn't happen to me.

This particular feature is acting as a temporary solution to my dashboard problem. Due to the nature of glitch.com's hosting, it's supposed to fall asleep after 5 minutes of http inactivity. I solved that already by adding a script that pings the URL every 4 minutes, but that caused another issue. I think what's happening is that because that script and the bot script are constantly running, and never technically 'finish', it never lets any incoming connection actually load the webpage. So my solution to that problem was to create another glitch project that would act as the dashboard website, and transfer information from the bot project. Of course then I'd need to create more scripts that communicate with each other via some internet protocol. The info recorded by the bot is all recorded in a private JSON database using the node-json-db npm library.

My problem is: I don't know what protocol would be best for this kind of thing. Even if I did know, then I'd have to go digging through the docs for the info I'm looking for.

My question is: What protocol should I use, and what docs do I need to read for this?

I've included some snippets of the code here:

The bot's server code (where I would add the script for communicating with the dashboard):

// server.js
// where your node app starts

// init project
const express = require('express');
const app = express();
const JsonDB = require('node-json-db');
const db = new JsonDB("myDataBase", true, true);

// we've started you off with Express, 
// but feel free to use whatever libs or frameworks you'd like through `package.json`.

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});

app.post('/login/c3RvcCBoYWNrZXIh', function(request, response) {
  var servername = request.param('servername');
  var password = request.param('password');
  if (db.getData("/" + servername + "/password") === password) {
response.json(db.getData("/" + servername));
  } else {
response.json(null);
  }
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

// to keep the bot alive, since glitch puts projects to sleep after 5 mins of inactivity.
const http = require('http');
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 270000);

The server.js on the dashboard website:

// server.js
// where your node app starts

// init project
const express = require('express');
const app = express();
const request = require('request');

// we've started you off with Express, 
// but feel free to use whatever libs or frameworks you'd like through `package.json`.

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});



app.post('/login', function(request, response) {
  var servername = request.param('servername');
  var password = request.param('password');
  if ("thereisnopassword" === password) {
    response.sendFile(__dirname + '/dashboard/index.html');
  } else {
    response.sendFile(__dirname + '/views/wronginfo.html');
  }
});

// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

Solution

  • I had this too, but solved it by simply putting the code to start the express server before the http loop.

    // Require packages
    const http = require('http');
    const express = require('express');
    const app = express();
    
    // Express
    app.get("/", (request, response) => {
      response.sendStatus(200);
    });
    app.listen(process.env.PORT);
    
    // Interval
    setInterval(() => {
      http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
    }, 240000);
    
    // Bot code
    const Discord = require('discord.js');
    const client = new Discord.Client();