Coder Social home page Coder Social logo

discord.ts-cogs's Introduction

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 : ChatInputCommandInteraction)
    {
        let arg1 = interaction.options.getString("arg1");
        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 : ChatInputCommandInteraction)
    {
        const a = interaction.options.getNumber("a");
        const b = interaction.options.getNumber("b");
        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.Guilds, 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, addition, and handling of slash commands. Checkout the complete example code for better understanding.

discord.ts-cogs's People

Contributors

disuqi avatar

Stargazers

Shadow avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.