Skip to content

Commit

Permalink
feat(SlashCommands): add slash command builders (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Román <[email protected]>
Co-authored-by: SpaceEEC <[email protected]>
  • Loading branch information
3 people authored Jul 16, 2021
1 parent 6ad24ec commit 6aa3af0
Show file tree
Hide file tree
Showing 22 changed files with 9,463 additions and 6,309 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
<a href="https://github.com/discordjs/builders/actions"><img src="https://github.com/discordjs/builders/workflows/Tests/badge.svg" alt="Test status" /></a>
<a href="https://github.com/discordjs/builders/actions"><img src="https://github.com/discordjs/builders/workflows/Lint/badge.svg" alt="Lint status" /></a>
<a href="https://codecov.io/gh/discordjs/builders"><img src="https://codecov.io/gh/discordjs/builders/branch/master/graph/badge.svg" alt="Code coverage" /></a>
<a href="https://codecov.io/gh/discordjs/builders"><img src="https://codecov.io/gh/discordjs/builders/branch/main/graph/badge.svg" alt="Code coverage" /></a>
</p>
</div>

Expand All @@ -24,4 +24,10 @@ pnpm add @discordjs/builders

## Contribution

See [Contributing Guide](https://github.com/discordjs/discord.js-next/blob/master/.github/CONTRIBUTING.md).
See [Contributing Guide](https://github.com/discordjs/builders/blob/main/.github/CONTRIBUTING.md).

## Examples

Here are some examples for the builders and utilities you can find in this package:

- [Slash Command Builders](./docs/examples/Slash%20Command%20Builders.md)
16 changes: 16 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('@babel/core').TransformOptions} */
module.exports = {
parserOpts: { strictMode: true },
sourceMaps: 'inline',
presets: [
[
'@babel/preset-env',
{
targets: { node: 'current' },
modules: 'commonjs',
},
],
'@babel/preset-typescript',
],
plugins: ['babel-plugin-transform-typescript-metadata', ['@babel/plugin-proposal-decorators', { legacy: true }]],
};
105 changes: 105 additions & 0 deletions docs/examples/Slash Command Builders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Slash Command Builders

## Ping command

```ts
import { SlashCommandBuilder } from '@discordjs/builders';

// Create a slash command builder
const pingCommand = new SlashCommandBuilder().setName('ping').setDescription('Check if this interaction is responsive');

// Get the raw data that can be sent to Discord
const rawData = pingCommand.toJSON();
```

## Arguments

```ts
import { SlashCommandBuilder } from '@discordjs/builders';

// Creates a boop command
const boopCommand = new SlashCommandBuilder()
.setName('boop')
.setDescription('Boops the specified user, as many times as you want');

// Adds a user argument to the command
boopCommand.addUserOption((option) => option.setName('user').setDescription('The user to boop').setRequired(true));

// Adds an integer option
boopCommand.addIntegerOption((option) =>
option.setName('boop_amount').setDescription('How many times should the user be booped (defaults to 1)'),
);

// Supports choices too!
boopCommand.addIntegerOption((option) =>
option
.setName('boop_reminder')
.setDescription('How often should we remind you to boop the user')
.addChoice('Every day', 1)
.addChoice('Weekly', 7)
// Or, if you prefer adding more choices at once, you can use an array
.addChoices([
['Every three months', 90],
['Yearly', 365],
]),
);

// Get the final raw data that can be sent to Discord
const rawData = boopCommand.toJSON();
```

## Sub commands and sub command groups

```ts
import { SlashCommandBuilder } from '@discordjs/builders';

const pointsCommand = new SlashCommandBuilder().setName('points').setDescription('Lists or manages user points');

// Add a manage group
pointsCommand.addSubCommandGroup((group) =>
group
.setName('manage')
.setDescription('Shows or manages points in the server')
.addSubCommand((subCommand) =>
subCommand
.setName('user_points')
.setDescription("Alters a user's points")
.addUserOption((option) =>
option.setName('user').setDescription('The user whose points to alter').setRequired(true),
)
.addStringOption((option) =>
option
.setName('action')
.setDescription('What action should be taken with the users points?')
.addChoices([
['Add points', 'add'],
['Remove points', 'remove'],
['Reset points', 'reset'],
])
.setRequired(true),
)
.addIntegerOption((option) => option.setName('points').setDescription('Points to add or remove')),
),
);

// Add an information group
pointsCommand.addSubCommandGroup((group) =>
group
.setName('info')
.setDescription('Shows information about points in the guild')
.addSubCommand((subCommand) =>
subCommand.setName('total').setDescription('Tells you the total amount of points given in the guild'),
)
.addSubCommand((subCommand) =>
subCommand
.setName('user')
.setDescription("Lists a user's points")
.addUserOption((option) =>
option.setName('user').setDescription('The user whose points to list').setRequired(true),
),
),
);

// Get the final raw data that can be sent to Discord
const rawData = pointsCommand.toJSON();
```
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
testMatch: ['<rootDir>/tests/**/*.test.ts'],
transform: {
'^.+\\.ts$': '@swc/jest',
},
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
Expand Down
Loading

0 comments on commit 6aa3af0

Please sign in to comment.