> 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/responding-to-commands.md).

# Responding to Commands

Hello World! Coming through loud and clear!

Now that you've registered a command, lets respond to it! For the /hello command, we want our bot to respond with a random fun message:

* Hello, World!
* Helloooo! I can hear you!
* Who's there?
* Beep, boop, I'm a bot.
* I'm alive!

Let's start by putting all those messages in an array in our `Bot` struct:

{% code lineNumbers="true" %}

```swift
@main
public struct Bot {
    private static let HELLO_MESSAGES = [
        "Hello, World!",
        "Helloooo! I can hear you!",
        "Who's there?",
        "Beep, boop, I'm a bot.",
        "I'm alive!"
    ]
    ...
```

{% endcode %}

We can use the built-in `.randomElement()` method on our array to get a random hello message. As we're sure our array isn't empty, we can safely force-unwrap the result with `!`. Putting all that together in our interaction's handler function gives us:

```swift
NewAppCommand("hello", description: "Get a nice hello message") { interaction in
    print("Received hello interaction!")
    // Reply with a random hello message
    try? await interaction.reply(HELLO_MESSAGES.randomElement()!)
}
```

Run your bot and invoke the /hello command. Your bot should respond with a random message each time, as shown below! Congrats on your first functional slash command!

<figure><img src="https://1903821505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWJuHiYLW9jKqPb7h8D7t%2Fuploads%2FGirQVFjM3S8IBXtjWT7S%2FScreenshot%202022-12-15%20at%206.00.52%20PM.png?alt=media&amp;token=8e45c328-05e4-4eb0-a75b-6aa9c6cc9b42" alt=""><figcaption><p>Hello World!</p></figcaption></figure>

{% hint style="info" %}
You'll have to respond to an interaction within 3 seconds, if not the Discord client will display an error stating that "The interaction did not respond".

![](https://1903821505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWJuHiYLW9jKqPb7h8D7t%2Fuploads%2FgrRzPNMzgfP7OW8icDvX%2FScreenshot%202022-12-15%20at%206.15.32%20PM.png?alt=media\&token=5a5801b9-d528-46f9-9177-a6409a1b411e)

If you need more than 3s to respond to an interaction (e.g. a 🐌 database), you'll need to send a deferred response first (guide coming soon) to extend the deadline to 15 minutes. This will show the user a loading state which will be replaced by a response once you send one.
{% endhint %}

{% embed url="<https://github.com/SwiftcordApp/DiscordKitGettingStarted/tree/responding-to-commands>" %}
Something not working as expected? Check out this branch!
{% endembed %}
