Need help making a bot in NodeJS

I think that OOP is the right thing for what I want for this. The reason I put this on the Dev Forum is because it is to do with Roblox.

I am working on a promotion bot that people can buy. To make things a lot easier for me, in the long run, I wanted to set up a system where:

In studio, someone could write this line of code (Or something similar):

local bot = HttpService:PostAsync('MyVPSIp:Port/createBot', BotUser, BotPass, GroupId)

That would essentially create a “class” on the VPS, with that someone in studio could then do:

bot:Promote(UserId)

That would then use Roblox-Js to promote the user in the group that was specified when creating the bots class.

My issue is actually using OOP to do this. My script so far is only a prototype so it doesn’t need to look pretty.
This is the main bot.js script:

const Express = require('express')

const rbx = require('./modules/RBXMain.js')

const app = Express()

app.get('/createBot', function(req, res) {
    const bot = new rbx(req.query.BotUsername, req.query.BotPassword, req.query.GroupId)
})

app.listen(1786, function() {
    console.log('Listening on port 1786')
})

My next script is the one called RBXMain.js:

var method = rbx.prototype

function rbx(botUsername, botPassword, groupId) {
    this.RobloxJS = require('roblox-js')
    this.GroupId = groupId
    this.RobloxJS.login(botUsername, botPassword).then(function() {
        console.log('Logged In.')
    }).catch(function(err) {
        console.log(err.stack)
    })
}

module.exports = rbx

All of this so far works perfectly. My issue is this: How could I then use another script called Promote.js that promotes the specified user in the group that was specified before. How do I make RBXMain.js and Promote.js to interact with each other? Is oop even the best way to do this?

3 Likes

Your answer is module.exports - this will help you by exporting your function (and related code) so that later you can require this module and execute the code inside of it without rewriting it all.

Let’s say we created a file called ‘greet.js’ that had the following code:

sayHello = function() {
  return "Hello World!";
};

We can utilise this function in another script without rewriting it, simply by exporting it and then later requiring it in the script we want to use it in.

Our greet.js file then looks like this:

var exports = module.exports = {};

exports.sayHello = function() {
  return "Hello World!";
};

Now that we’ve successfully set up our code to be exported and able for use in another file, we can create a file called main.js

In this file we will require the file that our exports function is in, and then use it to execute our function. (FYI module.exports and exports reference the same object)

var greet = require("./greet.js");

greet.sayHello();

// expected output: "Hello World!"

You can use this to write a promote function that expects a groupid and userid to promote. You can then use it in your main file. I hope this answers your question, I wrote those on mobile so excuse any mistakes :stuck_out_tongue:

If you have any more questions please feel free to ask!

3 Likes

Thanks, man!

1 Like