Skip to content

jochem-waque/disfluent

Repository files navigation

disfluent

A fluent, Zod-like API for discord.js commands.

Usage

After installing disfluent and adding import d from "disfluent" to a file, defining a command can be as simple as:

d.slashCommand("ping", "Respond with pong").handler(async (interaction) => {
  await interaction.reply("Pong!")
})

More likely than not though, you'll want to add subcommands, options, choices and autocomplete, which are all supported, of course:

d.slashCommand("examples", "Example commands")
  .integrationTypes(ApplicationIntegrationType.UserInstall)
  .subcommands({
    text: d
      .subcommand("Subcommand example")
      .options({
        body: d.option("Body text").string().required().maxLength(500),
        channel: d
          .option("Category channel")
          .channel()
          .required()
          .channelTypes(ChannelType.GuildCategory),
        choose: d
          .option("Option with choice")
          .integer()
          .choices({ first: 1, second: 2, third: 3 }),
        auto: d
          .option("Autocompleted number")
          .number()
          .autocomplete((value) => ({
            up: Math.ceil(parseFloat(value)),
            down: Math.ceil(parseFloat(value)),
          })),
      })
      .handler(async (interaction, { body, channel, choose, auto }) => {
        await interaction.reply(JSON.stringify({ body, channel, choose, auto }))
      }),
  })

In the second example, the type of the deconstructed parameter is inferred as:

{
  body: string
  channel: CategoryChannel
  choose?: 1 | 2 | 3
  auto?: number
}

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

About

A fluent API for writing type-safe discord.js commands, event handlers and components.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors