0.3.4 • Published 9 months ago

@trixis/lib-ts-bot v0.3.4

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

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);
  }
}

...
0.3.4

9 months ago

0.3.3

1 year ago

0.3.2

1 year ago

0.2.27

1 year ago

0.3.0

1 year ago

0.3.1

1 year ago

0.2.30

1 year ago

0.2.31

1 year ago

0.2.29

1 year ago

0.2.28

1 year ago

0.2.26

1 year ago

0.2.25

1 year ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.21

2 years ago

0.1.19

2 years ago

0.1.18

2 years ago

0.1.17

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago