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

disbot-utils

v1.0.0

Published

Various utilties for running a Discord bot as well as for offline and integration testing

Downloads

1

Readme

PsuedoDiscord.js

A small library for simulating the Discord.js npm module for creating bots for Discord. Imitates a server via the command line (via stdin and console.log). Intended to be a drop-in replacement for Discord.js though it has very limited functionality at the moment.

botwrapper.js

Some useful utility functions for bot development with Discord.js:

  • Regular expression for a standard command format
  • A message sender that accepts an array of strings and tries to send as few messages as possible to fit within the character limit (2000) while still breaking across boundaries nicely (message and newline boundaries)
// The following are intended behaviour and not tested

// Input of two strings (demonstrating breaking across input array boundaries)
'Donna toki demo ... Shinjiteiru no\n' // Assume this is exceeds limit of 2000 right at the end of ...
'Kono sekai wo, jibun no yume wo' // Length as is
// Outputs three messages, note that these are implicitly with a newline at the end (since messages appear on different lines)
`Donna toki demo ...`
` Shijiteiru no\n`
`Kono sekai wo, jibune no yume wo`


// Input of three strings (demonstrating combining)
'mukaekirenai koudou ha'
'hazimari no aizu datte sa'
'mada nemutta mama de iru omoi wo okosou'
// Output one message
`mukaekirenai koudou ha\nhazimari no aizu datte sa\nmada nemutta mama de iru omoi wo okosou`

// Input of one strings (demonstrating breaking across new lines)
'osorezu ni ite ...\nkono sekai wo susumu...\njikan wo' // Assume this exceeds limit of 2000 at the end of second ...
// Output two strings
`osorezu ni ite ...`
`kono sekai wo susumu...\njikan wo`
  • So the primary function included in this library is allow a node bot to include libraries that are being developed on without having to shut down the bot and reinterpret. The bot.js main file itself will have to be recompiled if any changes are made to it, however any libraries loaded via this method are read using the native module 'fs' (filesystem) and then evaluated at runtime. A bit of additional working (shown below) has to be down to make work around require's caching and eval() changing how pathnames are resolved, but other than that it should work as intended. This allows for fast iteration in the development process.
// In your main 'bot.js' file
//var IS_DEVELOPMENT = process.argv[2] && process.argv[2].trim().toLowerCase() === 'development';
var IS_DEVELOPMENT = true;

// If development, allow dynamically load commands at runtime for rapid testing
var path = require('path');
var wrapper = require('./botwrapper.js')
var dynamicImports = wrapper.conditionalLoader(IS_DEVELOPMENT, {
  commands: path.resolve('./src/commands.js'),
  utils: path.resolve('./src/utils.js'),
});
// If {IS_DEVELOPMENT} then read code from file on every command execution
// else if production then just load the code once
dynamicImports.staticLoadIfNotDev();

client = new Discord.Client();
client.login(/* token */);
client.on('message', function () {
  // Dynamic load returns a promise that will evaluate whether it is dynamically loaded from filesystem or statically via require normally (as per normal)
  Promise.all(extraModules.dynamicLoadIfDev()).then(function () {
    // Perform all your normal code in here
    dynamicImports.commands['ping'](); // Yay, using
    dynamicImports.utils.example();    // commands imported
  });
});
// In the files loaded via the filesystem you need the following before each require
// Need to delete entry from cache for non-native modules as it caching can cause a require() to load the wrong module 
if (IS_DEVELOPMENT) { // Don't need this if statement if you always intend to load via the filesystem
  delete require.cache['./src/examplelibrary.js']; // Likely will have to do path.resolve() or require.resolve();
  delete require.cache['./src/examplelibrary2.js'];
}
var example = require('./src/examplelibrary.js');
var example2 = require('./src/examplelibrary2.js');
```