How do I make a POST request to roblox using discord.js?

I recently made a discord bot and would like to know how I can integrate it to Roblox (beginner in web development). Code is also pretty messy so I want to start over based on your feedback

Explaination

What I have currently:
image
image
If I try to enter another command without restarting the bot and the Roblox game:
image
image
(Still prints two in the console)
Here’s my attempt at fixing this (got messy):

Code
  if (command == "robloxmessage") {
    if (args[0] && args.length <= 1) {

      if (!startedServer) {
        console.log("Server started at http://localhost:8000")
        message.channel.send("Initializing remote server.")
        server = app.listen(port)
        message.channel.send("Initialized remote server.")
        console.log("Server listening on port.")
        startedServer = true;
        try {
          app.post('/post', (request, response) => { //send a post request to the url
            response.send(`${args[0]}`) //send (post)
            console.log(request.body) //log roblox response
            setTimeout(function () {
              server.close();
              // ^^^^^^^^^^^
            }, 3000)
          })
        }
        catch (error) {
          console.log(`Roblox Event command error ${error}`)
          message.reply(`Try again or contact Johnapolitan#7935`)
        }
        console.log(`${startedServer}`)
      } else {

        const sockets = new Set();

        server.on('connection', (socket) => {
          sockets.add(socket);

          server.once('close', () => {
            sockets.delete(socket);
          });
        });

        /**
         * Forcefully terminates HTTP server.
         */

        const close = (callback) => {
          for (const socket of sockets) {
            socket.destroy();

            sockets.delete(socket);
          }

          server.close(callback);
        };
        try {
          app.post('/post', (request, response) => { //send a post request to the url
            response.send(`${args[0]}`) //send (post)
            console.log(request.body) //log roblox response
          })

        }
        catch (error) {
          console.log(`Roblox Event command error ${error}`)
          message.reply(`Try again or contact Johnapolitan#7935`)
        }
      }
    } else {
      return message.channel.send(`Please provide the proper arguments. Use !help to see them.`);
    }
  }```

Already tried: Forcing the server to close and using server.close()

To be honest, the code is at the point where it needs to be rewritten so if you’ve done this before I can just do it again of your response.

1 Like

Since you’re using NodeJS, I would recommend using Noblox.js as your Roblox integration.

And, yes, you should probably rewrite your code.

I wrote a Discord bot with Roblox integration a long time ago, you might find it of some use:
https://github.com/R0bl0x10501050/Ten-Fifty-Bot/tree/master/Commands

1 Like

Thank you for the response, the github link gives me a 404 Error

You can’t really send requests to Roblox games, but you can poll your server every few seconds until a shhutdown code was received.

Thank you for your response, I currently have this system in place. My issue is that it only works once.

In my node.js script, I check a couple of permissions(which can be ignored for now), then I initialize a local server when the command is fired. I use a POST method to send a message to Roblox. If the command is fired again, I want to shut down the server and repeat the above.

In Roblox, I use a while loop and define

local response = HttpService:RequestAsync(options)

Still, inside that loop, I use response.Success to print out everything.
If I fire the command again, Roblox doesn’t change anything and continues to resume the printing. I’ve tried using break/return after the while loop but that doesn’t work

1 Like