[NEW] Roblox Search Discord Bot

Use Glitch for this tutorial for real-time debugging, this tutorial is based on Glitch setup!

Glitch is a free to use website which can host node apps, websites and much more. It can be found here.

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 on your profile, and will only be ported over to discord.

We’re going to be using the noblox.js library for this tutorial, and soon I’ll add more libraries for this tutorial, I’ve decided to change the original tutorial into a generalised tutorial with support for other languages and libraries

To start off, lets require the specific libraries we need; this will give us the ability to reference them in our code, and let us use the API they contain, these variables are constant, meaning they will not change throughout the code,

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

I’d recommend using a command handler for everything to be more readable, and the way I’d set this up in glitch is to make a folder called modules, and a file called functions.js, inside this file, paste in this code, it will only extract the date, the month, and the year:

functions.js
 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
        }
      }
    };

After the previous task is complete, you will need to require these libraries in your index, in this case in glitch, it’ll be called server.js .

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

After that’s complete, insert this into your index, it will finish off the command handler:

index.js
  require("./modules/functions.js")(bot);
    // sends bot to the cmd folder where cmds are stored, yknow, to keep index clean :D
    // insert your commands in a folder called cmds
    fs.readdir("./cmds/", (err, files) => { 
        if(err) { return console.error(err) };
        // allow the files to be detectable by glitch, pop the "." so it can be read
        let jsfiles = files.filter(f => f.split(".").pop() === "js")
        if(jsfiles.length <= 0) {
            // log in console if no cmds are present
            console.log("No commands are present.") 
            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 the command…

Inside search.js, we need to require the libraries needed; so we can ensure we have the resources to make the command.

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

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.

The next step is to setup a module and the code is below.

To understand how it was all made, read the comments, it’ll explain what each line does.
Instead of copy and pasting the code, you should type it out, and understand what you are writing.

search.js
    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(`Search Bot`, 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
    }

EDIT: FULLY COMMENTED CODE IS NOW HERE:

To get the code functional, you will need to get the discord bot online, this can be done via this video and also, you have to add the libraries via package.json

Thanks for reading my tutorial! If you’d like more tutorials like this, please let me know in the thread.

58 Likes

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!

8 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.

2 Likes

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.

2 Likes

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.

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: GitHub - affenity/bloxy: Interact with the Roblox website and API

4 Likes

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

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:

1 Like

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

2 Likes

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

2 Likes

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

1 Like

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

1 Like

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

1 Like

Nothing wrong with it, just more comfortable with noblox.

Going to quickly bump this so more people are enticed to try this on their own.

(if you recreate this in Lua, please let me know and I’ll add it onto the tutorial)

The OP has been redone, thank you for your feedback.

This tutorial is fabulous, @DEMOLURKS is not only a talented User Interfaces Designer but also a great Discord Bots Programmer.

Something I would like to add on is, maybe try to make tutorials of other roblox-related commands for people who may need it.

2 Likes

Hey.

    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 DEMOLURKS tutorial.`, 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
    }

The command is well done but I think you should add a catch function to roblox.getPlayerInfo as well. If the user you try to search is banned, it will throw an error.

2 Likes

Thanks for telling me; I’ll get to it when I find some free time.

1 Like