# Add a Slash Command to a discord.js Bot

Yes. An agent adds a slash command to a discord.js bot the way the repository already builds them, with its option, strings, help entry and a test, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-slash-command/discord-js/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

A slash command in a real bot is rarely one file, and the agent finds out how many by reading an existing one end to end. In a bot built on KevinNovak/Discord-Bot-TypeScript-Template, `/info` lives in five places: the class in `src/commands/chat/info-command.ts`, its export in `src/commands/chat/index.ts`, its option in `src/commands/args.ts`, its registration payload in `ChatCommandMetadata` in `src/commands/metadata.ts`, and its instance in the `commands` array in `src/start-bot.ts`. Its name and description are not in the code at all. They are `chatCommands.info` and `commandDescs.info` in `lang/lang.en-US.json`, read through `Lang.getRef`, with localized names built by `Lang.getRefLocalizationMap`.

Take `/roll`, a dice command with one integer option. The agent adds `chatCommands.roll`, `commandDescs.roll`, `arguments.sides`, `argDescs.rollSides` and a `displayEmbeds.roll` embed to `lang/lang.en-US.json`, and the same keys to `lang/lang.en-GB.json`, because `Language.Enabled` lists both locales and a key missing from one falls back silently. In `args.ts` it adds `ROLL_SIDES` with `ApplicationCommandOptionType.Integer` and a `min_value` and `max_value`, so Discord rejects a bad number before the bot sees it. In `metadata.ts` it adds a `ROLL` entry shaped like `TEST`.

The class follows the house pattern exactly: `RollCommand implements Command`, `names` from `Lang.getRef`, a `cooldown` of `new RateLimiter(1, 5000)` as on `/test`, a `deferType`, an empty `requireClientPerms`, and an `execute` that reads the option with `intr.options.getInteger`, calls the existing `RandomUtils.intFromInterval` and answers through `InteractionUtils.send` with `Lang.getEmbed`. It writes no new helper where one exists. It then adds the command to the help text: `displayEmbeds.helpCommands` lists commands as `{{CMD_LINK_TEST}}` and `{{CMD_LINK_INFO}}`, filled in by `help-command.ts` with `FormatUtils.commandMention`, so the new one needs a `{{CMD_LINK_ROLL}}` line in both language files and a matching variable there.

The test goes beside `tests/commands/chat/test-command.test.ts` and uses the same tools: `interactionBuilder()` for the interaction and `mockRandomValues` from `tests/helpers/test-utils.ts` to make the roll deterministic. It covers the default number of sides, the bounds and the embed's content. Then the agent runs `npm test`, `npm run lint`, `npm run format` and `npm run build`. Lint matters here, because `eslint.config.mjs` requires explicit return types, typed parameters and property declarations, alphabetized imports and single quotes, and Prettier is set to four spaces at width 100.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a /roll slash command to this discord.js bot: one optional integer option, sides, from 2 to 100 with a default of 6, answered publicly with an embed that names the user and the result. Read CLAUDE.md, src/commands/chat/info-command.ts and test-command.ts, src/commands/args.ts, src/commands/metadata.ts, src/commands/chat/index.ts, src/start-bot.ts, src/commands/chat/help-command.ts, src/services/lang.ts, lang/lang.en-US.json, lang/lang.en-GB.json, eslint.config.mjs and tests/commands/chat/test-command.test.ts first, and follow the pattern they show. Put every user-facing string in both language files; hardcode none. Define the option in args.ts with min_value and max_value, the registration payload in metadata.ts, the class in src/commands/chat/roll-command.ts using RandomUtils.intFromInterval and InteractionUtils.send, export it from the chat index and add it to the commands array in start-bot.ts. Add the command to the help embed in both language files and pass its command link in help-command.ts. Write a Vitest test beside the existing command tests using interactionBuilder and mockRandomValues, covering the default, both bounds and the embed text. Run npm test, npm run lint, npm run format and npm run build and fix what they report. Do not register the command with Discord. Open a pull request that lists every file touched and why, with the output of the four commands and the one command a maintainer must run after merging.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request touches seven existing files and adds two: the command class and its test. The description walks through them in the order a command is wired, from the language keys to the `commands` array, so a reviewer can check that nothing in the chain is missing. A command that is in `metadata.ts` but not in `start-bot.ts` registers with Discord and then logs `commandNotFound` when someone uses it; one that is in `start-bot.ts` but not in `metadata.ts` never appears in the picker. The output of `npm test`, `npm run lint`, `npm run format` and `npm run build` is pasted under that.

