npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord-bmaker

v0.1.2

Published

A discord bot maker based on the package discord.hsp

Downloads

1

Readme

discord-bmaker

discord-bot is a framework to build bots for DiscordApp built on discord.js. It has been made in a modular way, so you can add your own stuff at each level of the stack.

Here is an hello world using discord-bot :

var Bot = require('discord-bmaker');

var bot = new Bot({
	token: "Token"
});

bot
	.on(bot.triggers.command, 'hello')
	.do(function(bot, conf, args) {
		this.reply('world');
	});

bot.connect();

A bot It is built using 3 types of components :

  • The triggers are what the bot reacts to, matching a message content for example.
  • The tasks are what the bot does when a trigger gets executed, like posting a message
  • The services are shared singletons that are registered to the bot, allowing triggers and task to use them.

Bot

Properties

client

The DiscordApp client (discord.js), for documentation, refer to discord.js docs.

Methods

connect(callback)

Connects the bot to discord. callback is called when the bot is ready.

on(trigger, [trigger arguments...])

Returns the trigger you just added.

Add a trigger to the bot and return it so it can be further configured.

addService(serviceName, Service)

Add a service to the bot (see below for details);

hasService(serviceName)

Is there a service registered under this name ?

getService(serviceName)

Get the service registered under this name or undefined if not found

Triggers

These are the things your bot reacts to. Here are the ones that are built-in :

Now

This is the most basic command, it executes once when the bot is connected.

bot.on(bot.triggers.now)

Methods

do(callback)

Attach a task for this trigger, ie the callback that will be executed when it is triggered. The arguments passed to the callback are:

  • the instance of BotMaker this trigger have been attached to
  • this trigger
  • arguments (trigger dependant) if any

forEach(array)

Each time the trigger is executed, it will call its task for each given item, the item is then available in the helper inside the task as forEachItem :

bot
	.on(bot.triggers.now)
	.forEach([1, 2, 3])
	.do(function(bot, conf, args) { /* <- this is the task */
		console.log(this.forEachItem);
	});

/* ->
1
2
3
*/

forEachServer()

Same as forEach, for all the servers this bot has access to.

forEachUser()

Same as forEachServer, for users.

addHelper(helperName, helperFactory)

Adds a helper, that will then be available in the task as this.<helperName>. The factory will be called with :

  • the instance of BotMaker
  • arguments (trigger dependant) if any
  • a callback that should be called with the actual helper

Here is a simplified version of the store helper as an example :

trigger.addHelper('store', function(bot, args, done) {
	userStore.get(args.message.author, function(data) {
		done(data);
	});
})
.do(function(bot, conf, args) {
	this.store // will contain the data fetched above
});

React

Extends Now.

Is executed when a bot sees a message that match given pattern :

bot.on(bot.triggers.react, /hey|hello/)

The message that triggered the bot will be available in the args params of your task :

bot
	.on(bot.triggers.react, /hey|hello/)
	.do(function(bot, conf, args) { /* <- this is the task */
		console.log(args.message);
	});

Methods

withStore()

Enables the store helper for this trigger. The store service needs to be setup (see below)

restrict({
	/* at least one of */
	serverId: serverId or array of serverIds,
	channelId: channelId or array of channelIds,
	userId: userId or array of userIds
})

This trigger will only be executed if the message that triggered it matches the rules above. Ie come from a server listed above.

Available helpers

reply(string) reply to the original message

store if enabled (see above)

Command

Extends React.

Is executed when the specified command in entered. A command is a message that starts with a ! then the command keyword. ie: !hello.

bot.on(bot.triggers.command, 'hello', [argumentNames])

Argument parsing

bot
	.on(bot.triggers.command, 'say', ['word'])
	.do(function(bot, conf, args) {
		this.reply(args.commandArgs.word);
	});

//-> !say hello
//<- hello

Arguments are separated by spaces and multi words arguments can be double-quoted. Quotes can be escaped ie :

  • argument
  • "i contain spaces"
  • "He said \"hello\""

If no [argumentNames] is given, args.commandArgs will be an array. [argumentNames] can contain mentions, ex ['word', '@user'].

Methods

describe(description)

Set a description for this trigger, allowing for example the help package to read it and expose it as a !help command. Description can be either a string or {usage: string, description: string}

MentionCommand

Extends Command.

Is executed when someone mentions the bot along with a command keyword, ie @bot print-help. Users can talk to the bot via private message. If so the mention is not nescessary.

bot
	.on(bot.triggers['mention-command'], 'say', ['word'])
	.do(function(bot, conf, args) {
		this.reply(args.commandArgs.word);
	});

//-> @bot say hello
//<- hello

Cron

Extends Now.

Is executed each time the given cron expression ticks.

bot
	.on(bot.triggers.cron, '* * * * *')
	.do(function(bot, conf, args) {
		// do stuff
	});

// will be executed every minute

See here for more details on cron syntax.

Create your own trigger

You can easily create your own trigger by extending an existing one, Now being the simplest. Typically you will override the run(bot) method to control when the trigger is executed.

Here is React implementation on run as an example :

React.prototype.run = function(bot) {
	var that = this;

	bot.client.on('message', function(message) {
		if (!message.content.match(that.pattern))
			return;

		that.execute(bot, { message: message });
	});
};

It can then be attached to the bot like any other :

var myTrigger = require('./my-trigger');

// bot initialisation

bot.on(myTrigger, firstTriggerContructorArgument, secondTriggerContructorArgument...)

Services

Services are registered to the bot via bot.addService(name, service). Once registered, they can be accessed everywhere the bot instance is available via bot.getService(name).

Help

This component is used by Command triggers to register their synopses so a help Command can print the list of available commands.

bot.addService('help', bot.services.help);

Methods

add(synopsis)

get() returns all the registered synopses

Store

The stores services are a convenient way to store and retrieve data about Discord users.

They provides these methods :

get(User, callback)

Retrieve data about a given User object (from discord.js) and callback(data). If no data if found on the storage layer, an object like { id: userId } will be passed to the callback.

set(User, data, callback)

Save data for a User.

getAndUpdate(User, callback)

Same as get, but adds a done() callback as a second parameter so the data can be saved back when modifications are done :

store.getAndUpdate(user, function(data, done) {
	data.someCounter++;
	done();
});

MongoDb

MongoDb backed store.

bot.addService('store', bot.services['mongo-store']('mongodb://localhost/discord'));