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

djs-structure

v0.0.1

Published

Advanced structure, algorithms, and tools for Discord.JS (v13 and up)

Downloads

4

Readme

djs-structure

Advanced structure, algorithms, and tools for Discord.JS (v13 and up)

If you aren't planning on using message-based commands and will only use slash commands, you can create a bot like this

import {ManagedClient, Module, Command, IncomingCommand} from "djs-structure";

class MyCommand extends Command {
    constructor() {
        super("mycommand", "Description here", [
            // any options here, follows application command option structure, this arg is optional
        ])
    }

    async incoming(i: IncomingCommand) {
        await i.reply("Reply from my command");
    }
}

class MyModule extends Module {
    constructor() {
        super("my-module")
    }

    load() {
        this.registerCommand(new MyCommand());
    }
}

class MyBot extends ManagedClient {
    init() {
        this.initStructure(new MyModule());

        this.login("my-token");
    }
}

const bot = new MyBot();
bot.init();

If you do this, commands, modules, and slash commands are all automatically managed for you. If you want to go more in depth, you can replicate the managed client in [ManagedClient](. /src/client/ManagedClient) and the typings will allow you to use it as long as it has the registerCommand and registerModule methods!

If you wanted to add message-based command support that uses the same codebase as your slash commands, there are built in methods for simulating them. You could do this (typescript):

import {Message} from "discord.js";
import {IncomingCommand} from "djs-structure";

this.on("messageCreate", async (msg) => {
    // check if it starts with the prefix
    if (msg.content.startsWith("!")) {
        // remove prefix
        let content = msg.content.replace(/^!/, "");

        // match name
        const nameMatches = content.match(/^[A-z0-9]/);
        if (!nameMatches || nameMatches.length <= 0) return;

        const cmd = nameMatches[0];

        content = content.replace(new RegExp(`%${cmd} ?`))
        // ^^^ removes command from content and optional space after

        if (this.commands.has(cmd.toLowercase())) {
            const command = this.commands.get(cmd.toLowercase()) as Command;
            if (!command.opts.supportsMsg) return; // by default this is true

            let deferredMessage: Message | undefined = undefined;

            // only defer if really needs to be, without forcelong this will only apply to slash 
            // commands
            if (command.opts.long && command.opts.forcelong) {
                deferredMessage = await msg.reply("Processing...");
            }

            const incoming = new IncomingCommand({
                type: "message",
                client: this,
                command,
                message: msg,
                content,
                deferredMessage
            });

            try {
                await inc.parseCommand(); // parses arguments into application command options
                await command.incoming(incoming);
            } catch (e) {
                // something went wrong, tell the user
                msg.reply(`${e}`);
            } finally {
                if (command.opts.isPublic === false) await msg.delete();
            }
        }
    }
})

If you want to implement your own way to handle incoming commands and emit an incoming event to a command, see the bottom of ManagedClient

More will come in the future!