Learn how to program
by playing video games.

Learn JavaScript by playing RuneScape

How To Code a RuneScape Bot with RobotJS

March 19, 2020
Part of: Learn JavaScript by playing RuneScape

In this tutorial series, I'm going to teach you how to program a simple Runescape bot in JavaScript. More specifically, we're going to be using Node.js and the RobotJS library to do GUI automation, and with those tools we're going to make a basic woodcutting bot. You don't need to have any prior programming experience to follow along. This tutorial is aimed at absolute beginners, so I'll be going slowly, showing you how to get everything set up, and explaining fundamental programming concepts as we go. So if you've ever been interested in programming or in JavaScript, this project should be a great place to get started.

Links
GitHub repo: https://github.com/learncodebygaming/woodcutter (view source code here)
Node.js: https://nodejs.org/
RobotJS: http://robotjs.io/
Visual Studio Code: https://code.visualstudio.com/download
Eloquent JavaScript: Free Online Edition / Paperback or Ebook Edition
W3Schools: https://www.w3schools.com/js/default.asp

Before we get too far, I need to start with a disclaimer.

My intentions with this tutorial, and all the videos on this channel, is to teach you how to program in a fun and engaging manner, NOT to help you cheat at video games. Cheating is really unfair to the other players, and even more so to the developers of the game. And as programmers, keep in mind that we might someday be that game developer who has to worry about cheaters inside of our game. So do NOT run your bot on the official Runescape servers (OSRS), or anywhere else where it's considered cheating. Instead, I'm going to show you how to develop your code on a Runescape private server (RSPS), where the skilling progression is sped up anyway, so don't have to worry about doing any harm to the game or breaking any rules.

If you do decide to run your Runescape bot where it's not allowed, don't be surprised, and don't blame me, if you get caught and your account gets banned. The game developers have been catching bots literally for decades, and you've only just begun to learn how to program. The methods I'll be showing you here are pretty basic and potentially easy to detect. So with those words of warning, let's get into it.

JavaScript has always been a popular language with new programmers because it's very easy to get started with. All you need is a web browser. Every modern browser can run JavaScript.

So let's write your first line of code. If you're in Chrome, right-click anywhere on a web page and click Inspect. Then in the window that pops up, click on Console. You can do almost the exact same thing in Firefox or even the Edge browser.

