Skip to content
GitHub

The CLI

MeoCord 4.3 · Shipping · page 37 of 42 · since 4.0.0

Create a project, run it while you work, build and register it for production, and scaffold its parts.

You'll learn

  • Run the bot in development, and build and start it for production
  • Register its commands without starting it
  • Scaffold controllers, services and the rest from the command line

meocord is the command a MeoCord project runs through. It creates the project, runs the bot while you work, builds it for production, registers its commands with Discord, and writes the files for a new controller or service. This page shows how the commands fit together; the CLI reference lists every flag.

When to use it

  • While you work: start --dev rebuilds and restarts the bot on every change.
  • On the server: build --prod, then start --prod, or run the build with node dist/main.js, as Deployment does in a container.
  • When registering is its own step: register sends the commands to Discord without starting the bot, from CI for instance.
  • For a new part: generate writes a controller, service, guard, interceptor, filter, pipe or observer, with a spec beside it.

Example

npx meocord start --dev          # build, start, and rebuild and restart on every change
npx meocord g co slash Greeting  # a /greeting command: its controller, spec and builder
npx meocord build --prod         # a production build in dist/
npx meocord start --prod         # start that build
bunx meocord start --dev         # build, start, and rebuild and restart on every change
bunx meocord g co slash Greeting # a /greeting command: its controller, spec and builder
bunx meocord build --prod        # a production build in dist/
bunx meocord start --prod        # start that build
pnpm exec meocord start --dev         # build, start, and rebuild and restart on every change
pnpm exec meocord g co slash Greeting # a /greeting command: its controller, spec and builder
pnpm exec meocord build --prod        # a production build in dist/
pnpm exec meocord start --prod        # start that build
yarn meocord start --dev         # build, start, and rebuild and restart on every change
yarn meocord g co slash Greeting # a /greeting command: its controller, spec and builder
yarn meocord build --prod        # a production build in dist/
yarn meocord start --prod        # start that build

Inside a project, npx meocord runs the version the project installed. Keep start --dev running while you work, and add a part with generate beside it: the bot rebuilds and restarts once something imports the new files, as the controller generate adds to src/app.ts does.

How it works

build, start --dev and any command given --build read meocord.config.ts first; start --prod and register read the config the last build compiled into dist/, and meocord.config.ts only when there is none:

  • a file that fails to load stops them with its path and the error, and for meocord.config.ts the line it failed at;
  • an option of the wrong type stops them with a list of every problem;
  • an option MeoCord doesn't know is reported as a warning.

start and register then check for the token, and stop at once when discordToken is empty. Every failure exits with code 1, so a script or a CI job stops too.

build and start build for development unless they're given -p, --prod, which wins when both -d and -p are given. register works the other way round: it registers as production does, unless it's given -d, --dev. A development command runs as development whatever NODE_ENV the shell holds. A production one, meaning --prod, or register without --dev, keeps a NODE_ENV the shell sets and is production otherwise. A CI shell with NODE_ENV=development therefore registers to commands.developmentGuild.

The bot runs on the runtime you launched the CLI with. In an app created with npm, yarn or pnpm, bun run start:prod runs it under Bun and npm run start:prod under Node; an app created with bun has bun --bun in its scripts, so they run it under Bun however they're started. Which runtime the bot runs on covers pinning one.

Creating a project

npx meocord create my-bot
bunx meocord create my-bot
pnpm dlx meocord create my-bot
yarn dlx meocord create my-bot

create writes a project into a new folder named after the app, in kebab case, and installs it. Accented letters fold to their base letters, so Café Bot gives cafe-bot, and a name needs Latin letters or digits. It asks which package manager to use; --use-npm, --use-yarn, --use-pnpm or --use-bun answers for it, and fails when that package manager isn't installed. create refuses a folder that already exists. The prompt suggests bun when it's installed, and npm otherwise. When the install fails, create prints the package manager's error, removes the folder so the same name can be tried again, and exits with code 1. On a Node older than 22.13 it warns, and still creates the app. It's the one command run outside a project, so the command above names the version this guide documents. Getting started walks through it.

After the install, create makes the folder a git repository and commits it, lockfile included. When git can't make that commit, on a machine with no user.email for instance, or with no git at all, the project stays as it is, and create prints the commands that finish the commit. Inside an existing repository, it makes no new one and leaves the files to that repository.

Development and production

