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

@trixis/lib-ts-bot

v0.3.4

Published

Package for Discord bots micro-framework template - https://github.com/TrixiS/base-ts-bot

Downloads

129

Readme

lib-ts-bot

Package for Discord bots micro-framework template - https://github.com/TrixiS/base-ts-bot

Used in base-ts-bot

Documentation

Built ontop Discord.js

Concepts

The library provides several base classes:

  • Extensions - classes for storing commands and event listeting (see BaseExtension ABC)
  • Commands - classes for incapsulating command data and interaction event handlers (see BaseSlashCommand ABC]

Decorators

The library uses TypeScript decorators for event listener registration. Also provides some decorators for different use cases:

  • commandHandler - a decorator used in BaseSlashCommand subclasses to register command interaction event listeners
  • eventHandler - a decorator used in BaseExtension subclasses to register Discord.js Client event listeners

Checks

Checks are decorators used to register command guard predicates. There are several check decorator factories:

  • commandCheckFactory
  • commandHandlerCheckFactory
  • eventHandlerCheckFactory

Client

Own subclass of Discord.js Client is used

import { BotClient } from "@trixis/lib-ts-bot";

const client = new BotClient({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

...

client.login(process.env.BOT_TOKEN);

BaseExtension

import { Message } from "discord.js";
import { BaseExtension, eventHandler } from "@trixis/lib-ts-bot";

class TestExtension extends BaseExtension {
  @eventHandler("messageCreate")
  async messageCreateHandler(message: Message) {
    await message.channel.send({ content: "Hello world" });
  }
}

...

await client.registerExtension(TestExtension);

...

CommandContext

An object with command contextual data. Instances are created with BaseSlashCommand.getContext method. Command handler callbacks get it as the first argument

type CommandContext<
  I extends CommandInteraction = CommandInteraction,
  O extends Record<string, any> = Record<string, any>,
  D extends Record<string, any> = Record<string, any>
> = {
  client: BotClient;
  interaction: I;
  member?: GuildMember;
  guild?: Guild;
  options: O;
  data: D;
};

BaseSlashCommand

import { SlashCommandBuilder } from "discord.js";
import {
  BaseSlashCommand,
  commandHandler,
  CommandContext,
} from "@trixis/lib-ts-bot";

class TestCommand extends BaseSlashCommand<TestExtension> {
  constructor(extension: TestExtension) {
    const builder = new SlashCommandBuilder()
      .setName("test")
      .setDescription("Some test command!");

    super(extension, builder);

  @commandHandler({ name: "test", autoDeferOptions: null })
  async testCommandHandler(ctx: CommandContext) {
    await ctx.interaction.reply("Hello world!");
  }
}

...

testExtension.addCommand(TestCommand); // instance of TestExtension is used (not the class itself)

...

Checks

Check predicate takes a single argument - CommandContext instance. All checks should return Promise.

Command checks

...
import { guildOnlyCommand } from "@trixis/lib-ts-bot";

@guildOnlyCommand() // makes all command handlers of TestCommand guild only (would't work in DMs)
class TestCommand extends BaseSlashCommand<TestExtension> {
  ...
}

...

Command handler checks

...
import { commandHandlerCheckFactory } from "@trixis/lib-ts-bot";

const guildOnlyCommandHandler = () => commandHandlerCheckFactory(async (ctx) => Boolean(ctx.guild));

class TestCommand extends BaseSlashCommand<TestExtension> {
  ...

  @guildOnlyCommandHandler() // makes a single command handler guild only
  @commandHandler({ name: "test" })
  async testCommandHandler(ctx: CommandContext) {
    ...
  }
}

...

Event handler checks

...
import { eventHandlerCheckFactory } from "@trixis/lib-ts-bot";

const guildOnlyEvent = () => eventHandlerCheckFactory(async (ctx) => Boolean(ctx.guild));

class TestExtension extends BaseExtension {
  @guildOnlyEvent() // check if event interaction guild is set
  @eventHandler("interactionCreate")
  async interactionHandler(interaction: Interaction) {
    ...
  }
}

...

Command handling

Commands would not be handled automatically by default. There is defaut CommandHandlerExtension you need to register yourself. Or write it yourself

import { CommandHandlerExtension } from "@trixis/lib-ts-bot";

// ... create a client instance
await client.registerExtension(CommandHandlerExtension);
// ... register your extensions and commands
// ... login the client

Custom id factory

...
import { CustomId, checkCustomId } from "@trixis/lib-ts-bot";

const data = {
  someId: 1
};

const testCustomId = new CustomId<typeof data>("test");

class TestExtension extends BaseExtension {
  @checkCustomId(testCustomId) // would check if interaction custom id is the specified one
  @eventHandler("interactionCreate")
  async interactionHandler(interaction: Interaction) {
    // interaction type has no customId, it is given here for example
    const data = testCustomId.unpack(interaction.customId);
    console.log(data.testId);
    const packedData = testCustomId.pack(data);
    console.log(packedData);
  }
}

...