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-bot-base

v6.0.2

Published

Javascript bot base for Discord

Downloads

29

Readme

discord-bot-base

This is a base bot for Discord using Javascript.

Installation

Requirements

This module requires

Install with NPM

npm install --save discord-bot-base

Usage

The goal of this repository, is to make it simple to create a bot, and add on commands. To get started just create an instance of Bot.

'use strict';

const BaseBot = require('discord-bot-base');
const TestCommand = require('./src/Command/TestCommand')

new BaseBot.Bot('dev', true, {
    admin_id:  '<your user id>',
    email:     '<your bot login>',
    password:  '<your bot password>',
    log_dir:   '<location of logs directory>',
    commands:  [TestCommand],
    prefix:    "!",
});

The arguments passed to Bot are first, the environment (generally 'dev' or 'prod'), debug (adds extra logging), and an array of options.

The keys that are provided to options (admin_id, email, password, commands, and prefix are all required).

Below is a list of options you can pass:

  • name - Name of the bot. This is just for the output on the CLI. Not necessary, and will default to discord-base-bot
  • version - Version of the bot. This is also just for the output on the CLI. Not necessary, and will default to the current version of discord-base-bot
  • author - Author of the bot. This is again, just for the output on the CLI. Not necessary, and will default to the author of discord-base-bot
  • status - Status of the bot. Will show up in the user list as "Playing "
  • email - Login for the bot. This is required.
  • password - Password for the bot. This is required.
  • prefix - Prefix the bot will use for commands. This is required. Try not to use common characters if you plan to be on big servers.
  • admin_id - User ID of the account you want to have access to admin commands. (e.g. the restart command)
  • container - Advanced. For now, if you need to add this, message me in the discord api server. Required to use mysql/mongo/redis stuff.
  • commands - An array of commands. The array should contain modules of commands. Check out an example below of a command.
  • mongo_url - a Fully qualified mongo dsn. e.g. mongodb://localhost/
  • redis_url - a Fully qualified redis dsn. e.g. redis://localhost/
  • queue - An object of config variables for rabbitmq

If you specify queue, you will be able to set up your bot with cluster support, with PM2.

Command Example

Below, is what the Stats command looks like:

const AbstractCommand = require('discord-base-bot').AbstractCommand;

class StatsCommand extends AbstractCommand {
    static get name() { return 'stats'; }

    static get description() { return "Shows the bot stats"; }

    handle() {
        this.responds(/^stats$/g, () => {
            let servers  = this.client.servers.length,
                channels = this.client.channels.length,
                users    = this.client.users.length,
                online   = this.client.users.filter(u => u.status != "offline").length;

            this.reply(
                `Currently joined to: ${servers} servers with ${online}/${users} members and ${channels} text channels.`
            );
        });
    }
}

module.exports = StatsCommand;

Take note of the static getters at the top. These are used for the help command.

All commands are required to extend AbstractCommand. AbstractCommand provides a couple helper methods. A few to note are:

  • responds - Used when you want the bot to respond to a direct mention, or command. Takes a regex, with the second argument being a callback if a match was found. The callback is passed an array of matches.
  • hears - Used when you want the bot to listen for a phrase. Takes a regex, with the second argument being a callback if a match was found. The callback is passed an array of matches.
  • sendMessage - Shortcut for discord.js's client.sendMessage
  • reply - Shortcut for discord.js's client.reply, just pass a message.
  • isThrottled - Lets you throttle commands. Pass a key, and how long (in seconds) to throttle for.
  • handle - This method is required for all commands as well. All your logic should go in here.