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

@chiruso/knub

v30.0.0-beta.47

Published

A bot framework for Discord

Downloads

3

Readme

Knub

Knub is a JavaScript/TypeScript framework for creating Discord bots.

High-level overview

A bot built on Knub consists of one or more plugins. For each plugin context (e.g. a Discord server ("guild")), Knub will create a new instance of each plugin that should be loaded for the context. A plugin context is usually just one server, but in the future it can also be a group of servers.

A plugin is simply a plain object that conforms to the PluginBlueprint interface.

Each plugin has access to a set of utility modules for setting up commands, event listeners, user configuration, etc. Plugins are also able to specify public interfaces that other plugins can use for interoperability, as well as plugin dependencies.

The primary goals for Knub are:

  • Safety first — Knub aims to make it easy to write safe code by default
  • Predictability — any "magic" within Knub should be easy to reason about, and ideally the magic is left out in the first place
  • Automatic context-awareness — guild plugins only have access to the context of the guild (server) they're loaded in by default
  • Extensive built-in functionality — for common bot requirements

Documentation

The documentation for Knub is currently a work in progress. For now, see the examples below and browse projects built on Knub, such as Zeppelin.

Examples

For simplicity, the examples below only showcase a very limited subset of Knub's features. Full examples for loading per-context configuration, advanced commands, event handling, global plugins, etc. is coming in the near future.

TypeScript example

import Eris from "eris";
import { Knub, typedGuildPlugin, typedGuildCommand, BasePluginType } from "@chiruso/knub";

interface CustomPluginType extends BasePluginType {
  state: {
    counter: number;
  };
}

// We use a type helper, typedGuildCommand(), here to allow TypeScript to infer argument types and other types within the command object ("blueprint")
// See the JavaScript example further below for an example that uses plain objects instead!
const CounterCommand = typedGuildCommand<CustomPluginType>()({
  trigger: "counter",
  // Permission requirement must always be specified,
  // even if explicitly to mark the command public as done here
  permission: null,
  run({ message, pluginData }) {
    // Type of `pluginData.state.counter` is `number`
    message.channel.createMessage(`Counter value: ${++pluginData.state.counter}`);
  },
});

// Another type helper here: typedGuildPlugin()
const CounterPlugin = typedGuildPlugin<CustomPluginType>()({
  name: "counter-plugin",

  commands: [
    CounterCommand,
  ],

  onLoad({ state }) {
    // Initialize counter for CounterCommand
    state.counter = 0;
  },
});

const client = new Eris("my-bot-token");
const knub = new Knub(client, {
  guildPlugins: [
    CounterPlugin,
  ]
});

knub.run();

JavaScript example

This example doesn't use the type helpers used in the TypeScript example above, and instead uses plain objects wherever possible.

const Eris = require("eris");
const { Knub, baseCommandParameterTypeHelpers } = require("knub");

const t = baseCommandParameterTypeHelpers;

const MyCommand = {
  trigger: "echo",
  signature: {
    text: t.string(),
  },
  permission: null,
  run({ args, message }) {
    message.channel.createMessage(args.text);
  },
};

const OtherCommand = {
  trigger: "ping",
  permission: null,
  run({ message }) {
    message.channel.createMessage("Pong!");
  },
};

const MyPlugin = {
  name: "my-plugin",
  commands: [
    MyCommand,
    OtherCommand,
  ],
};

const client = new Eris("my-bot-token");
const knub = new Knub(client, {
  guildPlugins: [
    MyPlugin,
  ]
});

knub.run();