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

@medallyon/djs-framework

v1.2.0

Published

This framework wraps around the Discord.js package to make creating a bot with Slash Commands as easy as possible.

Downloads

2

Readme

djs-framework

This framework wraps around the Discord.js package to make creating a bot with Slash Commands as easy as possible. The main purpose is to remove the strain of trying to figure out how to use Slash Commands.

Contributing

Please submit issues if you find a bug, have some valuable feedback or feature requests, or if you have a question. Feel free to create a pull request if you would like to directly contribute to the project!

Getting Started

First things first, require this package:

const { DiscordCommand, DiscordEvent } = require("@medallyon/djs-framework");

Before using any of the examples below, we also need to create a Discord.js Client instance, as per the documentation:

const { Client, Intents } = require("discord.js");

// Create the client
const client = new Client({
    // Don't forget your Intents
    intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES ]
});

// You can call login whenever
client.login(process.env.DISCORD_TOKEN)
    .then(() => console.log("I'm logged in!"))
    .catch(console.error);

Discord Events

Setup event listeners and pass in middleware functions. See discord.js#Client for a list of events and their arguments.

To setup an event listener for, say, the messageCreate event:

// Create a middleware function for an event
function log({ author, content })
{
    console.log(`${author.username}: "${content}"`);
    // Prints: Medallyon: "There is no way a bee should be able to fly."
}

// Then, create a new DiscordEvent instance and supply the Event Name, the Client, and any Options
new DiscordEvent("messageCreate", client, { middleware: [ log ] });

/**************
       OR
 **************/

// Create your DiscordEvent instance and store it
const onMessageCreate = new DiscordEvent("messageCreate", client);

// And register any middleware after creating your DiscordEvent
onMessageCreate.registerMiddleware(log);

Note that the messageCreate event will become a privileged intent starting April 30, 2022.

Note that events with the same name will be de-duplicated and destroyed.

Slash Commands

You can easily create slash commands with this framework. Here is an example:

// Create a class that inherits DiscordCommand for your chosen Command
class Ping extends DiscordCommand
{
    constructor(client)
    {
        super(client, {
            name: "ping",
            description: "Pong!",

            // The 'interaction' property defines your Slash Command
            interaction: {
                options: [
                    {
                        type: Constants.ApplicationCommandOptionTypes.BOOLEAN,
                        name: "pong",
                        description: "Pong?",
                        required: false
                    }
                ]
            }
        });
    }

    // The 'run' method is always required
    async run(interaction)
    {
        // The bot responds with "Pong!" or "Ping?..." based on if the user supplied the 'pong' option
        const pong = interaction.options.getBoolean("pong") ? "Ping?..." : "Pong!";
        await interaction.reply({ content: pong });
    }
}

// Create an instance of your custom Command
new Ping(client);

Once you're happy with your Command modules, call DiscordEvent.updateInteractions:

// Call this to update your Slash Commands after you have declared all of your custom Commands
DiscordCommand.updateInteractions(process.env.DISCORD_BOT_ID, process.env.DISCORD_TOKEN);

API

DiscordEvent

The constructor for DiscordEvent takes 3 arguments:

  • eventName | <string> - the Event Name to subscribe to. See discord.js#Client for a list of events.
  • client | <Discord.Client> - the Discord.js Client instance that you will use as the main client throughout your project
  • options | <Object> (optional)
    • on | <string> - a choice between "on" or "once". Read up on what this means here.
    • middleware | <Array<function>> - an array of middleware functions that should be executed when this event is triggered

Fields

  • name | <string> - the event name for this event listener
  • client | <Discord.Client> - the Discord.js Client
  • on | <string> - the method of subscription to this event
  • middleware | <Array<function>> - the Array of middleware functions that are run when this event is triggered

Methods

registerMiddleware(cb)

This method adds a function to the list of middleware functions that are executed when the event for a particular event listener is triggered. It takes 1 argument:

  • cb | <function> - the middleware callback function for this event. Any arguments are passed into the function depending on the type of event.
destroy()

This method destroys this event listener. It takes no arguments.

DiscordCommand

The constructor for DiscordCommand takes 2 arguments:

  • client | <Discord.Client> - the Discord.js Client
  • meta | <Object> - info associated with this command
    • name | <string> - the name of the command
    • description | <string> - a description for this command
    • interaction | <Object> - an Application Command Object
    • system (optional) - whether this is a system-only command. If true | <bool>, this command will not be added as a Slash Command.
    • example | <string> (optional) - an example string of how this command might be used

Fields

  • client | <Discord.Client> - the Discord.js Client
  • name | <string> - the name of this command
  • description | <string> - the description for this command
  • interaction | <Object?> - the Application Command object for this interaction

Properties

  • id | <Snowflake?> - the ID of this command

Methods

run(interaction)

When constructing your Command class, you should put any logic for your command in here. This method takes 1 argument: