Skip to content

v4.0.0

Choose a tag to compare

@yetnt yetnt released this 03 Dec 13:22
· 3 commits to main since this release

Github Wiki do not as of yet reflect the current docs of v4.0.0 but are still on v3.0.0
README.md will always have the updated docs however.

Breaking changes

All Handlers NO LONGER extend from Corehandler, and now instead their first parameters do not take a Client instance but a CoreHandler instance.

- const { CommandHandler, ReadyHandler, InteractionHandler } = require("ic4d")
+ const { CommandHandler, ReadyHandler, InteractionHandler, CoreHandler } = require("ic4d")

const client = new Client()

+ const core = new CoreHandler(client)

- const cHandler = new CommandHandler(client, "commands", ...etc )
+ const cHandler = new CommandHandler(core, "commands", ...etc )

And so on and so fourth.

ReadyHandler's second parameter now takes in a parameter called shardClient. If you are NOT sharding, leave as undefined then continue adding params.

const { ReadyHandler, CoreHandler } = require("ic4d")

const client = new Client()

const core = new CoreHandler(client)

- const rHandler(client, () => {}, () => {})
+ const rHandler(core, undefined, () => {}, () => {})

CommandHandler's handleCommands() method takes in 2 parameters instead of 1 rest parameter. Both parameters are arrays of functions

- cHandler.handleCommands(func1, func2, ...etc)
+ cHandler.handleCommands([func1, func2, ...etc]) 

New Stuff

Shared variables between Commands and interactions.

Via the addInteractionVariables() function passed into the execute() function of the SlashCommandManager's constructor object, and variables property passed into the fn() function of the setCallback() method in InteractionBuilder, variables can be passed as key-value pairs!

if that didn't make any sense, here is the docs on it

and here are the types:

type addInteractionVariables = (k: { [key: string]: any }) => void;
type variables = { [key:string]: any } 

and then here's an exmaple code of how you can use it

const test = new SlashCommandManager({
    data: new SlashCommandBuilder()
        .setName("test")
        .setDescription("Just a test")
        .addStringOption((option) =>
            option
                .setName("string")
                .setDescription("some input")
                .setRequired(true)
        ),
    // here we add the function in the parameter list
    async execute(interaction, client, addInteractionVariables) {
        try {
            await interaction.deferReply();
            const itemName = interaction.options.get("string").value;
            await interaction.editReply({
                content: "Huh?",
                components: [
                    new ActionRowBuilder().addComponents(
                        new ButtonBuilder()
                            .setCustomId("hello")
                            .setLabel("click now")
                            .setStyle(ButtonStyle.Primary)
                    ),
                ],
            });

            // using the function
            addInteractionVariables({ itemName });
        } catch (e) {
            errorHandler(e, client, interaction, EmbedBuilder);
        }
    },
}).addInteractions(button);

const button = new InteractionBuilder()
    .setType("button")
    .setCustomId("hello")

    // add the variables to the parameter list
    .setCallback(async (i, c, variables) => {
        // using the variable in the callback
        await i.update({ content: variables.itemName, components: [] });
    });

handleCommands() (from CommandHandler) now takes in a second parameter called postWare which is an array of functions run AFTER a command is run

Unlike middleWare which returns 1, or 0. postWare it doesn't matter what it returns.
Example : (Stolen from the docs)

const handler = new CommandHandler(core, path.join(__dirname, "commands"));

const blacklisted = ["302983482377931"];
const blacklist = (commandObject, interaction) => {
    if (commandObject.blacklist && blacklisted.includes(interaction.user.id)) {
        interaction.reply({
            content: "Daang you blacklisted my guy.",
            ephemeral: true,
        });
        return 1;
    }
    return 0;
};

const addXp = (commandObject, interaction) => {
    if (commandObject.category != "economy") return;
    interaction.reply({
        content: "Ayo! Free Xp +2",
        ephemeral: true,
    });
};

await handler.handleCommands([blacklist], [addXp]);

Sharding!

Well, not quite yet. Although nobody but me uses this package so unless i get a PR or actually start sharding my main bot. I'll update the docs better on this. Right now it should look okay but it's a bit funky.

Docs for sharding

Merged PRs

  • Merge variable scoping into main by @yetnt in #1
  • CoreHandler is no longer a parent class to all the other Handlers by @yetnt in #2

Full Changelog: v3.0.0...v4.0.0