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.ts-cogs

v1.0.1

Published

A discord.js extension which introduces Cogs (inspired by discord.py), it also has decorators to facilite command creations, plus a new way to handle and call commands.

Downloads

56

Readme

Intro

This is a TypeScipt package and extension for the discord.js library, which introduces cogs for discord.js (inspired by discord.py). This package also includes a new way to handle commands, thorugh decorators and new classes that encapsulate the command logic.

Installation

To install this package, run the following command: npm install discord.ts-cogs

Usage

Creating a cog

To create a cog, you need to create a class that extends the Cog class. Within your cog class use the decorators @Cog.command and @Cog.argument, to specify which functions are slash commands. In the @Cog.command decorator you can specify the name and description of the command, however this is not a requirement, if you do not specify a name, it will default to the function name, and if you do not specify a description it will default to "...". In the @Cog.argument decorator you can specify the name, type, description and if the argument is required of the argument, the name is the only requirement. The type is an ArgumentType enum; that will default to being a string, the description will default to "..." and if the argument is required will default to false.

Example of a Cog

import { Cog } from "discord.ts-cogs";

class MyCog extends Cog 
{
    //This will create a /ping command, and it will have a "..." description
    @Cog.command()
    async ping(interaction : CommandInteraction)
    {
        await interaction.reply("Pong!");
    }

   //You can also specify the name of the command and give it a description, in this case the command will be /say with the description "Make the bot say something"
    @Cog.command("say", "Make the bot say something")
    //For arguments add the argument decorator, and you can specify the name, type and description of the argument
    @Cog.argument("arg1", ArgumentType.String, "What should I say?")
    async botSay(interaction : CommandInteraction)
    {
        let arg1 = interaction.options.data[0].value as string;
        if(!arg1)
            arg1 = "You did not tell me what to say";
        await interaction.reply(arg1);
    }

    //You can have multiple arguments of different types, and they will be displayed in the order they are declared. The following arguments are set to be required.
    @Cog.command()
    @Cog.argument("a", ArgumentType.Number, "First number", true)
    @Cog.argument("b", ArgumentType.Number, "Second number", true)
    async add(interaction : CommandInteraction)
    {
        const a = interaction.options.data[0].value as number;
        const b = interaction.options.data[1].value as number;
        await interaction.reply((a + b).toString());
    }
}

Adding a cog to the bot

To add the cog you will need to use the CogsClient, it has a method called addCog which takes a cog object as a parameter. This function will automatically add the commands to the bot. The CogsClient inherits from the discord.js Client class, therefore, it is the same class, but with a few extra features. This client also contains a syncCommands method, which will sync the slash commands with the discord api, this will automatically sync cog commands, however, the cog will need to be added first.

const client = new CogsClient({intents: [GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]});

client.once(Events.ClientReady, c => {
	console.log(`Ready! Logged in as ${c.user.tag}`);
});

client.addCog(new MyCog());

//sync with a specific guild/server
client.syncCommands(token, appid, guildid);
//or sync with all guilds/servers
client.syncCommands(token, appid);

client.login(token);

Handling commands

This package also introduces a new way to handle commands. The CogsClient has a Collection<string, SlashCommand>, where the key is the name of the command, and the value is a SlashCommand object which is a new class specific to this package, this class has a call function, which will call the function from the cog parent object.

client.on(Events.InteractionCreate, async interaction => 
	{
		if (!interaction.isCommand()) return;

		const command = client.commands.get(interaction.commandName);
		if (command)
			await command.call(interaction);
	});

Benefits of using Cogs

Cog can have properties which can be used in the command functions, and shared across functions within that cog, as well as other classes, if the instance of the cog is given. Furthermore, it facilitates the creation, addittion, and handling of slash commands. Checkout the complete example code for better understanding.