1.0.3 • Published 3 years ago

replblog.js v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

replblog.js Basic Documentation

So, you're looking for something to make your Repl.it blog chat bot easier? Well, you came to the right place! The code, originally made by programmeruser, was forked by muchtek and put into an easy-to-use npm package just for you! This package is strictly for making repl.it blog chat bots in node.js, please keep this in mind!

Easy Setup (Getting Started)

replblog.js is essentially made to facilitate the making of repl.it blog chat bots. To get started, you will need some packages and certain defined variables:

"use strict";
const fetch = require('node-fetch');
const db = require('quick.db');
const ms = require('parse-ms');
const net = require('net');
const ReplBlogJS = require('replblog');
const bot = new ReplBlogJS();
const prefix = '.';
const user = process.env.REPL_OWNER;

Ok, this gives us some basic variables and packages that we can use. The next thing you want to do is create a sendingHelp variable and an async function to prevent spam

let sendingHelp = false;

async function stream(text) {
	if (text.length <= 252) {
		await bot.send(text);
	} else {
		for (let i = 0; i < text.length; i += 252) {
			await bot.send(text.slice(i, i + 252));
			await wait(500);
		}
	}
}

Whew! We have the basics, but we still haven't given the bot any commands! To do that, we need an on message detect function that will be triggered when a user sends a certain message that begins with the prefix that we set above. We should also take this time to make new variables that we will need for commands.

bot.onMessage(msg => {
	if (msg[0] === prefix) {
		if (sendingHelp) return;
		const args = msg.slice(1).split(' ');
		const command = args.shift();
    }
}

You may have noticed that if(sendingHelp) return;. This just means that if sendingHelp = true, then do nothing. The reason for this (and for the await stream function) is because sometimes help messages get soooo big that sending it in 1 message could cause some problems. Therefore, the bot must send it in multiple messages, with cooldown intervals in between. Wouldn't it be silly if you could send the bot a command while it was still sending the full help message?

Anywho, on to the fun part. Using the command variable that we just created, we can now use a switch/case method to make all the commands we need! For now, i'll make a ping command just so you can see how it works:

switch (command) {
      case 'ping':
          bot.send('Pong!');
          break;
}

Remember, this all goes in the on message detect function, so don't put this outside!

Now, we will create the long-awaited help command, and the default if the bot cannot find the command you ask for. In the help command, you will have to change sendingHelp, and instead of using bot.send() you should use the stream() function we created earlier to prevent spam. You will also create a default: if the bot cannot find the command that you ask for.

case 'help':
	sendingHelp = true;
	stream(`.ping - Responds with 'Pong!'
    .help - The command you are running right now!`).then(() => sendingHelp = false);
	break;

default:
	bot.send("I don't know what that command is!");

Yay! The only thing left is running the actual bot! You can do this with a simple line (outside of the on message detect function, of course!) of code:

bot.run();

Wow, that took a long time, but you are finally through it all! Below I will post the finished code so that you can look through each part.

Final Code

Here is the finished code put together from the basic bot we constructed above:

"use strict";
const fetch = require('node-fetch');
const db = require('quick.db');
const ms = require('parse-ms');
const net = require('net');
const ReplBlogJS = require('replblog');
const bot = new ReplBlogJS();
const prefix = '.';
const user = process.env.REPL_OWNER;

let sendingHelp = false;


async function stream(text) {
	if (text.length <= 252) {
		await bot.send(text);
	} else {
		for (let i = 0; i < text.length; i += 252) {
			await bot.send(text.slice(i, i + 252));
			await wait(500);
		}
	}
}

bot.onMessage(msg => {
	if (msg[0] === prefix) {
		if (sendingHelp) return;
		const args = msg.slice(1).split(' ');
		const command = args.shift();

		switch (command) {

            case 'ping':
                bot.send('Pong!');
                break;

			case 'help':
				sendingHelp = true;
				stream(`.ping - Responds with 'Pong!'
                .help - The command you are running right now!`).then(() => sendingHelp = false);
				break;

			default:
				bot.send("I don't know what that command is!");
		}
	}
});

bot.run();

Notices

  • the bot will only respond to GET requests. This means that you should have some sort of client (like mango) that has specific integration for your bot, something like if message starts with 'your epic prefix' then send the message as a GET request. Otherwise, the bot will not respond to your messages.

  • I am looking for people who are willing to do some JS projects with me, because I'm sick of coding alone :P

1.0.3

3 years ago

1.0.2

3 years ago