huruhuru - v0.0.1
    Preparing search index...

    huruhuru - v0.0.1

    huruhuru

    Provides a few frequently used utilities for building Discord bots with discord.js v14.

    This package is intended to be distributed as a utility library. It declares discord.js as a peer dependency.

    npm install huruhuru
    yarn add huruhuru
    pnpm install huruhuru
    bun add huruhuru

    Import the helpers from the package entry:

    import { CommandContext, paginate, createSimpleContainer, newAccentColor, makeRow } from "huruhuru";
    

    newAccentColor() — returns a random RGB tuple.

    createSimpleContainer(content, accentColor?) — builds a minimal ContainerBuilder with text.

    makeRow(components) — builds an ActionRow from component builders.

    Example:

    const container = createSimpleContainer("Hello world", newAccentColor());
    const row = makeRow([button1, button2]);

    CommandContext wraps either a ChatInputCommandInteraction or a Message and exposes a unified API for replying/editing/following up. This makes command implementations concise and interaction-agnostic.

    Example:

    const ctx = new CommandContext(interactionOrMessage);
    await ctx.deferReply();
    await ctx.reply("Done");

    Signature: paginate(ctx, items, topSection?, currentPage = 1, pageSize = 10, header?)

    • ctx: a CommandContext (wrapper for an interaction or a message)
    • items: an array of items to paginate. Each item may be a SectionBuilder or any object implementing toString().
    • topSection (optional): a SectionBuilder or any object with toString() that will be inserted at the top of every page when provided.
    • currentPage (optional, default 1): the initial page to show (1-based).
    • pageSize (optional, default 10): number of items per page.
    • header (optional): a text header displayed above the page sections.

    Behavior:

    • When topSection is provided, it is added to the top of each page.
    • When topSection is omitted, pages only contain the item sections.

    Examples:

    const items = ["one", "two", "three", "four"];

    // Skip topSection and specify page arguments
    await paginate(ctx, items, null, 1, 2, "List:");

    // Provide topSection
    await paginate(ctx, items, topItem, 1, 2, "List:");

    Signature: paginateMap(ctx, items, mapper, topSection?, currentPage = 1, pageSize = 10, header?)

    • mapper: a function (item, builder) => SectionBuilder that converts each input item into a SectionBuilder. A fresh SectionBuilder is provided as the second argument for each call.
    • The helper maps the provided items using mapper and then calls paginate with the mapped SectionBuilders.

    Example:

    // Map simple objects into SectionBuilder and paginate
    await paginateMap(
    ctx,
    users,
    (user, builder) => builder.addTextDisplayComponents(t => t.setContent(user.name)),
    "User List",
    1,
    10
    );

    GPL-3.0