Create chat command bots with the Roblox web chat!

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! :wave:

18 Likes

It looks nice!

Someone made Bad Apple using Discord Chat :stuck_out_tongue:

2 Likes

Thank you! I’m glad you like it.

1 Like

Is this program free?

Make Bad Apple in Roblox chat.

1 Like

Not only is it free, but it’s open source! You can follow the tutorial and create your own, 100% free.

I don’t think that’s possible due to anti-spam.


If you don’t know what Bad Apple!! is

3 Likes

Yeah maybe someone can bypass it

Who knows :laughing:

1 Like

You could most likely do it, but the motion wouldn’t be smooth. Roblox doesn’t tell us what the actual rate limits are for endpoints, but in theory it could be done if we did figure it out.

ro.py doesn’t implement rate limits (if we did implement them, we would have to constantly update it every time they change it) so if you did try to send a ton of messages in sequence you would just get ratelimited.

This is a problem for this extension, because we might be handling lots of commands at once, so I’m working on this to figure out exactly how we should handle this.

1 Like

I have a question. What would you use this bot for? Using it for groups wouldn’t work well due to the 5 person group limit. :thinking:

2 Likes

It’s more of a tech demo than a useful thing. What I’m planning on doing soon is a self bot system that allows you to send these commands and it’ll respond to yourself, which might be more useful.

It’s also really fun to mess with🙃

2 Likes

Update

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. Feel free to join if you’re interested, or don’t.
If you need support for the library, you can ask your questions here if you need faster support.
http://j-mk.ml/ro.py

This is pretty nice, I’m sure it’ll come in really handy for game support or feedback; is this against the terms of service in any way?

1 Like

No, it is not.
In theory, you could use ro.py to do something that’s against the ToS if you wanted to, but we don’t support that in any way. We haven’t written any rule-breaking code, but in theory you could write your own code that sent something nasty in chat or something like that.