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

data-custom-id

v0.1.3

Published

Hold data in Discord's Interaction Custom IDs.

Downloads

2

Readme

data-custom-id

npm version

DataCustomId lets you store data inside Discord's Custom ID system. This is useful for storing state within multi-interaction flows (confirm buttons, flows, etc.).

Data is stored using a system similar to URL query strings appended to the provided custom ID.

It's important to note that you can not guarantee data integrity. Custom IDs are sent by the client, and can be modified by users. DO NOT INCLUDE SENSITIVE DATA IN CUSTOM IDS!.

Once instances are created, the raw Custom ID (customId.rawId) is immutable. If you want to keep the current state and change the raw ID, you can create a new instance with the new raw ID, then call newCustomId.copyFieldsFrom(oldCustomId).

| Custom ID | State | DataCustomId (that you'll send to Discord) | |:-------------:|:----------------------------------:|:------------------------------------------:| | ban/confirm | { "user": "42390489028347289" } | ban/confirm?user=42390489028347289 |

Quickstart

See full documentation here.

Install

npm install data-custom-id / yarn add data-custom-id

For TypeScript users, types are included in the package.

Import

// if using node with modules, typescript, or a bundler
import DataCustomId from 'data-custom-id';
// otherwise,
const DataCustomId = require("data-custom-id");

Send a DataCustomId

// create a new DataCustomId
const dataCustomId = new DataCustomId("ban/confirm")
  // add fields (chainable)
  .addField("user", "42390489028347289")

await interaction.reply({
  content: "Are you sure you want to ban this user?",
  components: [
    new MessageActionRow().addComponents(
      new MessageButton()
        .setLabel("Confirm")
        .setCustomId(dataCustomId.toString())
        .setStyle("DANGER")
    )
  ]
})

Receive a DataCustomId

This example uses "path parts", which is your raw ID split by /.

// assuming you're using discord.js, but you don't have to
client.on("interactionCreate", (interaction) => {
  // only message components have custom IDs
  if(!interaction.isMessageComponent()) {
    return;
  }

  // new DataCustomId(id) parses fields from the string id if present
  // using the example from before, let's say the custom id is:
  // ban/confirm?user=42390489028347289
  const dataCustomId = new DataCustomId(interaction.customId);

  // pathParts splits the rawId by /
  // if you need, you can also use dataCustomId.rawId, which is the custom id without any data
  switch(dataCustomId.pathParts[0]) {
    case "ban": {
      // the `|| ""` is in case the id is just `ban`
      switch (dataCustomId.pathParts[1] || "") {
        case "confirm": {
          const userId = dataCustomId.getStringField("user"); // 42390489028347289
          // ban the user
          break;
        };
        // ... (more cases for ban/*)
      }
      break;
    }
    // ... (more cases for *)
  }
})

Copy fields between DataCustomIds

// new DataCustomId(id) parses the id and handles fields
const customId = new DataCustomId(interaction.customId)
// customId.getFields() -> { "user": "42390489028347289", "reason": "spam" }

const newCustomId = new DataCustomId("...")
  .copyFieldsFrom(customId)
// newCustomId.getFields() -> { "user": "42390489028347289", "reason": "spam" }

Tell me more

What can I store?

  • Boolean values (true, false)
  • Integers and integer arrays (1, 2, 3) or [1, 2, 3]
  • Strings and string arrays ("hello", "world") or ["hello", "world"]

How do I get the data back?

You can use convenience methods like:

  • getStringField / getStringArrayField
  • getNumberField / getNumberArrayField
  • getBooleanField

Or, you can use getFields to get all fields, however the convenience methods handle types, so "1" is converted to 1, for example.

Full documentation

See full docs here.