This is the JavaScript console. You can write and execute JavaScript code from right here. If we type 3 + 4 and hit enter, you can see we get the expected result. You can also call a function like alert(‘Hello'); and when you hit enter, you can see a popup appear.

JavaScript is the language of frontend web development, so any programmer who wants to work on the web needs to be able to write JavaScript at some point. That's another reason why JavaScript is a good place to start learning, because most programmers will need to know it eventually anyway.

Now, for the woodcutting bot we're going to write, I'm going to teach you a technique called GUI automation. This is a technique where you write code to automate mouse movement and keyboard inputs, while using information from the screen to make decisions about what your code should do next. If you've seen my tutorial series on PyAutoGUI, we're using the same concepts here, except we'll be doing things in JavaScript instead of Python.

In order to use GUI automation, we won't be able to run our JavaScript code inside the browser. That's because browsers come with protections that prevent code from controlling a user's mouse and keyboard. This is a very good thing, because could you imagine if the websites you visited could control your mouse and keyboard... that would be terrible. So we need some way to run our code as a desktop application that has permission to control our mouse and keyboard. 

To do that, we'll be using Node.js, a very popular JavaScript runtime that's used by professional developers all the time.

Download and install Node.js from their website, nodejs.org. The home page should give you a link to the recommended download for your platform. In my case that was the LTS 64-bit Windows Installer. When installing, you should click the checkbox to "Automatically install the necessary tools". If you don't, you'll get errors when we try to install the RobotJS library.

So that probably took a little while, but when it's finished, let's confirm it was installed correctly by using the terminal. On Windows, I prefer to use Git Bash, but you can also use PowerShell. Enter the command node --version and it'll output the installed version of node if it's installed correctly. If I've lost you, here's how you would open PowerShell and do it from there (right click the Windows button and select Windows PowerShell, or search for it). If you're on Mac, the program to search for is just called "Terminal".

Keep your terminal window open, because we're going to be using it again in a minute.

Now let's talk about how code is written. When we write code, we're just writing simple text files, and our code later gets interpreted or compiled into machine code when we run it. So you don't need anything more than just Notepad to write code. But most programmers don't write code in Notepad. We use a special type of application called an Integrated Development Environment, or IDE. These are just really fancy Notepads that provide extra features to make programming easier and more enjoyable, with features like syntax highlighting and autocomplete.

So first I'm going to show you how to write and run a simple program using just Notepad and the terminal. And then for the remainder of this tutorial series I'll be using an IDE called Visual Studio Code. And then you can decide for yourself if you want to install VSCode, or just keep using Notepad. You'll be able to follow along fine either way.

So in Notepad, I want you to type console.log("hello");

And save this as index.js inside of a new folder you make for this project.

What this code does is it prints the string we give it, in this case hello, to our console. Earlier, in our web browser, we saw the console tab, so that's where these messages go when you run this code inside a browser. But we're going to run this with Node, so in that case our console will be the terminal window. You'll see what I mean.

So now in your terminal, you first need to change directories to your new folder. You can use ls to list all the folders and files in your current directory, and cd path to change directories. In my case, PowerShell is already in my Ben folder, so I need to cd .\Desktop\woodbot\. And you can start typing your folder here and then hit tab to autocomplete the path.

If you do ls again, you should now see the index.js file in there.

Now we can run our code with node index.js and it should just output hello and finish.

And that's all it takes. You just write your code, then you can use the command node filename to run it. Now I'll show you this same thing in VSCode.

I'm just going to open the project folder we created, and index.js is already in there. If I open up that code, you can see it's still the same but now I've got syntax highlighting. And VSCode has an integrated terminal, which you can open up under View. And in here I can just do the same node index.js to run our code.

So if you'd like to use VSCode, you can download it for free. Link in the description, or you can just Google search for it. 

I'm not going to walk you through the whole setup for VSCode, but I do want to point out a few things. In the integrated terminal I'm using the Git Bash shell, but you can continue to use PowerShell here if you like. Also, you'll also see me use this "Run Code" button in the upper corner to execute my code. This is an extension called "Code Runner" that you can install if you want. All it does is run the node command I've been showing you for the file you have open. And if you like my color scheme, I'm using "Nord", which you can also find and install under the extensions.

Alright, so now you're all set up to write JavaScript and run it with Node.js. 

The next thing we want to do is to set up our project as a Node package. This will allow us to install the RobotJS library, or any other packages we might need.

In your terminal, enter npm init -y. This will generate a package.json file in our project folder. "npm" stands for Node Package Manager, and it was installed when we installed Node.js. We just ran the initialize command. Go ahead and open the package.json file that was created. In here, feel free to set your project name, description, and author if you like. But this isn't necessary because we won't be publishing our package for others to use.

Now we can install RobotJS. This is the library that will make it easy for us to control the mouse and keyboard, and to read the screen. Again in your terminal, enter npm install robotjs

If you get errors that look like "gyp ERR! find VS" and "gyp ERR! stack Error: Could not find any Visual Studio installation to use", that means you didn't check "Automatically install the necessary tools" when you installed Node.js, and you'll need to go back and repair that.

But if RobotJS installs successfully, you'll notice several more things have changed inside your project. There's now a package-lock.json file, and a node_modules folder. This folder will contain all the code for the libraries that you've installed. Also, in your package.json, you'll see a "dependencies" section has been added, and that our project now depends on "robotjs" version "0.6.0" or higher.

Now let's confirm that RobotJS is working properly.

In our code, first we need to import the RobotJS library. var robot = require("robotjs"); I'll explain this code in greater detail in the next video, but for now it's fine to just follow along. Right now I'm just verifying that RobotJS is working for you.

Once it's imported, we can call the move mouse function on it, robot.moveMouse(0, 0);, and by giving it the coordinates "0, 0" that will direct RobotJS to move our mouse to the upper left hand corner of the screen. Before we run this, move your mouse to somewhere in the middle of the screen, and what we expect to happen is: we expect the mouse to jump immediately to the very upper left corner when we run our code. So let's go ahead and do that, and we can see that's exactly what happens.

Alright, so we're finally ready to start building our bot.

The last thing you need to do, if you haven't done so already, is to get a Runescape private server client installed on your computer. For this tutorial I'll be playing on Ikov. Another RSPS I would recommend for this is DreamScape. But any private server that allows this sort of botting should work fine.

In that next video, we'll start writing code for our woodcutting bot. 


RobotJS Tutorial for Beginners
Part 2 of this tutorial will teach you the basics of how to automate mouse inputs using RobotJS. We go over require statements, functions, how …
Color Botting Basics
Our JavaScript RuneScape bot continues with part 3. Here we learn how to automate dropping logs from our inventory, how to use basic computer vision …
Perfecting our Woodcutting Bot
Today we finish coding our woodcutting RuneScape bot! In part 4 we use our pixel color matching skills to confirm when logs appear in our …
Ben Johnson My name is Ben and I help people learn how to code by gaming. I believe in the power of project-based learning to foster a deep understanding and joy in the craft of software development. On this site I share programming tutorials, coding-game reviews, and project ideas for you to explore.