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

discord-worker-framework

v0.1.0

Published

Discord multiservice worker framework consuming from various message brokers.

Downloads

7

Readme

Discord Worker Framework

js-semistandard-style

A fast, light-weight framework for consuming incoming messages from Discord in a multi-service fashion.

This package consumes from a message broker such as ActiveMQ or RabbitMQ, parses and validates incoming data and emits it to be processed by the bot-specific code.

This package also incorporates a RestClient, which can be used for sending information to the Discord API directly or through a rate-limiting proxy. Ratelimits aren't currently managed by the RestClient.

NOTE: THIS PACKAGE MAY BREAK AT ANY TIME

This package follows semver, and the major version is 0. This means that breaking changes can be introduced in any update. Do not use this in production unless you know what you are doing.

Features

  • Fast
  • Low-level
  • Customizable
  • Connects to any message broker (adapters are included for MQTT and AMQP)
  • Allows for caching (included adapter available for Redis)
  • Allows for collecting statistics via EventEmitter3

TODO

  • [ ] Move documentation to some webviewer
  • [ ] Move usage to examples/ directory and write more examples
  • [ ] Remove the note above when the package major version hits 1
  • [ ] Write tests

Installing

npm install discord-worker-framework --no-optional

If you would like to use the included Redis, MQTT or AMQP adapters, make sure that redis, mqtt and/or amqp.node are installed:

npm install redis mqtt amqp.node

Usage

'use strict';

const DiscordWorkerFramework = require('discord-worker-framework');
const raven = require('raven'); // optional, for RavenWorker only

// Create a MessageBrokerConsumer, check documentation for specifics
const consumer = new DiscordWorkerFramework.MessageBrokerConsumer;

// Create a new Worker
const worker = new DiscordWorkerFramework.Worker({
  messageBrokerConsumer: consumer
});

// Create a new RestClient
const RestClient = new DiscordWorkerFramework.RestClient({
  endpoint: 'http://localhost:8000',
  // authorization: 'value of Authorization header for the endpoint'
});

// ...or a RavenWorker (RavenWorker allows for reporting errors/warnings to
// Sentry)
/*
const worker = new DiscordWorkerFramework.RavenWorker({
  messageBrokerConsumer: consumer,
  ravenClient: raven
});
*/

// Listen for new messages and respond to pings
worker.on('discord:MESSAGE_CREATE', e => {
  if (e.data.content === 'ping') {
    RestClient.createMessage(e.data.channel_id, 'pong!');
  }
});

// Listen for deleted messages and log them... for reasons
worker.on('discord:MESSAGE_DELETE', e => {
  console.log(`[MESSAGE_DELETE] ${e.data.author.username}: ${e.data.content}`);
});

// Start consuming
worker.startConsuming().then(() => {
  console.log('Ready to rumble!');
}).catch(err => {
  console.error(err.stack);
  process.exit(1);
});

Incoming Event Structure

All received events from the MessageBrokerConsumer must follow a standard JSON structure or they will cause the Worker to fail to process incoming events:

{
  "type": "", // Discord event type, e.g. MESSAGE_CREATE, GUILD_CREATE
  "shard_id": 0, // shard ID which the event was received from
  "data": {} // event data, should match the structure from the Discord gateway
}

Events

The Worker currently emits the following events:

  • discord:*: Discord events, see example above
  • event:recv: emitted when any event is received, with the data
  • event:tested: emitted when any event has been verified to match the event structure schema (with result)
  • event:dispatch: emitted when any event is broadcasted to listener (discord:*), with the data

Contributing

  1. Discuss major changes you wish to propose by creating an issue first
  2. Fork, clone, checkout develop, make changes (semistandard codestyle), push
  3. PR to the develop branch

Testing

Tests are handled with mocha. You can run tests by using the npm test command, assuming you've cloned this repository directly and installed devDependencies.

License

A copy of the MIT license can be found in LICENSE.