@beathan/collie

A very small framework for creating a discord bot. It tries to take care of infrastructure so you can focus on creating the actual functionality you want to provide.

Usage no npm install needed!

<script type="module">
  import beathanCollie from 'https://cdn.skypack.dev/@beathan/collie';
</script>

README

what it has to offer

This framework focusses on the infrastructure. It uses discord.js and allows for easy and dynamic constructing of the help command. This is possible by regestering methods to the bot, in a similar fashion to using routes in express. It also allows for more readable and organized code. Furthermore it takes care of checking wether a message is a command or not. This saves time for you as developer to work on the actual functionality of the bot, rather than it's infrastructure.

And now: let me explain how you would go about using this framework.

content

installation

To install as a package you can use yarn:

yarn add @beathan/collie

or npm:

npm i @beathan/collie

To initailize this bot the following code can be used

const bot = new Bot({
    token: process.env.BOT_TOKEN as string,
    helpMessage: {
        author: "author-name",
        title: "bot-name",
        description: "here are all my commands:",
        Thumbnail: "url-to-thumnail",
        color: "#fcba03",
    },
});

command using bot.use

With this example code the bot listens for the 'emoji' action, it will then respond with the emoji given as the argument 'emoji'. If the argument is invalid (not given) it wil respond by replying an error message.

bot.use("emoji", {
    description: "gives the emoji as a reaction",
    args: new Args([{ name: "emoji", optional: false }]),
    method: (command: Command, response: Reponse) => {
        const emoji = command.getArg("emoji");

        if (emoji) {
            response.setType(MessageType.React).setMessage(emoji).send();
        } else {
            response
                .setType(MessageType.Reply)
                .setMessage("no valid arguments given")
                .send();
        }
    },
});

this is great for small bots, but for bigger projects it can be usefull to group commands.

commands using bot.useGroup

const testgroup = new Group("test commands!");

testgroup.use("emoji", {
    description: "gives the emoji as a reaction",
    args: new Args([{ name: "emoji", optional: false }]),
    method: (command: Command, response: Reponse) => {
        const emoji = command.getArg("emoji");

        if (emoji) {
            response.setType(MessageType.React).setMessage(emoji).send();
        } else {
            response
                .setType(MessageType.Reply)
                .setMessage("no valid arguments given")
                .send();
        }
    },
});

testgroup.use("test2", {
    description: "say test2",
    method: (command: Command, response: Response) => {
        response.setType(MessageType.ChannelEmbed).setTitle("test2!").send();
    },
});

bot.useGroup(testgroup);

Ideally one would seperate the group, methods and use statement into seperate files/folders. One way to do this is:
--src
----controller
--------x.controller.ts
----group
--------x.group.ts

where in our example the content of the files would look like this:
test.group.ts

import { methods } from "location-of-methods";

const testgroup = new Group("test commands!");

testgroup.use("emoji", {
    description: "gives the emoji as a reaction",
    args: new Args([{ name: "emoji", optional: false }]),
    method: methods.emoji(),
});

testgroup.use("test2", {
    description: "say test2",
    method: methods.test2(),
});

test.controller.ts

export const methods = {
    test2: (command: Command, response: Response) => {
        response.setType(MessageType.ChannelEmbed).setTitle("test2!").send();
    },

    emoji: (command: Command, response: Reponse) => {
        const emoji = command.getArg("emoji");

        if (emoji) {
            response.setType(MessageType.React).setMessage(emoji).send();
        } else {
            response
                .setType(MessageType.Reply)
                .setMessage("no valid arguments given")
                .send();
        }
    },
};

and finally our main.ts file:

import { testgroup } from "location-of-group";

bot.useGroup(testgroup);

the bot now knows wich group the methods belong to and shall show them grouped when the help command is used.