Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making a bot for chit chat could never be easier!
ThatGhost (4)

This was made with permission from GameDev46 himself.

Content

  • How did I do it?
  • Getting Started
  • Creating a new BotUser
    • Bot Security
  • Using the .when Event Emitter to Make Commands
  • Organizing Commands (command-handling)
  • My go to Command Handler
  • Using Chit-Chat Markdown to Style Bot Messages
  • Notes
  • Changelog

How did I do it?

I started by going through Chit-Chat's docs. These docs contained stuff like About Chit-Chat and Community Guidelines but at the very bottom of the page, there is a tutorial on how to make a Chit-Chat Bot. I skimmed through it and tried it out, only soon to realize it was very, very hard. Then I started making the api that I am talking about right now!

Getting Started

Create a new nodejs repl, and name it whatever! Then, you are going to want to install 2 packages. chit-chat-api.js and node-fetch. You never actually use node-fetch, but for some reason if you don't have it the whole api glitches out. So don't worry about node-fetch, all that is needed is chit-chat-api.js. Next, your going to want to import the package.

const Api = require("chit-chat-api.js");

Next, we need to create a bot. This bot will contain all the information to create an account, like a username and password. This is really simple, as we create the bot and then give it the username and password.

const Api = require("chit-chat-api.js");

const bot = new Api.Bot();
bot.setUsername("Bot_Username");
bot.setPassword(process.env.password);

Creating a new BotUser

This is also really simple, its just one more line of code!

const Api = require("chit-chat-api.js");
 
const bot = new Api.Bot();
bot.setUsername("Bot_Username");
bot.setPassword(process.env.password);

const Bot = bot.createBotUser();

And thats all! The reason we make it a variable is because the function createBotUser() creates and returns a special kind of bot called a BotUser, its very simular to the Bot class. But it creates an actual Chit-Chat account for the bot. Now this may spike some concerns about bot security so I will take a moment to talk about the measure I have taken to prevent bot hacking, although you can skip this and move on to the next section.

Bot Security

This is a big part in making bots, your bot's security is very important. And I want to list some of the measures I have taken to prevent your bot from being hacked.

  • When ever a new bot account is made its password gets encrypted (e.g. your bot's password could be password1234 but with the encryption turn out as ,gss~mrc1v36)
  • Keeping the signup system simular to Chit-Chat's, almost to the point where they are identical.
  • Requiring the same things GameDev46 required when making his send message system.

These are just some of the measures I took to keep your bot safe, and plan to add more security features in the future. I want to specify something, if your bot gets hacked I am not responsible. I have done my part as a developer to help keep bots safe, but at the end of the day it comes down to you. Did you put the password and secret value in a enviromental secret. Did you give your password out to a stranger. It all comes down to you, not me.

Using the .when Event Emitter to Make Commands

Now, this is the fun part. .when is a function (that acts like an event emitter.) built into the bot which allows you to make commands and other cool things. Its really easy to use, but you need to know your events. If we are making a command then we need an event that looks for a new message, the event for this is messageSent. It runs every 1/2 second (500 miliseconds) and returns the data from the server (the message.) and converts it to a sentence.

Bot.when("messageSent", message => {
	//Code
});

Next, inorder to make commands, we need a command handler.

Organizing Commands (command-handling)

I am going to show you the easiest command handler, although it is not efficent it gets the job done.

Bot.when("messageSent" => {
	//Without Args
	if(message === "!ping"){
		Bot.send("Pong!",process.env.secretValue);
	}
	//With Args
	if(message.startsWith("!add")){
		const args = message.split(" ").slice(1);
		if(!args[0]) return;
		if(!args[1]) return;
		if(args[2]) return;
		const x = parseInt(args[0]);
		const y = parseInt(args[1]);
		return x + y;
	}
});

That is the easiest command handler, although it has a lot of issues. I suggest experimenting, find a command-handler you like! This is my favorite part of bot coding because there is so much freedom with it!

My go to Command Handler

Here is my go to command handler:

const prefix = "!";

Bot.when("messageSent", message => {
	if(!message.startsWith(prefix)) return;
	const args = message.slice(prefix.length).split(/ +/);
	const cmd = args.shift().toLowerCase();

	//Command Examples.
	if(cmd === "ping"){
		Bot.send("Pong!",process.env.secretValue);
	} else if(cmd === "add"){
		if(!args[0]) return;
		if(!args[1]) return;
		if(args[2]) return;
		const x = parseInt(args[0]);
		const y = parseInt(args[1]);
		const number = x + y
                Bot.send(number);
	}
});

Using Chit-Chat Markdown to Style Bot Messages

If you don't know how to use chit-chat markdown, there is a great tutorial in the docs, but I will show you how to use markdown real quick.
To make bold text, do **bold**, to make italic text, do ::italic::, and to do a line break, type This is ~~ on a new line!.
So using that knowledge, type those symbols into your reply. I do want to say typing \n in the middle of your message will not make a line break, you have to use ~~

Notes

  • Right now, you can only make new accounts with the api, in the future I plan to make it to where you can also login with an existing bot.
  • To increase security, make enviromental variables for your password and secretValue.
  • Please comment any spelling mistakes, errors in code blocks, or just errors with the api itself in the comments!

Changelog

  • Added a new section.