> For the complete documentation index, see [llms.txt](https://swiftcord.gitbook.io/discordkit-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://swiftcord.gitbook.io/discordkit-guide/build-a-bot/adding-command-options.md).

# Adding Command Options

You now have a bot that uses slash commands, what's next? Command options, of course! They make your commands dynamic and are a crucial part of most commands. And of course, they're easy-as-pie to work with, thanks to the `resultBuilder` syntax used by DiscordKit.&#x20;

{% hint style="info" %}
**What are `resultBuilder`s?**

If you've used SwiftUI or even DiscordKit, you've already used them! They allow the special syntax you can use to create commands! It's special syntax that allows building multiple "blocks" into a "stack" (i.e. array), such as creating SwiftUI views or registering your bot's commands! In this page, we'll be further using this syntax to register, you guessed it, command options.
{% endhint %}

Let's try adding options to the **/hello** command we previously added. A `name` option would be really handy, so we could give the user personalised hello messages for increased *human-ness*.

To do so, we can add option children blocks to the **/hello** command we defined earlier, similar to how you'd add children to a SwiftUI view:

<pre class="language-swift"><code class="lang-swift">NewAppCommand("hello", description: "Get a nice hello message") {
    StringOption("name", description: "Your beautiful name")
} <a data-footnote-ref href="#user-content-fn-1">handler:</a> { interaction in
    print("Received hello interaction!")
    // Reply with a random hello message
    try? await interaction.reply(HELLO_MESSAGES.randomElement()!)
}
</code></pre>

Then, we'd need to handle the value of the name option, if one is given. As we did not specify that our option is required, it's optional by default. The `interaction` struct passed to your handler contains the `optionValue(of:)` method that allows easy retrieval of option values. Let's modify the handler to send back a personalised hello message if a name was specified, otherwise just return a random hello message as usual.

```swift
    ...
} handler: { interaction in
    print("Received hello interaction!")
    if let name: String = interaction.optionValue(of: "name") {
        try? await interaction.reply("Hello \(name), hope you're having a wonderful day!")
    } else {
        // Reply with a random hello message
        try? await interaction.reply(HELLO_MESSAGES.randomElement()!)
    }
}
```

Here, we're using an optional binding to conditionally unwrap and run a block of code if the **name** option isn't `nil` (which means it was specified by the user). The `else` clause will be run if the value of the option is `nil`.&#x20;

Give the command a go - with just a few lines, you've added and handled your bot's first command option! How cool is that?

<figure><img src="/files/R9Bor3n2V3wnPEassV4R" alt=""><figcaption><p>bbbbb &#x3C;insert realistic surname></p></figcaption></figure>

{% embed url="<https://github.com/SwiftcordApp/DiscordKitGettingStarted/tree/adding-command-options>" %}
That didn't work? Check out the code in this branch for a helping hand!
{% endembed %}

[^1]: Take note of the addition of this label: It's required as there are now 2 clauses, so a label for the latter option is required.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://swiftcord.gitbook.io/discordkit-guide/build-a-bot/adding-command-options.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