The description ends with the step the agent did not take. The new command does not exist for users until someone with the bot's token runs `npm run commands:register`, and the README says a change can take up to an hour to appear. The agent has no token and the request tells it not to register, so that line is addressed to whoever merges.

A reviewer looks at three things. The option's bounds are in the payload, not only in the code. Both language files have every new key. The test asserts what the user would see, the number in the embed, and not just that `send` was called.

## Run it on every command request

Command ideas arrive as one-line messages, and each is the same nine-file change. When the request lives in your issue tracker, a small handler on your server sends this page's body to `POST /o/{org}/sessions` with the command's name, options and reply written into `input`, the repository in `resources`, and a `request_id` that is a UUID v5 of the issue's id, so a retried delivery returns the run it already started. Only your handler can write those values into `input`.

If the requests are GitHub issues, an automation in the Project that holds the repository does it without a server: a `github` trigger on `issues.labeled` with a label such as `new-command` in `labels`, and a saved prompt that says to build the command the issue describes, following the pattern in the repository. The trigger places the issue after the prompt and fills in nothing, and it fires only for an issue that an owner, member or collaborator opened. A label on an outside user's issue starts nothing, so a maintainer opens the issue for a request that came from the community.

## Why run it on Rush?

The work is small and easy to get subtly wrong, which is the case for handing it off. A developer adding a command between other tasks copies `test-command.ts`, forgets the second language file or the help entry, and finds out from a user. The agent reads the wiring of an existing command first and reproduces every link in it, then runs the linter that enforces the repository's style on its own code, in a sandbox that has nothing but the clone.

It is also work you can hand off and walk away from. The run does not need your terminal, your token or your attention, and what comes back is a pull request with the commands it ran and their output in the run's transcript. If three commands were asked for this week, three runs start at once and you review three small diffs that do not touch each other's files beyond the shared lists.

## Frequently asked questions

### Does the command show up in Discord after the pull request merges?

Not by itself. Slash commands are registered with Discord's API, and in this template that is `npm run commands:register`, run with the bot's token. The pull request says so, and the agent does not run it because it has no token and registration changes what users see.

### What if my bot does not use this template?

The request tells the agent to read how an existing command is built and follow it, so it adapts. A bot that defines commands with `SlashCommandBuilder` in one file per command and a deploy script gets a new file in that folder and nothing else. Name the files to read in your own prompt.

### Can it add a command with subcommands or autocomplete?

Yes. The `Command` interface has an optional `autocomplete` method and `names` is an array, which is how `CommandUtils.findCommand` matches a subcommand group and subcommand. Describe the shape in the prompt and the agent adds the nested options to the payload and a test for the autocomplete choices.

### Why does it edit two language files for an English-only bot?

Because `Language.Enabled` lists `en-US` and `en-GB`, and `getRefLocalizationMap` builds the localized names from every enabled language. A key present in only one file works until someone with the other locale opens the picker. If you have removed a language, the agent sees that and edits one file.

### Will two command runs conflict with each other?

They touch the same lists, `metadata.ts`, `start-bot.ts` and the language files, so the second pull request to merge may need a rebase. The conflicts are one added line in each list. A follow-up message to the run asking it to rebase on the default branch resolves them.

## More discord.js jobs

- [Enable TypeScript Strict Mode in a discord.js Bot](https://getrush.ai/run/enable-strict-mode/discord-js/)
- [Fix TypeError: Cannot Read Properties of Undefined (Reading 'FLAGS') in discord.js 14](https://getrush.ai/run/fix-intents-flags-removed/discord-js/)
- [Upgrade discord.js in a TypeScript Bot](https://getrush.ai/run/upgrade-discord-js/discord-js/)

## The same job on other stacks

- [Add Slash Commands to a discord.py Bot](https://getrush.ai/run/add-slash-command/discord-py/)
- [Add a Slash Command With a Modal to a Slack Bolt App](https://getrush.ai/run/add-slash-command/slack-bolt/)

## More jobs like this: Hand off a long task

- [Add Slim CI for a dbt Project With GitHub Actions](https://getrush.ai/run/add-slim-ci/dbt/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
