Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add slash command builders example, fixes #7338 #7339

Merged
merged 6 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/builders/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ pids
# Dist
dist/
typings/
docs/**/*
!docs/index.yml
!docs/README.md
kyranet marked this conversation as resolved.
Show resolved Hide resolved



# Miscellaneous
.tmp/
Expand Down
102 changes: 102 additions & 0 deletions packages/builders/docs/examples/Slash Command Builders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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').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],
]),
);
sanjaybaskaran01 marked this conversation as resolved.
Show resolved Hide resolved

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

## Subcommands and subcommand 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),
),
),
);
sanjaybaskaran01 marked this conversation as resolved.
Show resolved Hide resolved

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