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:
- i know i did this like a year late ok i’m sorry don’t kill me
Search command. - Pastebin.com
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.