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

diseact

v4.0.1

Published

A package to make Discord Embeds, Components and Commands using JSX

Downloads

356

Readme

Diseact

Diseact is a Javascript library created to easily build components, embeds and commands from the Discord API using JSX.

Why is it useful?

  • In case you're trying to create an embedded interface, using buttons to navigate through each page of the embed.
  • Unite your components, embeds and other props in the same context.
  • Better organize your commands, and componentize them (separating subcommands in other files).

Use:

const modal = (
  <modal id="myModal" title="My Modal">
    <textinput isShort id="favoriteColorInput">
      What's your favorite color?
    </textinput>

    <textinput isParagraph id="hobbiesInput">
      What's some of your favorite hobbies?
    </textinput>
  </modal>
);

Instead:

const modal = new ModalBuilder()
  .setCustomId("myModal")
  .setTitle("My Modal");

const favoriteColorInput = new TextInputBuilder()
  .setCustomId("favoriteColorInput")
  .setLabel("What's your favorite color?")
  .setStyle(TextInputStyle.Short);

const hobbiesInput = new TextInputBuilder()
  .setCustomId("hobbiesInput")
  .setLabel("What's some of your favorite hobbies?")
  .setStyle(TextInputStyle.Paragraph);

const firstActionRow = new ActionRowBuilder().addComponents(favoriteColorInput);
const secondActionRow = new ActionRowBuilder().addComponents(hobbiesInput);

modal.addComponents(firstActionRow, secondActionRow);

How to Use:

  • Install Diseact on your project:
npm i diseact
  • In your jsconfig.json or tsconfig.json file, set:
{
  "compilerOptions": {
    "jsxFactory": "Diseact.createElement",
    "jsx": "react"
    // ...
  }
}

or use babel:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    [
      "@babel/preset-react",
      {
        "runtime": "automatic",
        "importSource": "diseact"
      }
    ]
  ]
}
  • Import Diseact on your JSX/TSX file:
import Diseact from "diseact";

const myEmbed = (
  <embed>
    <title>Hello from Diseact!</title>
  </embed>
);

If you are using babel with runtime setted to automatic, you don't need to import diseact to use JSX

See the Documentation below:

Embeds

To start, we can learn about creating an embed.

const myEmbed = (
  <embed color="White" timestamp={new Date()}>
    <author url="https://example.com/" iconURL="https://example.com/">
      John
    </author>
    <title>My Embed</title>
    <description>Testing this embed</description>
    <fields>
      <field name="tested" inline>
        true
      </field>
    </fields>
    <image>https://example.com/</image>
    <thumbnail>https://example.com/</thumbnail>
    <footer iconURL="https://example.com/">footer</footer>
  </embed>
);

message.send({ embeds: [myEmbed] });

Components

We can make a functional component that returns a message container. And inside our container, we can place embeds, buttons or select menus. You don't need to pass an action row, Diseact organize components in action row by type.

function MyComponent() {
  <container isMessage tts enforceNonce allowedMentions={{}}>
    <embed>
      <title>My Component</title>
    </embed>

    <button isPrimary label="My button" />

    <selectmenu isString max={1} min={1} placeholder="Select anything">
      <option value="cat" label="Cat">
        It's a cat
      </option>
      <option value="dog" label="Dog">
        It's a dog
      </option>
    </selectmenu>
  </container>;
}

Some properties are shown, but we have more.

ID is optional in every Discord component. If you don't provide an ID, Diseact will generate one and use it.

Hooks

Diseact have two hooks, like useEffect and useState, you can do something like:

function Counter() {
  const [count, setCount] = Diseact.useState(0);

  const handleIncrement = () => {
    setCount((c) => c + 1);
  };

  const handleDecrement = () => {
    setCount((c) => c - 1);
  };

  return (
    <container isMessage>
      <embed>
        <title>Counter</title>
        <description>Count: {count}</description>
      </embed>

      <button
        isSuccess
        id="increment"
        label="Add"
        emoji="➕"
        onClick={handleIncrement}
      />

      <button
        isDanger
        id="decrement"
        label="Reduce"
        emoji="➖"
        onClick={handleDecrement}
      />
    </container>
  );
}

Note: The button has a onClick property, expecting a function with an interaction argument, like it:

const handleButton = (interaction) => {
  return "Success! Button has been pressed";
};

You can return a string or InteractionReplyOptions. Diseact will send it back with interaction.reply. If you don't insert a return value, the interaction will be replied with a void value to avoid Discord sending errors. Like <button/>, <modal/> and <selectmenu/> have functions in parameters too. OnSubmit for modal, and OnSelect for select menu. Working the same way as OnClick.

Rendering

To render our component, we can do:

Diseact.render(channel, <MyComponent />);

The first parameter in the render function, is a target. This property can be a TextChannel, Message or CommandInteraction. The second parameter is a component to render. Easy peazy!

In the first render, Diseact will use channel.send, message.channel.send or interaction.reply to send the first message. After each render, the message is edited with message.edit or interaction.editReply.

Commands

We can use JSX to create Commands:

export default (
  <command name="ping">
    {(interaction) => {
      interaction.reply("Pong!");
    }}
  </command>
);

You can pass subcommands and groups too:

export default (
  <command name="member">
    <subcommand name="ban">
      <user name="target">
      <string name="reason" optional>

      {(interaction) => {
        ...
      }}
    </subcommand>

    <subcommand name="unban">
      <user name="target">

      {(interaction) => {
        ...
      }}
    </subcommand>

    <group name="meta">
      <subcommand name="set">
        ...
      </subcommand>
    </group>
  </command>
)

And Define localizations:

const localizations = {
  name: { "pt-BR": "Olá" },
};

export default <command localizations={localizations} />;

Definition

Use a iteration to read commands and

for (const commandPath of readdirSync("./commands")) {
  const command = (await import(commandPath)).default;
  guild.commands.set(command);
}

Handling

You need to use CommandInteractionExecutor to run command interactions:

client.on("interactionCreate", (interaction) => {
  if (!interaction.isCommand()) return;

  Diseact.CommandInteractionExecutor(interaction);
});