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

botlightr

v1.1.2

Published

A simple library for making discord.js bot commands.

Downloads

14

Readme

Botlightr

Botlightr is a TypeScript / JavaScript library for dealing with discord.js commands.

Installation

Use the package manager npm to install Botlightr.

npm install botlightr

Setup

Letting Botlightr manage the discord.js Client

import { Bot } from "botlightr";

const myBot = new Bot({ prefix: "!", token: "<your_bot_token_here>" }) // token is optional, you can still pass it in the login method

myBot.login("<your_bot_token_here>") // The token argument is optional since you can set it on the bot first constructor agument

Passing your own discord.js Client

import { Client } from "discord.js";

import { Bot } from "botlightr";

const myClient = new Client();

const myBot = new Bot({ prefix: "!" }, myClient); // You have to pass the client as the second argument

myClient.login("<your_bot_token_here>")

Defining commands

Creating a simple ping-pong command

import { Command } from "botlightr";

export const PingPongCommand = new Command({
    name: "ping",
    args: [],
    async handler({ message }) {
        await message.reply("pong!");
    },
});

Creating a little bit more complex command

import { Command } from "botlightr";

export const SumCommand = new Command({
    name: "sum",
    alias: "s",
    description: "Sums up two numbers",
    args: ["n1", "n2"],
    async handler({ args, message, channel }) {
        const result = parseInt(args.n1) + parseInt(args.n2);

        if (isNaN(result)) {
            return await message.reply("Make shure both numbers are valid!");
        }

        await channel.send(`${args.n1} + ${args.n2} = ${result}`);
    }
});

Error handling

We'll use the sum command from above for the following example. When an error is thrown in the command handler the onError method from the command config will catch it and handle it. Botlightr provides a custom error for handling command syntax errors.

import { Command, CommandSyntaxError } from "botlightr";

export const SumCommand = new Command({
    ...,
    async onError({ error, message }) {
        if (error instanceof CommandSyntaxError) {
            return message.reply(`Please use the command like this ${error.usage}`);
        };
    },
});

Command arguments

Botlightr comes with full native support for typescript. It gives you full intellisense over your code! You can still use botlightr in plain javascript.

export const SayCommand = new Command({
    name: "say",
    args: ["word"], // Unique arg
    async handler({ args }) {
        args.word // Valid
        args.duck // Property 'duck' does not exist on type '{ word: string; }'
    },
});

Register commands

For registering a single command use the following bot instance method

import { PingPongCommand } from "./commands";

myBot.registerCommand(PingPongCommand);

If you want to register multpile commands at once you can use the following bot instance method

// You can find the HelpCommand in the examples folder

myBot.registerCommands([PingPongCommand, HelpCommand]);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.