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

discordjs-variables

v3.2.15

Published

create custom variables for your discord bot, allow users to customize their welcome messages

Downloads

30

Readme

Streamline Discord bot development with discordjs-variables, a powerful npm module built on Discord.js. Easily create dynamic Discord variables and enhance bot interactions effortlessly. Perfect for developers looking to simplify Discord bot programming while adding exciting new features.

Open source

We welcome contributions from everyone!

This package is fully open source, and we're excited to see how you can help make it even cooler.

For information on how to get started, please refer to the contributing guide.

Please adhere to our code of conduct while contributing.

Version 3.0.0

The discordjs-variables package has been significantly enhanced to support a broader range of Discord.js events with improved syntax, making it even easier for developers to integrate into their production-grade applications. Key improvements include:

  • Expanded Event Support: Almost all Discord.js events are now supported, providing comprehensive coverage for various use cases.
  • Simplified Syntax: The syntax has been refined to include type support, enabling faster development and reducing potential errors.
  • Unified Parse Function: A single, powerful parse function simplifies event handling and ensures a consistent approach across your application.

These enhancements streamline the development process, allowing you to focus on building robust and scalable Discord bots with minimal friction.

Table of Contents

  1. Installation
  2. Usage
  3. Explanation
  4. Examples
  5. Change Logs
  6. Further Information

Installation

To install discordjs-variables and its dependencies, run the following command:

npm install discordjs-variables

Usage

Example Setup

const { Transformer, createRule, RuleStore } = require("discordjs-variables");
const { Client } = require("discord.js");

// Define rules
const rules = new RuleStore([
  createRule("{username}", "messageCreate", (message) => {
    return message.author.id;
  }),

  createRule("{name}", "messageCreate", (message) => {
    return message.author.displayName;
  }),
]);

// Initialize Discord client
const bot = new Client({ intents: ["MessageContent", "Guilds", "GuildMessages"] });

// Initialize Transformer with rules
const transformer = new Transformer({ collection: [rules] });

// Handle messageCreate event
bot.on("messageCreate", (message) => {
  if (message.author.bot) return;

  const content = message.content;
  const parsedContent = transformer.parse(content, "messageCreate", message);

  message.channel.send(parsedContent);
});

// Log in to Discord
bot.login("your token");

Explanation

createRule(identifier: string, event: keyof EventTypes, handler: Function): Rule

  • Purpose: Creates a transformation rule for replacing placeholders in Discord bot messages.
  • Parameters:
    • identifier: Placeholder identifier (e.g., {username}).
    • event: Discord.js event type (e.g., "messageCreate").
    • handler: Function handling the event to return the replacement value.
  • Returns: A Rule object defining the transformation rule.

RuleStore(rules: Rule[]): RuleStore

  • Purpose: Manages multiple transformation rules for Discord bot messages.
  • Parameters:
    • rules: Array of Rule objects.
  • Returns: A RuleStore instance for organizing and applying rules.

Transformer(options: TransformerOptions): Transformer

  • Purpose: Parses Discord bot messages and applies transformation rules.
  • Parameters:
    • options: Configuration options for initializing the Transformer.
      • collection: Array of RuleStore instances containing rules.
  • Returns: A Transformer instance configured with specified rules.

Examples

RuleStore

The RuleStore class was created to offer the ability to organize all of your rules for your dynamic Discord variables. It offers types to some extent. However, when you add more rules of different types, you may lose all TypeScript types and encounter errors. To solve this problem, the createRule helper function was created.

Example without createRule:

const rules = new RuleStore([
  {
    identifier: "{username}",
    event: "interactionCreate",
    definition: (interaction) => {
      return interaction.member?.user.username;
    },
  },
]);

Example with createRule to maintain TypeScript types:

const rules = new RuleStore([
  createRule("{username}", "interactionCreate", (interaction) => {
    return interaction.member?.user.username;
  }),
]);

Transformer

The Transformer does exactly what it sounds like: it contains methods for parsing strings. This class is responsible for parsing variables efficiently and effectively.

const transformer = new Transformer({
  collection: new RuleStore([]),
});

or...

const transformer = new Transformer({
  collection: [new RuleStore([]), new RuleStore([])],
});

To parse a string:

transformer.parse("[string to parse]", "[The Event type]", eventObject);

For more complex parsing:

transformer.parse(text, "eventType", eventObject1, eventObject2, etc...);

Unlike in version 2, where the package was not fully stable or completed, the parser in the current version now uses regex to handle string parsing behind the scenes. This ensures it will always match an identifier, no matter how complex it is within the string.


Change Logs

Latest Version: discordjs-variables v3.0.0

In version 3, the Converter class was removed and replaced by the Transformer class to make the package more scalable. The previous Converter class had all the event parsers defined as methods, which was hard to manage. The new Transformer class features a powerful single parsing method, streamlining the parsing process and enhancing maintainability.

For more detailed information on the changes, visit the changelog on GitHub.

Further Information

For more details and advanced usage, please refer to the GitHub repository.