Discord Search Command Tutorial [JS - noblox]

This is a tutorial to demonstrate how to search up Roblox profiles from your server. This will be the result. It’ll show all relevant details that the website correspondingly shows.

image

So, you’re going to need noblox.js installed, (i am assuming if you’re here you’d know how to install noblox.js)

To start off, lets require the specific libraries we need.

const discord = require("discord.js")
const roblox = require("noblox.js") // you can change the name of the const, as long as you keep it the same throughout your code.

Now, I’ve used a command handler, to make sure that my commands are organised and my index is squeaky clean. If you’d like to do this, make sure to insert a file called functions.js inside a folder called modules and insert this following code:

module.exports = (bot) => {
    bot.extractDate = (dateObj) => {
    let month = dateObj.getMonth()
    let day = dateObj.getDate()
    let year = dateObj.getFullYear()
    return {
      month: month + 1,
      day: day,
      year: year
    }
  }
};

Next, in your index install these libraries and require them.

const fs = require("fs")
const roblox = require("noblox.js")
const discord = require("discord.js")

After that’s complete, insert this into your index:

require("./modules/functions.js")(bot);
// sends bot to the cmd folder where cmds are stored, yknow, to keep index clean :D
fs.readdir("./cmds/", (err, files) => { //insert your commands in a folder called cmds
    if(err) { return console.error(err) };

    let jsfiles = files.filter(f => f.split(".").pop() === "js")
    if(jsfiles.length <= 0) {
        console.log("No commands are present.") // if no cmds are present it gets sold
        return
    }

    console.log(`Loading ${jsfiles.length} js files.`) 

    jsfiles.forEach((f, i) => {
        let props = require(`./cmds/${f}`)
        bot.commands.set(props.help.name, props)
        console.log(`${i + 1}: ${f} loaded!`) // loaded
    })
})

The final step of the setup is to insert a file called search.js inside a folder called cmds, and once this is complete, we can get to coding.

Inside search.js, we need to require the libraries needed.

const discord = require("discord.js")
const noblox = require("noblox.js")

The next step is to setup a module and the code is below. v:
To understand how it was all made, read the comments, it’ll explain what each line does.

module.exports.run = async (bot, message, args) => {
  let username = args[0]
 if (username) {
   roblox.getIdFromUsername(username).then(id => { // gets user id for the specific part of the embed
     if (id) {
       roblox.getPlayerInfo(parseInt(id)).then(function(info) {
         let date = new Date(info.joinDate) // states join date
         let dateInfo = bot.extractDate(date) 
         let embed = new discord.RichEmbed() // starts a new embed

         .setColor("#f9ae00") // sets the color of the embed
         .setURL(`https://roblox.com/users/${id}/profile`) // base link, changed by the variables 'id'
         .setTimestamp()
         .setThumbnail(`https://www.roblox.com/bust-thumbnail/image?userId=${id}&width=420&height=420&format=png`) // gets the roblox profile picture

         .addField("Username", info.username || 'Unresolvable', true) // everything in the embed is undefined, therefore can be changed by the variables
         .addField("User ID", id || 'Unresolvable', true)
         .addField("Blurb", info.blurb || 'Nothing', true)
         .addField("Status", info.status || 'Nothing', true)
         .addField("Account Age", `${info.age} days old` || 'Unresolvable')
         .addField("Register Date", `${dateInfo.month}/${dateInfo.day}/${dateInfo.year}` || 'Unresolvable')
         .addField("User Link", `https://roblox.com/users/${id}/profile`)
         .setFooter(`Powered By Engine#0001 and Alex`, bot.user.avatarURL)
          message.channel.send({embed})
       })
     }

   }).catch(function (err) {
     message.channel.send("Sorry, that user doesn't seem to exist, double check your spelling and try again!") // catching error
   });
} else {
   message.channel.send("Please provide a valid username, e.g. '-search ROBLOX'.") 
 }
    }

module.exports.help = {
    name: "search" // command name
}

Hopefully you enjoy your new discord bot command, and please leave below any more tutorials you want, if I need to elaborate more, you know, the good stuff!

27 Likes

This topic was automatically closed after 7 minutes. New replies are no longer allowed.

1 Like

Thanks, this was really helpful. I may use this in the future for some of my Discord bots I host!

A possible improvement; I’d perhaps write in more detail about what’s going on for those who don’t code in JavaScript. It allows them to understand what they’re writing in more detail and learn from it.

You could also give some free hosting sites where they could deploy their bot, such as glitch.com for those first time starters. Or if you’ve got money to spend, something like digitalocean.com would suffice.

Thanks for the contribution!

4 Likes

Great tutorial, however I agree with V_PN, you should definitely explain what the code is doing more as your target is (presumably) people much newer to NodeJS.

Really nice and useful tutorial! To people who do not use Javascript to program their discord bots, this can be done with just HTTP requests too. Just send a HttpRequest to the user’s profile and get the info from there.

That is something I did not know! I do not script, but code in Python and Javascript.
@fireboltofdeath Thank you for bringing this up, I forgot to even mention NodeJS, however the code has comments and can be understood vaguely, yet I can provide discord.js and noblox.js documentation.

1 Like

It is possible indeed. I actually made a bot which did the same thing in Lua with HTTP requests. (used Discordia to program the bot in Lua)

2 Likes

Although noblox.js will get the job done fine, there is a newer library that has arguably better code structure and uses the latest APIs (I’m pretty sure noblox still uses a few outdated APIs, but don’t quote me on that).

If you’re interested in trying it out, here’s the link to its GitHub repository: https://github.com/MartinRBX/bloxy

1 Like

I see, I’ll familiarise myself with it and hope to make the same tutorial using bloxy, this’ll be fun.

1 Like

It is still being worked on and is missing some of the methods that noblox has, but I think that the usage is more straightforward (personally).

Good luck with that :stuck_out_tongue:

Possible merger :eyes: and yes, it does use some outdated APIs.

1 Like

I’m considering whether or not I should make a discord-roblox promotion command tutorial, it may be a bit chunky, but worth the effort really.

  • Yeah, please do.
  • There’s already enough.

0 voters

Already made one but if you want to improve upon my insanely old article go ahead.

There’s been a lot of api changes since; not sure it’d still work

I simply direct them to use noblox and the new login method.

It’s relatively the same method, I’m just putting it into separate files, the code itself doesn’t really change which is a shame, I don’t like using bloxy, preferably noblox.

1 Like

What is wrong with bloxy? Is there something bad about it, or is it that you don’t like dealing with classes?

Nothing wrong with it, just more comfortable with noblox.