start --dev builds the bot as it starts, then watches the project: a change rebuilds it and restarts the bot, and meocord.config.ts and tsconfig.json are watched too. A change to .env, .env.local, .env.development or .env.development.local restarts the bot without a rebuild, since the bot reads them as it starts, as development whatever NODE_ENV the shell holds. It registers the commands to commands.developmentGuild, or where production would without one, and only when they changed since the last development start or differ from what Discord holds; --force-register sends them anyway.

A restart stops the running bot as Ctrl+C does, so its onShutdown hooks run before the new one starts, on every platform, Windows included. One save makes one restart, even when it makes two builds of the same output.

A bot that can't log in, or exits on its own, leaves watch mode running: the next change starts it again. So does a rebuild that can't start, as when a saved meocord.config.ts has an rsbuild hook that throws: the bot keeps running its last build, watch mode says why, and saving again retries. A saved config is loaded and checked for options of the wrong type, as at startup. One with problems, or one that fails to load, is reported, and the running bot is left as it is. The token isn't checked again: a config that loses it restarts the bot, which then can't log in. When watch mode itself can't start, start --dev stops the bot it started and exits with code 1, as build does. In a terminal, it clears the screen as it starts and keeps your scrollback; build and start --prod never clear it, and write no escape codes into piped output such as a CI log or docker logs.

For production, build once and start the build:

npx meocord build --prod
npx meocord start --prod
bunx meocord build --prod
bunx meocord start --prod
pnpm exec meocord build --prod
pnpm exec meocord start --prod
yarn meocord build --prod
yarn meocord start --prod

start --prod runs the build in dist/ as it is, so build again after every change, or use start --build --prod to do both in one command. --build matters only with --prod, since start --dev always builds.

Without --build, start --prod and register warn when the build in dist/ is older than your source, naming the first file changed since, or when it is a development build, and then run it as before:

text
dist was built before src/controllers/slash/ping.slash.controller.ts changed, so start --prod runs the previous build.
Add --build, or run meocord build --prod first.

Every build records its mode and the files it read in dist/meocord.build.json: the source, tsconfig.json and each file it extends, and with bundleDependencies the lockfile. The check hashes only files whose size or modification time changed, so a checkout that touches files without changing them gives no warning, and it stops after about 200 ms, saying nothing. A dist with no record beside it, as on a server that has only dist, gives no warning either.

Registering commands

A production start registers every command as the bot logs in, and Discord applies an unchanged set without harm. register does the same without starting the bot. It runs the last build in dist/, so it registers the commands that build holds: add --build after changing a command.

npx meocord register --build           # build, then register every command
npx meocord register --guild 123456789  # every command, to this server only
npx meocord register --dev             # to commands.developmentGuild, as development does
bunx meocord register --build          # build, then register every command
bunx meocord register --guild 123456789 # every command, to this server only
bunx meocord register --dev            # to commands.developmentGuild, as development does
pnpm exec meocord register --build          # build, then register every command
pnpm exec meocord register --guild 123456789 # every command, to this server only
pnpm exec meocord register --dev            # to commands.developmentGuild, as development does
yarn meocord register --build          # build, then register every command
yarn meocord register --guild 123456789 # every command, to this server only
yarn meocord register --dev            # to commands.developmentGuild, as development does

A blank guild id, as an unset variable gives, stops with an error rather than registering where commands.* says. Each run registers as its own flags and commands say: a variable left in your shell or .env, such as MEOCORD_REGISTER_GUILD, steers neither register nor start.

Removing commands left in other servers

Commands registered to a server stay there when the configuration moves on, such as a test server after you moved to global commands, and its members then see every command twice. --prune finds and removes them:

npx meocord register --prune --dry-run  # report what would be removed, and change nothing
npx meocord register --prune            # register, then remove this build's commands from other servers
npx meocord register --prune all --yes  # also remove commands this build doesn't declare, without asking
bunx meocord register --prune --dry-run # report what would be removed, and change nothing
bunx meocord register --prune           # register, then remove this build's commands from other servers
bunx meocord register --prune all --yes # also remove commands this build doesn't declare, without asking
pnpm exec meocord register --prune --dry-run # report what would be removed, and change nothing
pnpm exec meocord register --prune           # register, then remove this build's commands from other servers
pnpm exec meocord register --prune all --yes # also remove commands this build doesn't declare, without asking
yarn meocord register --prune --dry-run # report what would be removed, and change nothing
yarn meocord register --prune           # register, then remove this build's commands from other servers
yarn meocord register --prune all --yes # also remove commands this build doesn't declare, without asking

