Search…
Options
Add selectable options to your commands
Options are the primary way to take user input for slash commands, they're type-checked on Discord's side, so type safety is guaranteed!

In practise

1
import { Interaction } from 'detritus-client'
2
import { ApplicationCommandOptionTypes } from 'detritus-client/lib/constants'
3
4
import { BaseSlashCommand } from '../base'
5
6
export interface CommandArgs {
7
dice?: number
8
sides?: number
9
}
10
11
export default class DiceCommand extends BaseSlashCommand {
12
description = 'Roll some dice'
13
name = 'dice'
14
15
constructor () {
16
super({
17
options: [
18
{
19
type: ApplicationCommandOptionTypes.INTEGER,
20
name: 'dice',
21
description: 'The number of dice to roll (default: 1)',
22
required: false
23
},
24
{
25
type: ApplicationCommandOptionTypes.INTEGER,
26
name: 'sides',
27
description: 'The number of sides on the dice (default: 6)',
28
required: false
29
}
30
]
31
})
32
}
33
34
async run (context: Interaction.InteractionContext, args: CommandArgs): Promise<void> {
35
const { dice, sides } = args
36
const diceCount = dice ?? 1
37
const diceSides = sides ?? 6
38
39
let total = 0
40
for (let i = 0; i < diceCount; i++) {
41
total += Math.floor(Math.random() * diceSides) + 1
42
}
43
44
await context.editOrRespond(`${context.user.username} rolled ${diceCount}d${diceSides} and got ${total}`)
45
}
46
}
Copied!
Last modified 3mo ago
Export as PDF
Copy link
Contents
In practise