Wiki93

Introduction

So you've set up your Trollbox bot but don't know what to do next? Not to worry! This page is packed with snippets of code that you can either directly add to your bot and/or tweak to suit your own bot's needs.

Note: This tutorial is for people who already have a bot up and running. If you would like to learn how to make a bot, read this first!

Things to point out

1. This page is still a work-in-progress

I am constantly refining and adding new examples to this page when I can so watch this page!

2. You're not alone in this

If you think that you're not going to receive any help while you're learning about all this then you're sadly mistaken. Each snippet contains comments that explain whats going on in each line. Additionally, if you do have any other questions and/or queries, contact me. I'm on the official Windows 93 Discord so don't be shy! There are no stupid questions when it comes to me.

3. What extensions are

Extensions are little bonus activities that you can do to take things a small step further so your bot can stand out from the others. While they are optional, it is good practice to do them so then not only does your bot and code stand out, but you also get a feel and better understanding of the code, therefore, making your a better programmer in the process!

4. What challenges are

Challenges, much like extensions, are bonuses that take the example one step further and also test your knowledge. Again, these are optional and are only there if you want to, well, challenge yourself.

Better message handling

A good thing to do is to add this at the beginning of the message event:

var message = data.msg.toLowerCase(); // the message is converted to all lowercase so our commands can be case-unsensitive! var args = message.split(' ').slice(1); // this line will be very useful for some example commands later on. the ".slice(1)" at the end takes away the command and prefix so we're only left with the actual arguments.

So, from now on each if statement will look something like this:

if (message.startsWith("command")) { // our code will be in here! }

Now with that out of the way, let's get to some commands!

Sum of two numbers

For this example, we're going to be using two arguments taken from the user, thanks to that line of code above.

if (message.startsWith("h:add2")) { // the command is called "add2", funny enough. if (args[1] === undefined) { // if there isnt a 2nd number to add... socket.emit('message', "not enough args! need two numbers.") // send an error message. return; // and then end the command here. } n1 = Number(args[0]); // since the arguments are strings, we need to convert them to numbers n2 = Number(args[1]); sum = (n1 + n2).String(); // but then, we can convert it back to a string again. socket.emit('message', sum) // send the sum! }

Extension: More than two numbers

Try to tweak the command a bit to make it able to add as many numbers as the user gives it.

Challenge: Calculator

Here's a real difficult one! Try to make the bot able to evaluate any maths equation! It's hard, but not impossible. Give it a go!

Colour wheel

In this command, we're going to be using RNG and predetermined arrays!

if (message.startsWith("h:colourwheel")) { // you already know what this line's about by now...hopefully... var colours = ["blue", "red", "yellow", "orange", "purple", "green", "cyan"] // here's our colours, all snug in a little array! var landedOn = colours[Math.round(Math.random() * (colours.length - 0) + 0)] // a random selection from the colour wheel is stored in "landedOn" socket.emit('message', "the colour wheel has landed on " + landedOn) // send the selection! }

Extension: Not just colours

You can change the array name and contents and make a completely differently themed command. This is only just to help you out, so make it your own and let your imagination run wild!

Using other npm modules

Did you know that you can use other modules in your code? I mean, it is a node application after all, right? For this example, we're going to utilize a neat little module called "moment" to tell the time.

Installing moment

First, we need to install moment before we can import it. So in order to do that, stop your bot by hitting Ctrl+C in the terminal and then type in "npm install moment":

^C C:\Users\You\WhereverYour\BotFolderIs> npm install moment

Give it up to a minute to install and then you're ready for the next step!

Requiring moment

With moment installed, we can now import it! To do so, type this line of code in somewhere at the top of your file:

const moment = require('moment') //put this in! const io = require('socket.io-client') const socket = io('http://www.windows93.net:8081', { path: '/socket.io', transportOptions: { polling: { extraHeaders: { // etc etc etc....

Using moment

We've installed it, imported it and now it's time to put it into action! Here's a command that outputs the time in UTC:

if (message.startsWith("h:utctime")) { // mhm socket.emit('message','it is currently ' + moment.utc().format('h:mma') + " UTC") // if you're not sure what's going on in this line, go check out the moment.js docs. }

Challenge 1: Timezones

While this time-telling command is cool and all, it only displays the time for a singular timezone. See if you can make your bot display the time in a certain timezone using arguments and moment's sister module "moment timezone"!

Challenge 2: Yet another module

The use of modules in your bot doesn't stop with moment. See if you can use other npm modules in your bot. Who knows, you might make something revolutionary!

Random messages every so often (feat. JSON files!)

Requested by Ant on Trollbox!

If you've been on Trollbox for at least 5-10 minutes, you've probably noticed that harry93 and Gayleb say random things every so often without anyone doing anything. The code behind this interesting feature is actually quite simple. Let me show you!

DISCLAIMER: The misuse of the following code can get your bot shut down by Jankenpopp and will probably force me to remove this section from the page. So, if you're planning on using this code in your bot, use it responsibly and don't ruin it for everyone else!

Getting the list together

Before we get it to say random things, we need to make a list of random things to say! To do this, we can use an external JavaScript Object Notation (JSON) file. So, in your bot folder, create a new file and call it text.json . It doesn't have to be called text , but its extension needs to be .json . With JSON files, we can store large amounts of numbers, strings, and arrays so it doesn't clog up the main bot file.

{ // even though json doesn't support commenting, i still would like to mention that curly braces are used to open and close the whole file. you can think of a json file as a lua list almost! "funnyNumber": 7, "niceGirl": "kailey", "distros": [ "ubuntu", "mint", "arch", "fedora", "debian" ], "player": { "x": 53, "y": 315 } }

So for our text.json , we can create an array called quotes or random_stuff if you want.

{ "quotes": [ "trans rights are human rights!", "every user in this room is real...except me.", "kailey is an amazing girl. please send her hugs!", "make trollbox friendly again!", "jet set radioooooooooooooooooooooooooooooooo", "be nice, alright?" ] }

And now, back in our index.js file, we can import the JSON file similarly to how you would import a module.

const text = require('./text.json') // here's our json file! const moment = require('moment') const io = require('socket.io-client') ...

Now that we have our random things, let's make our bot say these things!

The actual code behind it

All you have to do is use a setInterval(); function to make your bot execute code over and over again after a, well, set interval!

// you can put this line of code just about anywhere in your file. i keep mine at the bottom so it's not in the way. setInterval(function(){ socket.emit('message', text.quotes[Math.round(Math.random()*text.quotes.length-1)]) // this line may look familiar. notice how we call the quotes by using "text.quotes". }, 120000) // this number determines the interval in milliseconds. in this case, its for 2 minutes. you can set this number to whatever you want but i strongly advise to set the interval to over a minute or your bot might become a bit spammy.

And that's it! Your bot will now say something random every so often!

[ js ] [ trollbox ] [ tutorials ]

page made by harry with <3