This tutorial is part of the ro.py tutorial set.
To view more ro.py tutorials and learn more about ro.py,
visit the Discord server, main topic, documentation, or GitHub repository.
If you’ve ever made Discord bots or worked with discord.py, we’ll essentially be going over the Roblox equivalent of that!
This tutorial will go over how to use the bots
extension from ro.py. When you’re done, you’ll be creating interactive bots that use the Roblox web chat like my example:
Please note that this is a very early feature of ro.py and it’s not guaranteed to work! If you encounter an issue, send me a reply and I’ll fix it.
If you want to read the source code for the bots extension, it’s available here! The source code for all of ro.py is available here. The best way to help support the project is to give it a star on GitHub, since it allows more people to learn about ro.py
To begin, just start with Part 1 below and keep going! Don’t worry, it’s not hard.
Update: ro.py on Discord
I’ve set up a small ro.py Discord server. It’s obviously very tiny, but some of you can be the first people to help found the server. If you need support for the library, you can ask your questions here if you need faster support. http://j-mk.ml/ro.py
Part 1: Installation/Setup
To begin, you’ll need to install ro.py. You can install it from PyPI (pip), but I suggest cloning from GitHub to get the latest features.
At the time of writing, the features used in this tutorial aren’t available on the PyPI version. They are now available!
We’ll use this command to install ro.py:
pip install git+git://github.com/rbx-libdev/ro.py.git
(Use pip3
instead of pip
to ensure you’re using the Python 3.x version of pip if you have Python 2.x installed.)
Next, create your Python file that’s going to contain your code, and add this to the top of it:
from ro_py.extensions.bots import Bot
bot = Bot()
The first line imports the bots
extension from ro.py.
The second line creates a new Bot.
By default, our bot will use the !
prefix. To change this, just pass a different prefix to the bot:
bot = Bot(prefix="?") # uses ? as prefix
Part 2: Add Your Commands
I told you this wouldn’t be hard! We’re already ready to create our commands.
All we have to do to tell our bot that our method should be used as a command is add a decorator to it.
To begin, add this to your file:
async def hello(ctx):
await ctx.send("Hello")
The first line creates an asynchronous method called hello
with a parameter called ctx
. ctx
is short for Context
and contains context about the command and allows us to easily respond to it with ctx.send
.
The second line responds with Hello
.
To tell our bot that this should be treated as a command, we’ll add this above it:
@bot.command()
Your method should now look like this:
@bot.command()
async def hello(ctx):
await ctx.send("Hello")
Repeat this with different method names to add any commands you would like!
Part 3: Add Parameters
To add parameters to our commands, we can just add parameters to our method. All parameters are strings at the moment and the system isn’t perfect, but it’s good enough for now.
We’ll add a parameter called echo
to our hello
command, which we’ll add to the end of our Hello
text like so:
@bot.command()
async def hello(ctx, echo):
await ctx.send("Hello")
We aren’t doing anything with this echo
parameter yet. It’s just a normal variable, so you can use it for anything you’d like. I’ll just add it to the end of our Hello
text using the +
operator.
@bot.command()
async def hello(ctx, echo):
await ctx.send("Hello " + echo + "!")
Sometimes, we won’t want to pass a parameter. If we give this parameter a default value, we won’t need to specify it when calling our command. We can give it a default value like so:
@bot.command()
async def hello(ctx, echo="World"):
await ctx.send("Hello " + echo + "!")
Now, without a parameter, we can send !hello
and it’ll respond with Hello World!
. If we do specify a parameter, it’ll use that parameter instead of World
. For example, if we sent !hello Earth
it would repsond with Hello Earth!
Important Note: It’s not a good idea to keep something like this in an actual bot, as it can be abused and cause your bot account to be banned if something someone sends violates the ToS, since it’ll reply that right back to that user.
Part 4: Events
Certain event methods are called when certain things happen, like when a command fails to run. We can use a decorator to mark a method as an event like so:
@bot.event()
This is a method that will be called when a command errors out:
@bot.event()
async def on_error(ctx, exception):
try:
await ctx.send("Error: " + str(exception))
except Exception:
await ctx.send("Something went wrong.")
The reason we have an extra try
statement here is because it’s possible that something goes wrong in our own error handler (e.g. our message gets filtered). In that case, we just want to send Something went wrong.
Part 5: Start Your Bot
We’re almost there! All we have to do to launch our bot is call bot.run
at the end of our file.
Please note that this does require passing a .ROBLOSECURITY token. Even though this program is completely open-source and I have written it, please don’t test this on your main account! You’ll need an alternate account either way to run these commands.
I won’t explain how to grab a .ROBLOSECURITY token in detail here, but you can grab it from your browser’s cookies.
Add this to the end of your file:
bot.run("roblosecurity goes here!")
And you’re off! You are ready to launch your bot. If you encounter any issues, please reply as I’ll fix them as soon as I can. To test your bot, send chat messages to it from another account.
Documentation for ro.py is available at https://ro.py.jmksite.dev/. It’s not up to date with the bots extension at the time of writing, but I’m pushing changes soon! It is now up to date.
If you liked this tutorial, feel free to give it a like or star our project on GitHub to help support us. Have a great day!