After a successful registration, --prune checks every server the bot is in, one request per command it removes. It removes only the commands this build declares, by name and type, from servers the configuration no longer names, and lists the others it keeps. --prune all removes those too, asking once in a terminal, or without asking given --yes. --prune-guild <id> adds a server the bot isn't in, and --after <id> resumes a run that stopped early, as its message says. A development start also warns when this checkout once registered to a server the configuration no longer names. Set commands.register to false to keep registering out of startup, and run register once per deploy instead. Registering commands covers the scopes.

Scaffolding

generate, or g, writes a part and its spec:

SubcommandAliasWrites
controllercoa controller, its spec, its builder
servicesa service and its spec
guardgua guard and its spec
interceptorian interceptor and its spec
filterfan exception filter and its spec
pipepia pipe and its spec
observeroba dispatch observer and its spec

A controller or an observer is added to src/app.ts: its import after your last import, and its entry in @MeoCord({ controllers }) or @MeoCord({ observers }). The edit uses your project's own typescript, inserts only those lines in the list's own layout, and is linted with your project's ESLint, without fixing: an edit that would bring an error the file didn't have is undone. When the class can't be placed safely, such as a list that isn't an array written in place, a name already taken in the file, or no @MeoCord({ … }) to edit, generate writes the files, warns why, prints the two lines to add, and leaves src/app.ts as it was. --skip-app leaves it alone too. Other parts are used by naming them in code: generate prints where, and a service is bound the first time something injects it.

Each part goes in src/<kind>s/, such as src/services/<name>.service.ts beside <name>.service.spec.ts. A file name is the name in kebab case, and the class name is the whole path in Pascal case: g s billing/invoice writes src/services/billing/invoice.service.ts with the class BillingInvoiceService.

Once the files are written, generate formats them with the project's own ESLint, in one run, and says "Formatting with your project's ESLint..." while it waits. When it can't run, or reports problems it can't fix, generate says "Could not format the generated files:" and why, and still exits 0. Either way, the files are kept, with whatever ESLint fixed. A project without ESLint skips the step.

Controllers

npx meocord g co <type> <name>
bunx meocord g co <type> <name>
pnpm exec meocord g co <type> <name>
yarn meocord g co <type> <name>

<type> is one of button, modal-submit, select-menu, user-select-menu, role-select-menu, mentionable-select-menu, channel-select-menu, reaction, message, slash, autocomplete, context-menu and primary-entry-point. Each lands in a folder named after its type:

text
src/controllers/<type>/
├── <name>.<type>.controller.ts
├── <name>.<type>.controller.spec.ts
└── builders/<name>.builder.ts     # slash, context-menu and primary-entry-point only

<name> is the name in kebab case: Greeting writes greeting.slash.controller.ts.

  • A builder comes only with the three types Discord registers by name. npx meocord g co slash Greeting registers /greeting. The builder receives that name from @Command, so the two can't drift apart. The other types are reached by custom ID; a message controller by the message's text, a reaction controller by its emoji, and an autocomplete controller by the command it completes.
  • The spec invokes the handler through the testing module, with the input Discord would send it, and checks its answer: a command's reply, a button's update, the choices an autocomplete offers.
  • An autocomplete controller completes the query option of the command it's named after, which already exists with its own builder. generate leaves that builder alone and prints the option to add to it, with setAutocomplete(true); until the command declares it, Discord never asks the handler.
  • A message context menu is npx meocord g co context-menu Report --message; without --message, it's a user context menu.
  • A name with / nests the files: npx meocord g co slash admin/ban writes into src/controllers/slash/admin/ and registers /admin-ban, since a command's name is global to the application.

Where a controller sits is only a convention: @MeoCord({ controllers }) is what wires it up.

Gotchas

  • generate never overwrites. When a file it would write exists, it names the files and writes nothing.
  • generate runs from the project's root, beside package.json; in a folder without a package.json, it refuses rather than scatter files. A name can't leave its folder: .., a leading / and a drive letter are refused. A name must start with a letter, since it becomes a class name: g s 2fa is refused, and g s two-factor is not.
  • start --prod runs the last build, and warns when your source changed since; add --build.
  • --message is for context menus only. On any other type, generate refuses it.

Next steps